Really weird int problem

This is a discussion on Really weird int problem within the C++ Programming forums, part of the General Programming Boards category; Okay, I have this program that im making that's a login program. It uses classes, functions and everything, but what's ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    25

    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:
    Code:
    cout << pass;
    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.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    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";
    	}
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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";
    Last edited by 7stud; 03-08-2006 at 10:02 AM.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    25
    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*

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Post the whole code if you want to.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 03:24 PM
  4. problem with sorting
    By pinkpenguin in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 10:06 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 06:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21