• C

Stop output with if and else statements

How do you stop the output of print statements if they do not meet requirements of the if statement. The error message does work fine but the output of the program still is displayed before the print statement in the else statement. I want to stop the output of the program with out stoping the menu to reload. Below is the example program:

 
#include <stdio.h>  



int main()        
                     

//Declare main function variables and initalize as needed

 /*beginning functionality*/ //Display five foreign currencies with menu //

{      
      float AUD=0.73399f;       /* Austrialia Dollar */
      float GBP=1.82367f;       /* United Kingdom Pound*/
      float CAD=0.89206f;      /* Canadian Dollar */
      float EUR=1.25890f;      /* European Euro */
      float MXN=0.08777f;      /* Mexican Pesos */
      float US=1.00f;            /* United States Dollar */
      float CON=0.0f;           /* Conversion */
      char ch;
      while (ch!='Q')
     {
   
     /*display program title */
     
        printf("\n\nU.S. Dollars Currency Conversion Menu.\n");   /* print display */
        printf("--------------------------------------\n");       /* print display */
        printf("A. Austrialia \n");                               /* print display */
        printf("B. United Kindom Pound \n");                      /* print display */
        printf("C. Canadian Dollar \n");                          /* print display */
        printf("D. European Euro \n");                            /* print display */
        printf("E. Mexican Pesos \n");                            /* print display */
        printf("Q. Quit\n");                                      /* print display */
        fflush( stdin );
        printf("\n\nEnter A,B,C,D,E or Q: ");                     /* print display */
        ch=toupper(getchar());
          switch(ch)
        {
            case 'A':
            case 'B':
            case 'C':
            case 'D':
            case 'E':
               
               
                printf("--------------\n\n");                     /* print display */
                printf("\nPlease enter a US Dollar Amount: $ ");  /* Enter Dollar Amount */
              scanf("%f",&CON);                                 /* print display */
                printf("\n%.2f U.S. Dollars equal ",CON);         /* print display */
                switch(ch)
               
               /*calculate the selected currency*/
               
                {
                case 'A':
                    printf("%.2f Austrialia \n\n",CON*AUD);
                    break;
                case 'B':
                    printf("%.2f United Kindom Pound\n\n",CON*GBP);
                    break;
                case 'C':
                    printf("%.2f Canadian Dollar\n\n",CON*CAD);
                    break;
                case 'D':
                    printf("%.2f European Euro\n\n",CON*EUR);
                    break;
                case 'E':
                    printf("%.2f Mexican Pesos\n\n",CON*MXN);
                    break;
                 }
                   
            case 'Q':
                break;
                    default:    
                  printf("--------------\n\n");                                        /* print display */
                printf("\n\nEnter in A,B,C,D,E of the currency you would like\n");     /* print display */
                printf("to convert from U.S. Dollars or enter Q to Quit\n\n");         /* print display */
               
       }

          if ((CON) >=0.0f && CON <=0.0f)     /* error check for characters and negative numbers */  
                printf("\nSelect Again");                                          /* print display */
          else
                printf("\n Error! You entered a negative number or character.\n");       /* print display */  
            
     }
   
return 0;
}
//end of program
fsadewatAsked:
Who is Participating?
 
manish_regmiCommented:
yes,
but dont forget to enclose them in braces.

if()
{
printf()
continue;
}

regards
Manish Regmi
0
 
sunnycoderCommented:
I guess this is the cause of your misery

          if ((CON) >=0.0f && CON <=0.0f)     /* error check for characters and negative numbers */  
              printf("\nSelect Again");                                          /* print display */
          else
               printf("\n Error! You entered a negative number or character.\n");       /* print display */  

Your if statement says (if CON >0 AND CON<0)

needless to say, it will always be false and you would always end up executing the else part ...
if (CON < 0.0f) should suffice for checking negative numbers ...

A few observations are in order here
1. You do not require two switch constructs ... look carefully, you can remove the outer switch

2. floating point comparisons are not exact ... > would work fine but dont depend on equality

3. For robust input validation, you need to read it in as a string and then validate and convert it to float

cheers!
sunnycoder
0
 
manish_regmiCommented:
Did you mean this?
....
          printf("\nPlease enter a US Dollar Amount: $ ");  /* Enter Dollar Amount */
          scanf("%f",&CON);                                 /* print display */
        if ((CON) >=0.0f && CON <=0.0f)     /* error check for characters and negative numbers */  
                continue;
    printf("\n%.2f U.S. Dollars equal ",CON);         /* print display */
..............................

regards
Manish Regmi
0
Network Scalability - Handle Complex Environments

Monitor your entire network from a single platform. Free 30 Day Trial Now!

 
manish_regmiCommented:
you can also do

do{
printf("\nPlease enter a US Dollar Amount: $ ");  /* Enter Dollar Amount */
  scanf("%f",&CON);    
}while(CON < 0.0f)

regards
Manish
0
 
fsadewatAuthor Commented:
Manish, can a print statement be made before continue command so there is a message that says printf("\n Error! You entered a negative number or character.\n");  before returning to the main menu?
0
 
fsadewatAuthor Commented:
Manish thank so much for the help I have been spending so much time trying to figure this problem out. There is nothing in my books gives the continue statement. Do you know any good references on commands that go with looping statements while if and else? Again thank you!
0
 
manish_regmiCommented:
Any good C programming would give that info.
I like K&R but it can quite tough for beginners.

regards
Manish Regmi
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.