Thread: Help (Beginner)

  1. #1
    NewbieC
    Guest

    Question Help (Beginner)

    I need to make a menu in a for or while loop that redisplays the menu after each selection and updates the variable each time. With a switch structure to control the program options.The options of the menu are 1 increment the variable 2 decrement the variable and 3 exit. If someone could help me I would really appreciate it.

  2. #2
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    uhh, wheee

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int i = 0, choice;
    	bool exit = false;
    
    	while(!exit)
    	{
    		cout << "i = " << i;
    		cout << "\n1. increment\n2. decrement\n3. exit\n\n";
    
    		cout << "Choose: ";
    		cin >> choice;
    
    		switch(choice)
    		{
    		case 1:
    			i ++;
    			break;
    		case 2:
    			i --;
    			break;
    		case 3:
    			exit = true;
    			break;
    		}
    
    		cout << endl;
    	}
    
    	return 0;
    }
    Last edited by abrege; 01-28-2003 at 10:05 PM.
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  3. #3
    NewbieC
    Guest

    Smile Thanks, but stillneed more help

    This is what i have so far
    Code:
    #include <iostream.h>
    int main ()
    {
    int i;
    char input;
    
    while (1)
    	{
    cout <<"\n\n";
    cout << "		**** Increment / Decrement ****\n\n";
    cout << "		The present value of the internal variable is:"<< i <<endl;endl;
    cout << "		(I)ncrement the Internal Variable.\n";
    cout << "		(D)ecrement the Internal Variable.\n";
    cout << "		(E)xit the program.\n\n";
    cout << "	Select==>";
    cin >> input;
    
    switch (input)
    		{
    	 
    case 'I':
    case 'i':
    		i ++;
    		break;
    
    case 'D':
    case 'd':
    		i --;
    		break;
    		}
    if (input == 'E' || input == 'e'){break;}
    	}
    }
    What do i need to do to fix it. The variable is showing some wierd numbers and its not updating. Its also supposed to start off at 10.

  4. #4
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    You need to initialize "i" before you can use it properly

    Code:
    #include <iostream.h>
    
    int main ()
    {
    	int i = 10;
    	char input;
    
    	while (1)
    	{
    		cout <<"\n\n";
    		cout << "		**** Increment / Decrement ****\n\n";
    		cout << "		The present value of the internal variable is:"<< i <<endl << endl;
    		cout << "		(I)ncrement the Internal Variable.\n";
    		cout << "		(D)ecrement the Internal Variable.\n";
    		cout << "		(E)xit the program.\n\n";
    		cout << "	Select==>";
    		cin >> input;
    
    		switch (input)
    		{
    			 
    		case 'I':
    		case 'i':
    				i ++;
    				break;
    
    		case 'D':
    		case 'd':
    				i --;
    				break;
    		}
    
    		if (input == 'E' || input == 'e')
    			break;
    	}
    
    	return 0;
    }
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  5. #5
    NewbieC
    Guest

    Talking Thanks!!

    Dude thanks a lot!!!. Now it says to rewrite it with nested loops so it doesnt redraw unless "i" or "d" is chosen and i need to exit when "e" is chosen. You'd be the coolest ever if you could show me how to do this.

  6. #6
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Hmm, that is coming a bit unclear to me, perhaps something like this?

    Code:
    #include <iostream.h>
    
    int main ()
    {
    	int i = 10;
    	char input;
    	bool exit = false;
    
    	while (!exit)
    	{
    		cout <<"\n\n";
    		cout << "		**** Increment / Decrement ****\n\n";
    		cout << "		The present value of the internal variable is:"<< i <<endl << endl;
    		cout << "		(I)ncrement the Internal Variable.\n";
    		cout << "		(D)ecrement the Internal Variable.\n";
    		cout << "		(E)xit the program.\n\n";
    
    		do
    		{
    			cout << "	Select==>";
    			cin >> input;
    
    			switch (input)
    			{
    				 
    			case 'I':
    			case 'i':
    					i ++;
    					break;
    
    			case 'D':
    			case 'd':
    					i --;
    					break;
    			case 'e':
    					exit = true;
    					break;
    			}
    		}
    		while(input != 'i' && input != 'd' && input != 'e');
    	}
    
    	return 0;
    }
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  7. #7
    NewbieC
    Guest

    Thumbs up Perfect

    Thanks so much abrege!!! You probably saved me from failing my programming class.

  8. #8
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    No problem
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  9. #9
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    What is break all about?

  10. #10
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    In a switch statement, program execution begins at the first case that is successful. Program execution will then continue through the switch statement. break; causes the remaining switch cases to be skipped.

    In the example, you can see he has a case for 'i' and 'I', no break; so either will run the block. If he had not put the break at the end of the 'I' block, the program would then have processed the 'd' block and so on.

    There are other uses for break; but that is what it is doing there.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  2. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 02:02 PM
  3. Best Free Beginner Online Books/Tutorials?
    By Zeusbwr in forum C++ Programming
    Replies: 2
    Last Post: 10-12-2004, 05:52 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM