Why doesn't this program terminate properly? c++
Hello all,
I am teaching myself C++ with the help of Programming Principles and Practice Using C++ by Bjarnes Stroustrup but I am having trouble with a drill exercise. The step of the drill is to create a program in which you enter two numbers and the program states which is smaller and which is larger, or if they are equal. And it is supposed to terminate on entering the '|' character. The program works so far, except for when entering the '|' character, it infinitely repeats the last larger/smaller lines e.g. Code:
23 is the smaller number. Code:
#include <iostream> |
You've declared 'x' and 'y' to be of type double. '|' is a char.
If I change your while statement to: while(x != 0 && y != 0) for instance, it terminates appropriately. |
Thank you for pointing that out!
The exercise requires that I use '|' so I suppose I will have to find another way. |
You could use fgets to read a line, remove the terminating \n character, then compare the content with "|".
Then try sscanf (fmt "%lf %lf") to read two double values from the linlineffer - if it doesn't return 2, then warn the users. |
Quote:
I am only on the 4th Chapter of the book and have not learned about those. I am assuming I am supposed to find a way to accomplish the task only with what has been taught so far. But thank you for the suggestion! |
Quote:
EDIT: This is incorrect. It will only properly compare single character digits. Sorry, it's been a long day. |
Well, these functions/ways are considered to be 'old fashioned' by some C++ programmers, so you might not read about them in Stroupstrup's book.
A note: at this level it might be completely enough to exit when the reading of a number fails (see cin.fail()) |
Quote:
The directions for this part requires that I use 'double' Quote:
|
I remember doing a project similar to this one when I was learning C. You had to write a program that functioned like an accumulator. So, you would enter a number then an arithmetic symbol to operate on the total amount stored. The input was 'int' 'char'. Where 'char' was the arithmetic symbol or 'E' to exit.
|
I am not an expert in C++, but I gather that you could define a class which can contain either double or char, and define corresponding methods for input and comparison. I have never done that, but it is possible, right?
|
Just in case anyone is interested I think I'm making progress
Code:
#include <iostream> |
Quote:
Quote:
Considering the problem of doing it with doubles, using "|" as the only termination character is an interesting exercise, no doubt why it was included! Think about it this way: Your first task was to use integer types which presumably resulted in your code structure. The termination test was a natural fit in that case - you didn't have to think about it too much. Then they asked you to use doubles for the numeric types, which produces the apparent "conflict" with the termination character comparison - but only because you already wrote everything usiing integers! Now you have to think about how to accept and compare numeric input or character input, without knowing which it will be in advance. Stringstream comes to mind. Good luck! |
Quote:
try this, its in need of cleaning up, but... it works for me.. Code:
#include <iostream> you where taking in both, one after then other, and only need to check for the first one, first to see if that meets the requirements to stop the program, if no, then take in second data type, then proceed with what ever you need to do. your 'if statements too' it is process of elimination logic Code:
Code:
if |
Quote:
Code:
#include <cerrno> |
All times are GMT -5. The time now is 01:28 AM. |