Thank you in advance for any assistance. I am working on a final project and I'm writing a program which converts fahrenheit to celsius. I am getting 3 "not declared in scope error messages on lines 25, 27, and 31. I am not sure what I'm doing wrong. This what I have so far.
1. #include <iostream>
2.
3. using namespace std;
4.
5. int main()
6. {
7. double userInput;
8. cout << "Enter Fahrenheit Value: "; // asks for a value to convert
9. do // starts the do loop
10. {
11. cin >> userInput; //user input value
12. cout << "\n"; // blank line
13. if (cin.fail() || (userInput > 213) || (userInput < -147)) // defines the range of acceptable user input values
14. {
15. cout << "invalid value, please try again: "; // output when unacceptable value is input
16. cin.clear(); // clear
17. cin.ignore(); // so output doesn't repeat
18. }
19.
20. } while (cin.fail() || (userInput > 213) || (userInput < -147)); // defines acceptable range of user input values
21.
22. double celsius;
23. celsius = (userInput - 32) *5.0 / 9.0; //sets output formula
24. cout << fixed; //enables for specific decimal precision
25. cout << setprecision(2); //sets precision value to 2 points after the decimal
26. cout << "\n"; //blank line
27. cout << internal << showpos << setw(5) << userInput << " Degrees Fahrenheit = " << celsius << " Degrees Celsius. \n" << endl << endl; //output display for result calculated with +/- symbol included
28.
29. cout << "Would you like to convert more values?" << endl << endl; //asks to continue or not
30. cout << "Enter 'Y' to continue." << endl << endl; // requires user input to continue or end program
31. cin >> again; //change control variable
32. cout << endl; // spacing for viewing ease
33.
34. }
Okay, first of all, in order to user setw and setprecision, you need to include the iomanip library.
So write this on line 2 (after including iostream) : #include <iomanip>
31. cin >> again; //change control variable
Then in line 31, you want user to input something and store it in 'again' but you have not declared what again is. The compiler doesn't know what that is if it is a char or a string or an int or a double.
So somewhere before that statement, you need to declare the variable 'again'.
Firstly, the code isn't particularly readable seeing as you haven't used code tags or indentation. Secondly, exactly what is out of scope and where have you defined it? For example, you call setprecision(2) on line 25, but it is not defined anywhere in the code you have given.
Thanks I really appreciate the help. Your suggestion helped most definitely. I took out line 31, but now after entering "Y" to continue, I can't figure out how to ask to run line 8 again. My instructions are to use repetitive statements such as DO WHILE, FOR, or IF THEN ELSE to do the conversion again. When I run the program, it does its thing but then after it says to enter "Y" to continue, and I enter "Y", it says process returned. I have to figure out how to get it to ask to re-enter values again after I press "Y". This is what I have now.
1. #include <iostream>
2. #include <iomanip>
3.
4. using namespace std;
5.
6. int main()
7. {
8. double userInput;
9. cout << "Enter Fahrenheit Value: "; // asks for a value to convert
10. do // starts the do loop
11. {
12. cin >> userInput; //user input value
13. cout << "\n"; // blank line
14. if (cin.fail() || (userInput > 213) || (userInput < -147)) // defines the range of acceptable user input values
15. {
16. cout << "invalid value, please try again: "; // output when unacceptable value is input
17. cin.clear(); // clear
18. cin.ignore(); // so output doesn't repeat
19. }
20.
21. } while (cin.fail() || (userInput > 213) || (userInput < -147)); // defines acceptable range of user input values
22.
23. double celsius;
24. celsius = (userInput - 32) *5.0 / 9.0; //sets output formula
25. cout << fixed; //enables for specific decimal precision
26. cout << setprecision(2); //sets precision value to 2 points after the decimal
27. cout << "\n"; //blank line
28. cout << internal << showpos << setw(5) << userInput << " Degrees Fahrenheit = " << celsius << " Degrees Celsius. \n" << endl << endl; //output display for result calculated with +/- symbol included
29.
30. cout << "Would you like to convert more values?" << endl << endl; //asks to continue or not
31. cout << "Enter 'Y' to continue." << endl << endl; // requires user input to continue or end program
32. cout << "Re-enter values"; //user inputs value again
33. cout << endl; // spacing for viewing ease
34.
35. }
If you put everything that is in main in a while loop which uses a boolean variables which can be set to false if the user wishes to exit, then that should solve the problem. Also code tags are [ code ] and [ /code ] without the spaces. You can use the preview button to check if you've done it right. Also, line 21 is pointless, you have a loop and no curly braces so the loop doesn't do anything.
Ok, what I would suggest is that you before all you code in main, declare a char 'again' and set it to 'Y' and then put everything in a big while loop that runs only if again = 'y.
#include <iostream>
#include <iomanip>
usingnamespace std;
int main(){
char again = 'Y';
while (again == 'Y' || again == 'y')
{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 specific 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; // After user enters, it'll check the big while loop condition
// only if user enters 'Y' or 'y', it'll continue
cout << endl; // spacing for viewing ease
}
return 0;
}