943,946 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2716
  • C++ RSS
Apr 6th, 2005
0

Simple Program will not exit gracefully

Expand Post »
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): ");



C++ Syntax (Toggle Plain Text)
  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 >>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Starz20 is offline Offline
3 posts
since Apr 2005
Apr 6th, 2005
0

Re: Simple Program will not exit gracefully

Quote 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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Apr 9th, 2005
0

Re: Simple Program will not exit gracefully

Thank you, Dave. I am still learning!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Starz20 is offline Offline
3 posts
since Apr 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: what is the error
Next Thread in C++ Forum Timeline: Strings and For Loops





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC