Thread: Help with "cin" in C++

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    11

    Help with "cin" in C++

    Hello, I am somewhat I newbie to C++ programming and I require some help with this. I am writing a simple organizer program but I run into a problem: I run it and it gives me the main menu, and I enter an option, but then it just blows off the option number and gives me the next function in the code. Here is the code:

    life.cpp
    Code:
    #include <iostream.h>
    #include "menu.cpp"
    using namespace std;
    int main()
    {
    	int answerget;
    	startmenu();
    	cin >>answerget;
    	if (answerget = "1")
    			contactsmenu();
    	if (answerget = "2")
    			notepadmenu();
    	if (answerget = "0")
    			cout << "\nGood Bye\n";
    			return 0;
    }
    main.cpp
    Code:
    void startmenu()
    {
    	cout << "\nWelcome to Life V.00001";
    	cout << "\n Select option number:";
    	cout << "\n[1]Contacts";
    	cout << "\n[2]Notepad";
    	cout << "\n[0]Quit\n";
    }
    
    void contactsmenu()
    {
    	int contactmget;
    	cout << "\n CONTACTS--";
    	cout << "\n\n[A]List Contacts";
    	cout << "\n[B]Add Contact";
    	cout << "\n[C]Edit Contact";
    	cout << "\n[Q]MainMenu\n";
    	cin >> contactmget;
    	if ( contactmget = 0)
    			startmenu();
    }
    
    void notepadmenu()
    {
    	int notepadmget;
    	cout << "\n NOTEPAD--";
    	cout << "\n\n[1]New File";
    	cout << "\n[2]Open File";
    	cout << "\n[0]MainMenu\n";
    	cin >> notepadmget;
    	if ( notepadmget = 0)
    			startmenu();
    }
    Can anyone help me out?

  2. #2
    Registered User
    Join Date
    Jul 2005
    Posts
    20
    Try this:

    Code:
    #include <iostream.h>
    #include "menu.cpp"
    using namespace std;
    int main()
    {
    	int answerget;
    	startmenu();
    	cin >>answerget;
    	if (answerget == 1)
    			contactsmenu();
    	if (answerget == 2)
    			notepadmenu();
    	if (answerget == 0)
    			cout << "\nGood Bye\n";
    			return 0;
    }
    See the errors?

  3. #3
    Banned
    Join Date
    Jun 2005
    Posts
    594
    first of all your using deperciated header (iostream.h)
    and i guess its a prefrence but i look at it as good
    programming to use brackets*.


    *like with your if statments


    you also might possibly consider using cin.ignore();
    after your cin>> statments to take the newline
    character off the buffer as to not mess up any other
    cin>> statments.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    11
    Its actually still not working right. I did a quick test, and it is not working for me:

    Code:
    #include <iostream.h>
    #include "menu.cpp"
    using namespace std;
    int main()
    {
            int answerget;
            startmenu();
            cin >>answerget;
            if (answerget == 1)
    			cin.ignore();
                            contactsmenu();
    	if (answerget == 2)
    			cin.ignore();
                            notepadmenu();
            if (answerget == 0)
                            cout << "\nGood Bye\n";
                            return 0;
    }
    Am I still not doing this right?
    Last edited by Phan; 07-26-2005 at 10:27 PM.

  5. #5
    *this
    Join Date
    Mar 2005
    Posts
    498
    Code:
    #include <iostream.h>
    #include "menu.cpp"
    using namespace std;
    int main()
    {
            int answerget;
            startmenu();
            cin >>answerget;
            cin.ignore(); // you only need it here
    
            // the reason why it didnt work here is because you forgot brackets "{" and "}".
            // for more than one statement you need brackets.
            if (answerget == 1)
                            contactsmenu();
    	if (answerget == 2)
                            notepadmenu();
            if (answerget == 0)
                            cout << "\nGood Bye\n";
                            return 0;
    }

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    you also might possibly consider using cin.ignore();
    after your cin>> statments to take the newline
    character off the buffer as to not mess up any other
    cin>> statments.
    The >> operator skips over any leading whitespace, e.g. a newline, so using cin.ignore() has no effect on any subsequent cin>> statements.


    Code:
    void contactsmenu()
    {
    	int contactmget;
    	cout << "\n CONTACTS--";
    	cout << "\n\n[A]List Contacts";
    	cout << "\n[B]Add Contact";
    	cout << "\n[C]Edit Contact";
    	cout << "\n[Q]MainMenu\n";
    	cin >> contactmget;
    	if ( contactmget = 0)
    			startmenu();
    }
    
    void notepadmenu()
    {
    	int notepadmget;
    	cout << "\n NOTEPAD--";
    	cout << "\n\n[1]New File";
    	cout << "\n[2]Open File";
    	cout << "\n[0]MainMenu\n";
    	cin >> notepadmget;
    	if ( notepadmget = 0)
    			startmenu();
    }
    Neither of those functions does anything if the user enters anything but 0. In addition, you didn't learn your lessons from the people who posted earlier. Write and post a 3 page essay on the differences between '=' and '=='.
    Last edited by 7stud; 07-27-2005 at 01:22 AM.

  7. #7
    Banned
    Join Date
    Jun 2005
    Posts
    594
    it mess up the cin >> statment by going pass the statement
    if \n is on the buffer, cin.ignore() get it off the buffer so as not
    to affect the other cin >> statments.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	char a, b, c;
    	cin >> a;
    	cin >> b;
    	cin >> c;
    	cout << a << "   " << b << "   " << c << endl;
    	cin.get();
    }
    compared to this

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	char a, b, c;
    	cout << "letter : ";
    	cin >> a;
    	cin.ignore(80, '\n');
    	cout << "letter : ";
    	cin >> b;
    	cin.ignore(80, '\n');
    	cout << "letter : ";
    	cin >> c;
    	cin.ignore(80, '\n');
    	cout << a << "   " << b << "   " << c << endl;
    	cin.get();
    }
    if you try to enter more character then a, b, or c, can hold
    then ignore removes them off the buffer so not to affect
    the other cin >> statments.

    id say it was good coding practice, to handle all possibly mistake
    by the user, so it may not have been a priority here, but
    shoudl still be something to consider.
    Last edited by ILoveVectors; 07-27-2005 at 01:40 AM.

  8. #8
    Banned
    Join Date
    Jun 2005
    Posts
    594
    yea i forgot to mention on this
    you use "=" to set somethign equal to something

    you use "==" for comparison purposes

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    it mess up the cin >> statment by going pass the statement
    if \n is on the buffer, cin.ignore() get it off the buffer so as not
    to affect the other cin >> statments.
    Post an example of this baffling behaviour.

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Where do you see cin.get() in the poster's code?

  11. #11
    Banned
    Join Date
    Jun 2005
    Posts
    594
    cin.get() just to keep it open to see the result
    had nothing to do with the code i posted.

    anyways the ignore let you handle
    say if you have char, and the user types


    abcde but char only holds one, all the character left on the
    buffer will go into the next statements,
    so use something like cin.ignore(80, '\n');

    to get rid of everything left on the buffer before the next cin >>
    statment as nto to mess it up.

    sorry if i was unclear by just saying newline, i ment up to the
    newline

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    cin.get() just to keep it open to see the result
    had nothing to do with the code i posted.

    anyways the ignore let you handle
    say if you have char, and the user types


    abcde but char only holds one, all the character left on the
    buffer will go into the next statements,
    so use something like cin.ignore(80, '\n');

    to get rid of everything left on the buffer before the next cin >>
    statment as nto to mess it up.

    sorry if i was unclear by just saying newline, i ment up to the
    newline
    The posted code is not reading in chars using cin>>, and the cin.ignore() that you recommended the poster add to his/her code is superfluous.
    Last edited by 7stud; 07-27-2005 at 01:42 AM.

  13. #13
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Code:
    cout << "\n\n[A]List Contacts";
    cout << "\n[B]Add Contact";
    cout << "\n[C]Edit Contact";
    cout << "\n[Q]MainMenu\n";
    i guess that would be my mistake that
    he was reading with a int, when accoding
    to these statments he seems to wants a character
    input.

  14. #14
    Registered User
    Join Date
    Jul 2005
    Posts
    11
    Thanks guys. Right now, it works as programmed. However, on the contact and notepad menus, I press "0" and it takes me back to the main screen but then it quits. This is expected since all my cin stuff is in my int main. When I try to rearrage the code, forexample: putting int answerget and cin >> answerget in startmenu(), I get a lot of different errors. Is there any good way to rearrange this? Or to use the main fuction again?

  15. #15
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Posting your new code would be the best thing to do at this point. My best guess about this:

    putting int answerget and cin >> answerget in startmenu(), I get a lot of different errors.

    is that you may be trying to use the value of answerget back in main() but you aren't using the correct syntax to transfer the value of answerget from startmenu() back to main().
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using "cin" in a thread function?
    By Fossil in forum Windows Programming
    Replies: 4
    Last Post: 11-24-2003, 09:08 PM
  2. improving input (replacing "cin")
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 12-07-2001, 05:55 PM