Solved

Stop output with if and else statements

Posted on 2006-07-11
7
253 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

705 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

17 Experts available now in Live!

Get 1:1 Help Now