Thread: multiple instances of cin

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    multiple instances of cin

    Heya everyone!
    I was curious about something...I've created a while loop that handles user input for two numbers...then inside the loop I have another instance where the user is entering a number to enter a switch statement...interestingly, when the user enters the number for the switch, the switch is never executed and in fact the prog makes it so you haveta enter the variable for the switch three times and when you do that...it skips the whole loop body and starts from the beginning of the loop...
    Now I was wondering if it's the fact that I have another instance of cin within the while loop that's conflicting with the calls to cin in the while test expression? Any insight into this interesting predicament would help me a lot...if it helps any here's a bit of my while loop:
    Code:
    while (cin >> first_num >> sec_num)
        {
            cout << "Would you like to multiply or add the two numbers?\n"
                 << "Enter 1 to multiply,"
                 << "enter 2 to add: ";
            int choice;
            cin  >> choice;
            switch(choice)
    Well...any help would be really cool, thanks a lot everyone-Chap

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    You'll have to give more information than that. I took your exact code and placed in this program and it worked fine. What is your full program? What inputs are you giving it?
    Code:
    #include <iostream>
    
    int main()
    {
        using std::cin;
        using std::cout;
        using std::endl;
    
        int first_num;
        int sec_num;
        while (cin >> first_num >> sec_num)
        {
            cout << "Would you like to multiply or add the two numbers?\n"
                 << "Enter 1 to multiply,"
                 << "enter 2 to add: ";
            int choice;
            cin  >> choice;
            switch(choice)
            {
            case 1:
                cout << "Result: " << first_num*sec_num << endl;
                break;
            case 2:
                cout << "Result: " << first_num+sec_num << endl;
                break;
            default:
                cout << "Bad choice." << endl;
                break;
            }
        }
    }

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    Here's my full switch statement...maybe I'm doing something wrong:
    Code:
    switch(choice)
            {
                case 1:
                    answer = calculate(first_num, sec_num, mult);
                    break;
                case 2:
                    answer = calculate(first_num, sec_num, add);
                    break;
                default :
                    "Wrong # entered, you're a fool.\n";
                    continue;
            }
            cout << "The two numbers you entered"
                 << " have come out to: " << answer;
            cout << "\n\nEnter two more numbers or q to quit: ";
    is it legal what i did with the function call? -Chap

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    by the way...I know I could more easily accomplish what I want with an if else...I was just refreshing my switch statements...been a while since I used one

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    what the hell...this is SO weird, I added a single cout statement to my first case in the switch and now it works fine...hmmm, and now I took the cout statement and it's working fine now...well,jlou, thanks a lot for the help I think the problem was in my switch statement which you helped me realize...for that I give you a reputation boost :-P Thanks a lot for the help!-Chap

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple instances and variables
    By FOOTOO in forum Windows Programming
    Replies: 1
    Last Post: 04-07-2005, 10:54 AM
  2. Multiple instances of the same object.
    By suzakugaiden in forum Game Programming
    Replies: 12
    Last Post: 03-19-2005, 11:49 PM
  3. Multiple instances of form resource
    By knutso in forum Windows Programming
    Replies: 4
    Last Post: 05-25-2004, 03:47 PM
  4. Multiple variable cin line
    By Dark Nemesis in forum C++ Programming
    Replies: 3
    Last Post: 08-02-2003, 10:57 PM
  5. Getting multiple inputs using cin
    By edk in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2001, 02:34 PM