Problem with simple octal to decimal code

Hello all
I started computer science and C++ programming only 6 weeks ago with no prior knowledge. It's a great language, the only other one apart from English that I hope to speak fluently quite soon. I look forward to participating further on these forums, seeking answers and looking at previous threads. Hopefully I will be able to help others later on. Let me cut to the chase.

  1. The problem statement, all variables and given/known data:
    Although this is an assignment question, it isn't due for a while, and I'm mainly only awake at 4:20am for the sake of it. The question asks to write a C++ program that converts a 4-digit octal number to decimal. I wrote and compiled it, but there's something wrong. I don't think it's arithmetic, because the program terminates at return 1, even if there isn't an 8 or a 9.

  2. Relevant commands, code, scripts, algorithms:

#include <iostream>
using namespace std;
int main()
{
    int number = 0;
    int digit4 = 0;
    int digit3 = 0;
    int digit2 = 0;
    int digit1 = 0;
    int decimal = 0;

    cout << "Enter a four digit octal number to convert to decimal form: " << endl;
        cin >> number ;

    ( digit4 = number/1000 );
    ( digit3 = number/100 - digit4*1000 );
    ( digit2 = number/10 - digit4*1000 - digit3*100 );
    ( digit1 = number - digit4*100 - digit3*100 - digit2*10);

    cout << digit1 << digit2 << digit3 << digit4 << endl; // This was me testing each digit. 

    if (digit1 || digit2 || digit3 || digit4 == 8 || 9)
    {
        cout << "The number entered contains the digit 8 and/or 9. Please try again." << endl;
        return 1;
    } 
    if (number > 7777)
    {
        cout << "The number entered exceeds the limit for this program, please try again." << endl;
        return 2;
    }
    (decimal = digit1 + digit2*8 + digit3*64 + digit4*512);

    cout << "The entered octal number is equal to: " << decimal << "in decimal form." << endl;

return 0;
}
  1. The attempts at a solution (include all code and scripts):
    As you can see in the code, i checked the output of each digit. It returned a dud result.

Tried removing the if statement testing for 8s and 9s. Returned a large negative number.

Also tried other fine-tuning methods to no avail.

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

University of Wollongong, Wollongong, Australia, Daniel Saffioti, CSCI114

Are you allowed to use the std library? in C (this is portable but not great C++)

char octal[]="0144";
long decimal=strtol(octal, (char **)0, 8);

decimal is now the decimal number converted from octal.

    ( digit4 = number/1000 );
    ( digit3 = number/100 - digit4*1000 );

That does not compute. Try the computation yourself with, say, number=1234.

1 Like

Wow, I can't believe I didn't pick that up:wall:. Thanks a lot