What loop should I use? While? Do While?

This is a discussion on What loop should I use? While? Do While? within the C Programming forums, part of the General Programming Boards category; Hey I am creating a basic currency converter. I have a simple menu but the problem is there is no ...

  1. #1
    kie
    kie is offline
    Registered User
    Join Date
    Oct 2006
    Posts
    14

    Smile What loop should I use? While? Do While?

    Hey

    I am creating a basic currency converter. I have a simple menu but the problem is there is no error control. Here is the code...

    Code:
    void menu(void)
    {
    int menu;
    int valid = 0;
    
       printf("	 *****Menu*****\n\n1. Pounds to Euro\n2. Pounds to USD\n3. Pounds to Chinese Yen\n4. Pounds to Australian dollar\n5. Pounds to Canadian dollar\n\n");
    	cin >> menu; //this  won't work with scanf for some reason
    
    
           switch(menu)
    		{
    			case 1:
    				rate = 1.48018;
                strcpy(convertto, "euros");
                valid = 1;
    				break;
    			case 2:
    				rate = 1.94849;
                strcpy(convertto, "USD");
                valid = 1;
    				break;
    		  	case 3:
    				rate = 15.288;
                strcpy(convertto, "chinese yen");
                valid = 1;
                break;
    			case 4:
    				rate = 2.4932;
                strcpy(convertto, "australian dollars");
                valid = 1;
                break;
    			case 5:
    				rate = 2.2014;
                strcpy(convertto, "canadian dollars");
                valid = 1;
                break;
            }
    }
    Basically, I was the menu to show the first time the App is executed. Then if the user enters a selection higher than 5, they get an error. The menu is shown again ready for them try again.

    Any help would be appreciated.

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Then you want a do-while loop.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    kie
    kie is offline
    Registered User
    Join Date
    Oct 2006
    Posts
    14
    like this?

    Code:
    do{
       printf("	 *****Menu*****\n\n1. Pounds to Euro\n2. Pounds to USD\n3. Pounds to Chinese Yen\n4. Pounds to Australian dollar\n5. Pounds to Canadian dollar\n\n");
    	cin >> menu; //this  won't work with scanf for some reason
    
    
           switch(menu)
    		{
    			case 1:
    				rate = 1.48018;
                strcpy(convertto, "euros");
                valid = 1;
    				break;
    			case 2:
    				rate = 1.94849;
                strcpy(convertto, "USD");
                valid = 1;
    				break;
    		  	case 3:
    				rate = 15.288;
                strcpy(convertto, "chinese yen");
                valid = 1;
                break;
    			case 4:
    				rate = 2.4932;
                strcpy(convertto, "australian dollars");
                valid = 1;
                break;
    			case 5:
    				rate = 2.2014;
                strcpy(convertto, "canadian dollars");
                valid = 1;
                break;
                }
            }while (valid=0);
    I have tried this but it doesn't seem to work

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > (valid=0)
    Use ==

    Also, post a small and complete program which "doesn't work".
    There's all sorts of input reasons which can break your code, along with mis-declared variables, broken code logic and the like.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Try to avoid using void main(), use int main().
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Actually, it's void menu () Whoops.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    cin is C++ not C
    this won't work with scanf for some reason
    maybe you forgot to give pointer to variable

    scanf("%d", &menu);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 11:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 03:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 01:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM

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