Help please

This is a discussion on Help please within the C Programming forums, part of the General Programming Boards category; Hello, Need help please for a school project. I wrote a program for currency converter , everything works except I ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    5

    Help please

    Hello,
    Need help please for a school project.
    I wrote a program for currency converter , everything works except I cannot go back to the same question after validating the userís input, instead of just exiting

    Hereís the code if you can help me,

    http://code.doria.se/code.aspx?c=e76...f-2574860bd851


    any help will be appreciated

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by huslayer View Post
    Hello,
    Need help please for a school project.
    I wrote a program for currency converter , everything works except I cannot go back to the same question after validating the userís input, instead of just exiting

    Hereís the code if you can help me,

    http://code.doria.se/code.aspx?c=e76...f-2574860bd851


    any help will be appreciated
    EZ as pie, Hussein!

    Put a
    Code:
    quit = 0;
    Do {
    
    //All your menu header and menu choices go in here
    
    //add a choice to quit the program.
    
    }while(quit == 0);
    loop, all around your menu choices in main(). With every choice, the program can go to a specific function, (preferrably), or to the rest of main(), and then return to see the main menu choices, again.

    When they select the number for the Quit selection, they leave the do while loop.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    5
    Adak, Thank you so much..
    I tried to do what you told me but i got syntax error!!

    Iím reading about the loop function but still can't get it ! As also Iíll need to do it for each of the 5 parts in the code, for each currency that Iíll convert

    Thank anyway for your help, and any clarifications will be appreciated

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    Quote Originally Posted by huslayer View Post
    Adak, Thank you so much..
    I tried to do what you told me but i got syntax error!!

    I’m reading about the loop function but still can't get it ! As also I’ll need to do it for each of the 5 parts in the code, for each currency that I’ll convert

    Thank anyway for your help, and any clarifications will be appreciated
    Code:
    quit = 0;
    Do {
    
    //All your menu header and menu choices go in here
    
    //add a choice to quit the program.
    
    }while(quit == 0);
    1. Maybe the reason it's having a syntax error is because you followed the syntax above. Maybe adak mistyped the "Do" syntax that supposed to be "do".

    Maybe you could try to sample code.
    Code:
        do
        {
            printf( "1. Convert 1 \n" );
            printf( "2. Convert 2 \n" );
            printf( "3. Convert 3 \n" );
            printf( "0 Quit" );
            scanf( "%d", &input );fflush( stdin );
        }while( input != 0 );
    since all the processes are in your main function only then you have to house your statement using the do...while or while... looping statement.
    Last edited by $l4xklynx; 01-23-2009 at 12:59 AM.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    fflush( stdin );

    undefined - read FAQ
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yeah, no Do, just do.

    After each call to scanf(), you will need to get rid of the newline char that scanf() leaves behind.

    You can use getchar(), as in:

    Code:
    scanf()  //any call to scanf() function.
    gar = getchar(); //this pulls off one newline char scanf leaves behind
    where "gar" is any char or integer variable, which can be used for "garbage". You still have
    to declare "gar" (or whatever name you use), as a variable in your list of variables.

    If you fail to use getchar() after each scanf() call, you will have scanf() being skipped over because it will read the newline left behind in the keyboard buffer, and will take that newline as your input, and just keep going.

    You'll swear it had "skipped" over your scanf(), but it didn't.

    You don't need to put a loop around each part of the five currencies - just the main menu portion. The other parts should be in one or more separate functions, that you'll call (and return from), in main().

    Code:
    In pseudo code:
    
    int main()
       Declare your variables here
    
    Start your do loop here
    
      do  
       {
       Your menu header here
    
       Your menu body here
    
       Get user choice here with scanf()
    
       gar = getchar() here
    
       Switch statement for user's choice, here
          which calls all other function(s).
    
       Functions return here
       
       }while loop ends here
    If you have a problem with it, you'll have to tell me what compiler errors you are getting - not just "I'm getting
    compiler errors", ok?

    Also, I need you to post the code that you're having trouble with. I won't know what changes you've made, otherwise.

    It's all about the details, so show em.
    Last edited by Adak; 01-23-2009 at 01:29 AM.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    Quote Originally Posted by vart View Post
    fflush( stdin );

    undefined - read FAQ
    I understand, but it would confuse him if we're not going to focus on the issue.. ^_^

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    5
    Quote Originally Posted by $l4xklynx View Post
    I understand, but it would confuse him if we're not going to focus on the issue.. ^_^

    Am confused already and lost my mind !!

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    5
    Finally i got it to validate option #1, but the rest of the options doesn't work anymore !! lol

    code is here:

    http://code.doria.se/code.aspx?c=83f...a-b50f773ac21e

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    //this line is a problem:
    while( USD <1 || USD > 100000000000000000 );
    
    Because your data type won't come close to handling 1 * 10^17
    
    Every part that you have separated as a choice for conversion, should be another
    function:
    
    //###################Euros is selected#################
    
    //What to LOOOOOP
    
    do {
    
        //IF condition
    
        if (currency_selection == 1){
    
        printf("\n\t------------------------------------------------------------\n");
    
        printf("\n\tThanks! USD to Euros is selected (Please Enter Numbers Only)\n");
    
        printf("\n");
    
        printf("\n\tAmount of USD to convert: ");
    
        }
    
        //User Input
    
        scanf("%f", &USD);  fflush( stdin );
    
    }while( USD <1 || USD > 100000000000000000 );
    
     
        //Result if success
    
        printf("\n\tThe total is: %f Euros\n", USD*EUR);
    
        printf("\n");
    
        return 0;
    
     
    
    //###############British Pounds is selected###############
    
    
     if (currency_selection == 2){
    
        printf("\n\tThanks! USD to British Pounds is selected\n");
    
        printf("\n\tPlease enter the amount of USD you need to convert: ");
    
        scanf("%f", &USD);
     }
    
        //Validation of dollar amount
    
        if ( USD <1 || USD > 100000000000000000)
    
        printf("\nPlease enter a valid number");
    
        else
    
        printf("\n\tThe total is: %f British Pounds\n", USD*GBP);
    
        printf("\n");
    
            return 0;
    
     
    //##############Canadian Dollars is selected########
    
        if (currency_selection == 3){
    
        printf("\n\tThanks! USD to Canadian Dollars is selected\n");
    
        printf("\n\tPlease enter the amount of USD you need to convert: ");
    
        scanf("%f", &USD);
    
        //Validation of dollar amount
    
        if ( USD <1 || USD > 100000000000000000)
    
        printf("\nPlease enter a valid number");
    
        else
        printf("\n\tThe total is: %f Canadian Dollars\n", USD*CAD);
    
        printf("\n");
    
        }
    
     
    
    //#########Japanese Yens is selected#########
    
        if (currency_selection == 4){
    
        printf("\n\tThanks! USD to Japanese Yens is selected\n");
    
        printf("\n\tPlease enter the amount of USD you need to convert: ");
    
        scanf("%f", &USD);
    
        //Validation of dollar amount
    
        if ( USD <1 || USD > 100000000000000000)
    
        printf("\nPlease enter a valid number");
    
        else
    
        printf("\n\tThe total is: %f Japanese Yens\n", USD*JPY);
    
        printf("\n");
    
        }
    
    
    //###Australian Dollars is selected##############
        if (currency_selection == 5){
    
        printf("\n\tThanks! USD to Australian Dollars is selected\n");
    
        printf("\n\tPlease enter the amount of USD you need to convert: ");
    
        scanf("%f", &USD);
    
    
        //Validation of dollar amount
    
        if ( USD <1 || USD > 100000000000000000)
    
        printf("\nPlease enter a valid number");
    
        else
    
     
    
        printf("\n\tThe total is: %f Australian Dollars\n", USD*AUD);
    
        printf("\n");
    
        }
    
    //#######################End######
        return 0;
    
    }
    
    Then, in your main menu, you just have:
    
    if(currency_selection == 1)
       usdollar(amount);
    else if(currency_selection == 2)
       gbpound(amount);
    else if(currency_selection == 3)
       japanyen(amount);
    
    //etc.
    Every different colored part of code, should be a different function. Make it happen. You'll be surprised how easy it is, and how much it helps, with just some practice and study.

    If you have questions, we're right here.

    When that is done, the code will automatically return to the menu when it is done.
    Last edited by Adak; 01-23-2009 at 09:18 PM.

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    5
    Adak, thanks so much for your help

    i'm using this line "while( USD <1 || USD > 100000000000000000 );"
    just to validate the input is a number not a character
    i really don't know another way to validate

    but i'll try to do what you've advised me to do
    Thanks again

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    Quote Originally Posted by huslayer View Post
    Adak, thanks so much for your help

    i'm using this line "while( USD <1 || USD > 100000000000000000 );"
    just to validate the input is a number not a character
    i really don't know another way to validate

    but i'll try to do what you've advised me to do
    Thanks again

    Let's assume that you don't need to implement a function just yet for some reason. The blue codes that you can see there are the things that I just added in your program. Try to test it if it satisfies your need.

    **note: I replaced the variable declaration, since I'm not at home and I'm using an old compiler-the compiler is complaining about the variable declaration.

    Code:
     /*Hussein yousef - University of phoenix - 2009
    CSS 561 Class
    Conversion Program
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        //Constants Declarations
        const float USD=0;
        const float EUR=0.7747;
        const float GBP=0.7182;
        const float CAD=1.2656;
        const float JPY=89.82;
        const float AUD=1.5381;
    
        int currency_selection = 0;
    
        do
        {
    	  //Decoration
    	  printf("\n");
    	  printf("   *************************************************************************\n");
    	  printf("   *                        University Of Phoenix Online                   *\n");
    	  printf("   *                                                                       *\n");
    	  printf("   *                            Hussein Yousef                             *\n");
    	  printf("   *                                                                       *\n");
    	  printf("   *                          COURSE NUMBER: CSS 561                       *\n");
    	  printf("   *                                                                       *\n");
    	  printf("   *************************************************************************\n");
    	  printf("\n");
    	  printf("                               ..:CONVERSIONS:..                            \n");
    	  printf("\n");
    
    	  //Menu
    	  printf("                            ..:Currency Selection:..                         \n");
    	  printf("\n\t1-Convert USD to Euros\n");
    	  printf("\n\t2-Convert USD to British Pounds\n");
    	  printf("\n\t3-Convert USD to Canadian Dollars\n");
    	  printf("\n\t4-Convert USD to Japanese Yens\n");
    	  printf("\n\t5-Convert USD to Australian Dollars\n");
    	  printf("\n\t6-Quit\n" );
    	  printf("\n\tPlease select a choice <1-5>: ");
    
    	  //User Input
    	  scanf("%d", &currency_selection); fflush( stdin );
    	  //Validation
    	  if ( currency_selection  > 6 || currency_selection < 1 )
    	  printf("\nPlease Enter A Valid Number between 1 & 5\n");
    
    	  //###########################################################
    	  //Euro Selected
    
    	  if (currency_selection == 1){
    	  printf("\n\tThanks! USD to Euros is selected\n");
    	  printf("\n\tPlease enter the amount of USD you need to convert: ");
    	  scanf("%f", &USD);  fflush( stdin );
    	  //Validation of dollar amount
    
    	  if ( USD <1 || USD > 100000000000000000)
    	  printf("\nPlease enter a valid number");
    
    	  else
    	      //Output
    	      printf("\n\tThe total is: %f Euros\n", USD*EUR);
    	      printf("\n");
    	      //return 0;
    
    	 }
    
    	 //###########################################################
    
    	 if (currency_selection == 2){
    	 printf("\n\tThanks! USD to British Pounds is selected\n");
    	 printf("\n\tPlease enter the amount of USD you need to convert: ");
    	 scanf("%f", &USD);  fflush( stdin );
    
    	 //Validation of dollar amount
    	 if ( USD <1 || USD > 100000000000000000)
    	    printf("\nPlease enter a valid number");
    	 else
    	    printf("\n\tThe total is: %f British Pounds\n", USD*GBP);
    	    printf("\n");
    
    	//return 0;
    
    	}
    	//###########################################################
    	if (currency_selection == 3){
    	    printf("\n\tThanks! USD to Canadian Dollars is selected\n");
    	    printf("\n\tPlease enter the amount of USD you need to convert: ");
    	    scanf("%f", &USD); fflush( stdin );
    	    //Validation of dollar amount
           if ( USD <1 || USD > 100000000000000000)
    	  printf("\nPlease enter a valid number");
           else
    	   printf("\n\tThe total is: %f Canadian Dollars\n", USD*CAD);
    	   printf("\n");
           }  
    
           //###########################################################
           if (currency_selection == 4){
              printf("\n\tThanks! USD to Japanese Yens is selected\n");
              printf("\n\tPlease enter the amount of USD you need to convert: ");
              scanf("%f", &USD); fflush( stdin );
           //Validation of dollar amount
           if ( USD <1 || USD > 100000000000000000)
              printf("\nPlease enter a valid number");
    
           else
              printf("\n\tThe total is: %f Japanese Yens\n", USD*JPY);
              printf("\n");
           }
    
           //###########################################################
    
           if (currency_selection == 5){
              printf("\n\tThanks! USD to Australian Dollars is selected\n");
              printf("\n\tPlease enter the amount of USD you need to convert: ");
              scanf("%f", &USD); fflush( stdin );
              //Validation of dollar amount
          if ( USD <1 || USD > 100000000000000000)
              printf("\nPlease enter a valid number");
          else
              printf("\n\tThe total is: %f Australian Dollars\n", USD*AUD);
              printf("\n");
          }
    
          //###########################################################
       }while( currency_selection != 6 ); //program will not quit until 6 is pressed
       
       return 0;
    }
    Last edited by $l4xklynx; 01-24-2009 at 02:58 AM.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    Eh, $l4xklynx, fflush(stdin) results in undefined behaviour.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    Quote Originally Posted by laserlight View Post
    Eh, $l4xklynx, fflush(stdin) results in undefined behaviour.
    If the looping problem fixed huslayer's problem then we'll go ahead and advice him not to use scanf to avoid using fflush.

  15. #15
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    Quote Originally Posted by $l4xklynx View Post
    If the looping problem fixed huslayer's problem then we'll go ahead and advice him not to use scanf to avoid using fflush.
    scanf is standard, fflush(stdin) - not

    using one function does not require using the other
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

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