Error message looping/ doesn't react properly to correct value

Hello,
First off I am VERY NEW to C++ as well as this forum so please forgive any mistakes or missteps in procedure or format.
I am working on an assignment and part of the assignment is to create an error read if an inappropriate user value is entered. I believe my user value is properly set. (to the values I chose). However I am having trouble working out the code to properly loop it back and recognize when a correct value is entered in order to complete the program.

Below is my entire code.

...yes I realize there is some funny stuff in it... I was experimenting as well.

The assignment requirements are as follows (so this is my end goal)and it is the first point I am having issue with;

• Read integer Fahrenheit temperatures from the user. You need to check whether the input is the correct one or not. If the user enters the incorrect number, ask it again.
• Use the formula: Celsius = (Fahrenheit – 32) * 5.0 / 9.0
• The output Celsius should be a floating point with two digits of precision.
• The Celsius temperatures should be displayed with a sign of positive or negative.
• The program should ask the user to continue or not. If the user wants to do the conversion again, use repetitive statements such as DO WHILE, FOR, or IF THEN ELSE to do the conversion again.
• Add comments to explain the functions of the program.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  #include <iostream>
#include <Windows.h> 
using std::cout;
using std::endl;
using std::internal; //uses internal spacing
using std::showpos; // shows positive symbol on positive integers
#include <string>
#include <iomanip>
using std::setw;

using namespace std;

int main()
{
	HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); // finds color options for text
	SetConsoleTextAttribute(h, FOREGROUND_RED ); // applies red color to the following text
	char again = 'Y'; //defines the variable for continuing the program

	cout << "Hello Instructor Kevin Lee," << endl << endl << endl; //greets instructor aka the user
	cout << "This is Cherilyn Bradfords' Temperature Conversion Program" << endl << endl; // identifies me
	cout << "Created for INF231, Programming Concepts." << endl << endl << endl; // identifies class

	while (again == 'y' || again == 'Y') //begins while loop
	{
		SetConsoleTextAttribute(h, FOREGROUND_GREEN); //applies green color to the following text
		
		double userInput;
		cout << "Enter Fahrenheit Value: ";
		do
		{
			cin >> userInput;
			if (cin.fail());
			{
				cout << "invalid value, please try again." << endl;
				cin.clear();
				cin.ignore();
			}
					
		} 
		while (cin.fail() || (userInput < 213) || (userInput > -147));
			

		double celsius;
		celsius = (userInput - 32) *5.0 / 9.0; //sets output formula
		cout << fixed; //enables for spesific decimal precision
		cout << setprecision(2); //sets precision value to 2 points after the decimal
		cout << "\n"; //blank line
		cout << internal << showpos << setw(5) << userInput << " Degrees Fahrenheit = " << celsius << " Degrees Celsius. \n" << endl << endl; //output display for result calculated with +/- symbol included

		cout << "Would you like to convert more values?" << endl << endl; //asks to continue or not
		cout << "Enter 'Y' to continue." << endl << endl; // requires user input to continue or end program
		cin >> again; //change control variable
		cout << endl; // spacing for viewing ease
	
	} // ends while loop

		SetConsoleTextAttribute(h, FOREGROUND_RED); //applies red color to the following text
	cout << endl << endl; //adds spacing for better viewing
	cout << right << setw(15) << "Thank You" << endl; //statement displayed when user chooses not to continue
	cout << right << setw(14) << "The End" << endl << endl << endl; // this and previous statement also right aligned and centered to each other for nice viewing.

	
		system("pause"); //added so user can terminate program

	return 0;

}


Thank you in advance for any help and/or suggestions!
However I am having trouble working out the code to properly loop it back and recognize when a correct value is entered in order to complete the program.


What is happening/not happening that is the problem and what did you want to happen? Or is the code not compiling? If not, what are the errors?
when I run the program no matter what number I input, it just outputs the error message "invalid value, please try again" over and over again with each number input even if it is a number within the range that should make the program calculate and finish.

I would like for it to move forward to calculate the Celsius value of the user input variable (Fahrenheit value) and complete the program... when an acceptable number is input.

does that make sense...?

I am not getting any error messages or codes, it runs, just doesn't run the way I need it to.

Thank you!

You have an erroneous ';' after your if statement on line 32. It's causing your if statement block to always run.
I removed the suggested ';' on line 32. Thank you

Now when I run the program I enter a number it moves to the next line but does nothing. It does this whether it is a good number or a bad number. So I no longer get the repeating error message I now get nothing at all but a blinky cursor on the next line below.

That solved some of the issue but how do I fix this problem. I have tried fiddling with it a bit but haven't managed to correct the issue yet. Any more suggestions?

Thank you!

Is the user input supposed to be greater than -143 and less than 213?
Yes, Greater than -147 but less than 213.

if it fits those parameters then the program should go on to calculate the Celsius value and then ask the user if they want to calculate another value.

If the user input value does not fit those parameters then it should give an error message of "invalid value please try again" then if a valid number is entered it should go on as above, if a valid number is not given then it should go back to the error message UNTIL a valid number has been entered.
while (cin.fail() || (userInput < 213) || (userInput > -147));

OK - the while loop continues if the number is within the valid range. It's waiting for input from line 31. You want to check if the input is out of the valid range, so flip the less than/greater than signs.

Also - if there is a fail within the loop, you clear it, so there won't be a fail for that first while condition. But since valid input isn't captured after the fail, the calculation goes ahead without valid info.
OK
I flipped the less than/greater than signs...

This worked to a degree.
The program runs and will continue to run properly as long as a number within the valid range is entered. If i enter a number not within the valid range it does nothing. No error message or anything it just moves a blinky cursor to the next line.

I am thinking this is what the second part of your reply was about, however I am not quite understanding what you mean and/or how to do what you said.

I'm really new to this and have found success learning the concepts on youtube as opposed to my school book because it seems easier for me to understand. If there is a little more clarification you can give me that would be excellent.

I very much appreciate your help and patience! I feel like I am on the brink of realizing the issue and correcting it and actually understanding it all as well!
No error message or anything it just moves a blinky cursor to the next line.


It's waiting for input for the cin at line 31. You have the message "Enter Fahrenheit Value: " outside the loop, so that only gets asked once.

Just a suggestion - maybe there should be some message including the valid range of values? Either in the request for input or a specific error if they don't enter within the range?

If they enter something like 't' instead of a number - the fail bit is set. You clear that out over lines 35-36, so there won't be a fail to match this condition in the while statement while (cin.fail() || . The variable userInput won't have a valid value from input yet.

When I ran this with 't', I get the error message, but the calculation goes ahead. The uninitialized variable comes out as 0 in the compiler attached to this forum (the little gear icon at the top right of the code block), but sometimes shows as a garbage value. Somehow you want to work the while loop so you ask the user for a new input if you've cleared out the fail.

Enter Fahrenheit Value: t 
invalid value, please try again. 
+0.00 Degrees Fahrenheit = -17.78 Degrees Celsius. 

"enter Fahrenheit value" is only meant to be asked once in the beginning and once if you choose 'y' to run the program again. So that part is actually functioning as I intended. (Thank God something is lol).

I like your suggestion I may try to add that in later after I figure out how to get this to run the way I want it to. (Thank you!)

I tried running the program with several different character variable inputs instead of numbers and also with numbers within the range. It runs as I would like now for this part.

The issue I still have is when I use a number (as opposed to a character such as 't') that is not within the desired range it does not output the desired text of "invalid value, please try again". It simply moves the cursor to the next line and waits for another value to be entered. It continues to do this with no error message output until you enter a valid number. Then the program completes.

Here is what I have now...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
 
#include <iostream>
#include <Windows.h> 
using std::cout;
using std::endl;
using std::internal; //uses internal spacing
using std::showpos; // shows positive symbol on positive integers
#include <string>
#include <iomanip>
using std::setw;

using namespace std;

int main()
{
	HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); // finds color options for text
	SetConsoleTextAttribute(h, FOREGROUND_RED); // applies red color to the following text
	char again = 'Y'; //defines the variable for continuing the program

	cout << "Hello Instructor Kevin Lee," << endl << endl << endl; //greets instructor aka the user
	cout << "This is Cherilyn Bradfords' Temperature Conversion Program" << endl << endl; // identifies me
	cout << "Created for INF231, Programming Concepts." << endl << endl << endl; // identifies class

	while (again == 'y' || again == 'Y') //begins while loop
	{
		SetConsoleTextAttribute(h, FOREGROUND_GREEN); //applies green color to the following text

		double userInput;
		cout << "Enter Fahrenheit Value: ";
		do
		{
			cin >> userInput;
			if (cin.fail())
			{
				cout << "invalid value, please try again." << endl;
				cin.clear();
				cin.ignore();
				cin >> userInput;
			}

		} while (cin.fail() || (userInput > 213) || (userInput < -147));


		double celsius;
		celsius = (userInput - 32) *5.0 / 9.0; //sets output formula
		cout << fixed; //enables for spesific decimal precision
		cout << setprecision(2); //sets precision value to 2 points after the decimal
		cout << "\n"; //blank line
		cout << internal << showpos << setw(5) << userInput << " Degrees Fahrenheit = " << celsius << " Degrees Celsius. \n" << endl << endl; //output display for result calculated with +/- symbol included

		cout << "Would you like to convert more values?" << endl << endl; //asks to continue or not
		cout << "Enter 'Y' to continue." << endl << endl; // requires user input to continue or end program
		cin >> again; //change control variable
		cout << endl; // spacing for viewing ease

	} // ends while loop

	SetConsoleTextAttribute(h, FOREGROUND_RED); //applies red color to the following text
	cout << endl << endl; //adds spacing for better viewing
	cout << right << setw(15) << "Thank You" << endl; //statement displayed when user chooses not to continue
	cout << right << setw(14) << "The End" << endl << endl << endl; // this and previous statement also right aligned and centered to each other for nice viewing.


	system("pause"); //added so user can terminate program

	return 0;

}


I have played with a few things to try and get the message to pop up as I would like when an wrong number is entered however I still haven't been able to make it work. (so close and yet so far lol)

Thank you for your continued help and patience!
BY JOE I THINK I GOT IT! lol

I kept moving things around until I finally got it to run the way I want! Here it is now;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream> //alows access to iostream library
#include <Windows.h> //allows access to windows library
using std::cout;
using std::endl;
using std::internal; //uses internal spacing
using std::showpos; // shows positive symbol on positive integers
#include <string> // allows for string inclusion
#include <iomanip> // allows for space/padding inclusion
using std::setw; // spacing/padding
using namespace std;

int main()
{
	HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); // finds color options for text
	SetConsoleTextAttribute(h, FOREGROUND_RED); // applies red color to the following text
	char again = 'Y'; //defines the variable for continuing the program

	cout << "Hello Instructor Kevin Lee," << endl << endl << endl; //greets instructor aka the user
	cout << "This is Cherilyn Bradfords' Temperature Conversion Program" << endl << endl; // identifies me
	cout << "Created for INF231, Programming Concepts." << endl << endl << endl; // identifies class

	while (again == 'y' || again == 'Y') //begins while loop
	{
		SetConsoleTextAttribute(h, FOREGROUND_GREEN); //applies green color to the following text

		double userInput;
		cout << "Enter Fahrenheit Value: "; // asks for a value to convert
		do // starts the do loop
		{
			cin >> userInput; //user input value
			cout << "\n"; // blank line
			if (cin.fail() || (userInput > 213) || (userInput < -147)) // defines the range of acceptable user input values
			{
				cout << "invalid value, please try again: "; // output when unacceptable value is input
				cin.clear(); // clear 
				cin.ignore(); // so output doesn't repeat 
			}

		} while (cin.fail() || (userInput > 213) || (userInput < -147)); // defines acceptable range of user input values

		double celsius;
		celsius = (userInput - 32) *5.0 / 9.0; //sets output formula
		cout << fixed; //enables for spesific decimal precision
		cout << setprecision(2); //sets precision value to 2 points after the decimal
		cout << "\n"; //blank line
		cout << internal << showpos << setw(5) << userInput << " Degrees Fahrenheit = " << celsius << " Degrees Celsius. \n" << endl << endl; //output display for result calculated with +/- symbol included

		cout << "Would you like to convert more values?" << endl << endl; //asks to continue or not
		cout << "Enter 'Y' to continue." << endl << endl; // requires user input to continue or end program
		cin >> again; //change control variable
		cout << endl; // spacing for viewing ease

	} // ends while loop

	SetConsoleTextAttribute(h, FOREGROUND_RED); //applies red color to the following text
	cout << endl << endl; //adds spacing for better viewing
	cout << right << setw(15) << "Thank You" << endl; //statement displayed when user chooses not to continue
	cout << right << setw(14) << "The End" << endl << endl << endl; // this and previous statement also right aligned and centered to each other for nice viewing.


	system("pause"); //added so user can terminate program

	return 0; //terminates program

}


Thank you firedraco and extra special thank you wildblue!! Is there somewhere I can post a good review or comment for you??
Last edited on
Topic archived. No new replies allowed.