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!