Solved

Stop output with if and else statements

Posted on 2006-07-11
7
258 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers 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.

789 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