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.
-
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. -
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;
}
- 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.
- 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