Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Stop output with if and else statements

Posted on 2006-07-11
7
Medium Priority
?
263 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:fsadewat
  • 4
  • 2
7 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17079551
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
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17079552
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
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17079564
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:fsadewat
ID: 17079718
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
 
LVL 8

Accepted Solution

by:
manish_regmi earned 2000 total points
ID: 17079757
yes,
but dont forget to enclose them in braces.

if()
{
printf()
continue;
}

regards
Manish Regmi
0
 

Author Comment

by:fsadewat
ID: 17079819
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
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17079839
Any good C programming would give that info.
I like K&R but it can quite tough for beginners.

regards
Manish Regmi
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question