-
Really weird int problem
Okay, I have this program that im making that's a login program. It uses classes, functions and everything, but what's giving me problems right now is a simple integer in the int main() section.
Basically, I declare an int (doesnt matter if i declare it as an int or const int...same thing happens) and set it to a value. A few lines later I tell it to cout that value...and it outputs a different value than it should.
declaration:
Code:
int pass = 01103657;//'secret' password
cout:
Here's the troublesome section of code:
Code:
//Main Function
int main()
{
//Variable declaration
int choice, count = 0, i;
int idnum, pwd;
int pass = 01103657;//'secret' password
//pass=01103657;
char firstname[26];
char lastname[26];
//Pointer to new data structure in class
Students *stdnt = new Students[100];//Students not logged in
//Students *stdnt2 = new Students[100];//Student logged in
do//Infinite(for now) Menu Loop
{
//Main Menu
system("cls");//Clear screen
cout << "**** MCC COMPUTER LAB LOGON ****\n";
cout << " !!!PLEASE LOGIN!!! \n\n";
cout << "Please choose:\n";
cout << "1. Login\n";
cout << "2. Logout\n";
cout << "3. New Student\n";
cout << "Pleas note that you are a New Student if\n"
<< "this is your first time here this semester!!\n\n";
cout << pass;
cin >> choice;//Enters menu choice
When I run the program, the cout gives me '296879' instead of '01103657'.
I can post the whole program if need be.
-
Code:
#include <iostream>
int main() {
if (010 == 8) {
std::cout << "8 in decimal is 010 in octal\n"
<< "01103657 in octal is 296879 in decimal\n";
}
}
-
If you delete all the irrelevant lines, your program distills to this:
Code:
int pass = 01103657;//'secret' password
cout<<pass<<endl; //296879
If you eliminate the leading 0, it displays the correct value. In C++, when you write integers with a leading 0, the compiler interprets that as an octal(base 8) representation of the integer value.
You can use a C++ string type for the password instead:
Code:
#include<string>
....
string password = "01103657";
-
Thanks for the replys. Shortly after I posted the question, i realized the answer, lol.
Now im getting a new error with a different part of the program *sigh*
-
Post the whole code if you want to.