Solved

Stop output with if and else statements

Posted on 2006-07-11
7
254 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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 500 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem with MFCApp 78 347
mixing C++ and C code elegantly 10 153
C hashtable library 3 83
Grammars for C C++ and java 1 114
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now