Simple Program will not exit gracefully

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2005
Posts: 3
Reputation: Starz20 is an unknown quantity at this point 
Solved Threads: 0
Starz20 Starz20 is offline Offline
Newbie Poster

Simple Program will not exit gracefully

 
0
  #1
Apr 6th, 2005
Hi, everyone. You may have probably heard this a hundred times, but I am new to programming and I am taking an intro to C++ class. I have a program that I have written, but I can't understand why it will not exit gracefully.
This is what I have written, but if I type in "E" to exit, it loops to enter in a number again and doesn't exit.
printf("\nEnter one positive or negative number (1, 2, 3. . . or E to exit): ");



  1. /*multiple lines comment - Program to display square and square root for a given number*/
  2. #include <math.h>/*for sqrt( ) function*/
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. int main(void)
  6. {
  7. /*variable and data type*/
  8. float x;
  9.  
  10. /*standard output*/
  11. printf("Shannon says programming is hard!\n");
  12. printf("\nEnter one positive or negative number (1, 2, 3. . . or E to exit): ");
  13. /* Prompt for input again */ printf(">> ");
  14. /*standard input*/
  15. scanf("%f", &x);
  16. printf("\nx = %f ", x);
  17. printf("\nSquare for x is = %f", x * x);
  18. //sqrt() is the predefined function, defined in math.h
  19. printf("\nSquare root for x is = %f\n", sqrt(x));//print results;
  20. return 0;//finished
  21. }

Any help would be greatly appreciated!


<< moderator edit: added [code][/code] tags >>
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Simple Program will not exit gracefully

 
0
  #2
Apr 6th, 2005
Originally Posted by Starz20
This is what I have written, but if I type in "E" to exit, it loops to enter in a number again and doesn't exit.
I don't see the loop, but "E" is not a valid float number. If you want the possibility of the user input to be either text or a number, read the input as a string. Then, if the string is not "E", (attempt to) convert the input string into a float.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 3
Reputation: Starz20 is an unknown quantity at this point 
Solved Threads: 0
Starz20 Starz20 is offline Offline
Newbie Poster

Re: Simple Program will not exit gracefully

 
0
  #3
Apr 9th, 2005
Thank you, Dave. I am still learning!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC