New to programming, messed up program

This is a discussion on New to programming, messed up program within the C++ Programming forums, part of the General Programming Boards category; Hey, I'm new to programming, I was following those tutorials and built upon that this: Code: #include <iostream> using namespace ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    3

    New to programming, messed up program

    Hey, I'm new to programming, I was following those tutorials and built upon that this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int input;
    	
    	cout<<"1. Message number one.\n";
    	cout<<"2. Message number two.\n";
    	cout<<"3. Message number three.\n";
    	cout<<"4. Exit.\n";
    	cin>> input;
    	do {
    		switch ( input ) {
    		case 1:
    			cout<<"Meeting on Monday.\n";
    			break;
    		case 2:
    			cout<<"Tuesday is open.\n";
    			break;
    		case 3:
    			cout<<"Wednesday is full.\n";
    			break;
    		case 4:
    			cout<<"Check back for messages later! Goodbye!\n";
    			break;
    		default:
    			cout<<"Sorry, invalid input, terminating!";
    			break;
    		}
    	} while ( input > 4 );
    cin.get();
    }
    It does nothing useful in particular, but when I enter something above 4 it loops like its supposed to but it continues to select 4 at the menu, can I reset the integer input somehow?

    Thanks.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    aye...put the cin statement inside your do loop.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    3
    Thanks.

  4. #4
    ICU
    ICU is offline
    Registered User
    Join Date
    Jun 2005
    Posts
    1
    I'd get rid of that cin.get() function, and put the cin >> input; statement within the loop if you want it to reset to user input each time.

    If I've understood what it is you're trying to do, I'd go about it this way:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int input;
    	
    	cout<<"1. Message number one.\n";
    	cout<<"2. Message number two.\n";
    	cout<<"3. Message number three.\n";
    	cout<<"4. Exit.\n\n";
        do
        {
        cout << "Enter your choice: ";
    	cin>> input;
    
    		switch ( input ) {
    		case 1:
    			cout<<"Meeting on Monday.\n";
    			break;
    		case 2:
    			cout<<"Tuesday is open.\n";
    			break;
    		case 3:
    			cout<<"Wednesday is full.\n";
    			break;
    		case 4:
    			cout<<"Check back for messages later! Goodbye!\n";
    			break;
    		default:
    			cout<<"Sorry, invalid input, terminating!";
    			break;
    		}
    
    	} while(input < 4 && input > 0);
    
    }
    Last edited by ICU; 06-25-2005 at 02:46 PM.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    3
    Thank you, that technique is much cleaner!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. Replies: 2
    Last Post: 05-10-2002, 05:16 PM

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