[Error] expected initializer before 'while'

I keep receiving this error, nothing I do helps it's in
line 11, while (again == 'Y' || again == 'Y') //begins while loop

Help


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
sing std::endl;
using std::showpos; // shows positive symbol on positive integers
#include <string> // allows for string inclusion
using namespace std;


int main()

while  (again == 'Y' || again == 'Y')  //begins while loop

{

double userInput;
char p[5]; "again";
		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
system("pause"); //added so user can terminate program

	return 0; //terminates program  

Last edited on
What is there before the while (again == 'Y' || again == 'Y')?
What is the first thing that there should be after main()?
besides int main()
nothing is before while (again == 'Y' || again =='Y'
You do have something essential missing, the braces around the body of the function.
See: http://www.cplusplus.com/doc/tutorial/program_structure/

Once you have followed that advice you will get a different error: What is the "again"?


PS. Please edit your post to use code tags.
See: http://www.cplusplus.com/articles/jEywvCM9/
Pay attention to indentation too. It can help reading your code.
When I put a brace { before the while statement I end up with a 10 more errors?
This is my first programming class I have never done anything like this and I'm frustrated.

These are the instruction for the program, I have to include all of this and I don't even know if I have included all of it.


Create the following program which converts Fahrenheit to Celsius. Your program must have the following functions:
•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.
Last edited on
I have been working on this and this is what I now have, with a set of new errors.
which are [Error] 'setprecision' cannot be used as a function
and [Error] 'setw' cannot be used as a function

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
int main()

{
int again, setprecision, setw;
while  ( again == 'Y' || again == 'Y');  //begins while loop



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


Include the header #include <iomanip>.
Hi dreaweiss, welcome to the forum.

I'll outline the problems with your code:

1.) On line 4, you declare three variables. Two of them, namely 'setprecision' and 'setw' are also the names of functions found in the 'std' namespace. Normally, one would access these functions by using the scope-resolution operator like so: std::setprecision and std::setw. However, since you've elected to use using namespace std; in global scope, the compiler thinks that your function calls on lines 28 and 30 are actually attempts to treat your variables on line 4 as functions.

The way to fix this is to simply remove the offending variables on line 4, since you aren't even using them for anything. Your fourth line of code should therefore only declare 'again'.

2.) Technically, line 4 shouldn't just declare 'again', but also define / initialize it. If you do not give 'again' an initial value, you're invoking undefined behavior on line 5 when you enter the while-loop because 'again' contains garbage, and you're attempting to access it. Fix this by initializing 'again' on line 4 (so that it may enter the while-loop).

In addition, 'again' shouldn't be an int, it should be a char.

3.) There's a discrepancy in the condition of your while-loop on line 5. What you probably meant to write was while(again == 'y' || again == 'Y'), not while(again == 'Y' || again == 'Y').

4.) The while-loop on line 5 has no body. The semi-colon (';') immediately following the parentheses on line 5 is the cause of this. You will have to remove the semi-colon and add some braces ('{' and '}') around all of the code that should be part of the while-loop's body.

That should be a good start. I've appended this basic code as a guide:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <iomanip>

int main() {

	char again = 'y';
	while (again == 'y' || again == 'Y') {

		//Do things in here...

	}

	return 0;
}
Last edited on
Xismn,
Thank you
I followed all of your directions, I no longer have any errors in my code but when I run it the black boxes opens and nothing is in it. It should be asking for a Fahrenheit degree and then give it to me Celsius, and then ask if I want to convert more values. I don't know where to go from here, with no errors showing up
This is what 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
#include <iostream> //allows access to iostream library
#include<iomanip>
//converting fahrenheit to celsius 
using std::cout;
using std::endl;
using std::showpos; // shows positive symbol on positive integers
#include <string> // allows for string inclusion
using namespace std;


int main()

{
char again = 'y';
{while  ( again == 'y' || again == 'Y');  //begins while loop



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



Last edited on
{while ( again == 'y' || again == 'Y'); //begins while loop

line 15 - two things

- Remove the semicolon at the end.
- Move the { from the beginning of the line to after the loop condition (but before the first line of the code in the loop).

1
2
3
4
while(condition)
{
   do this...
}
Wildblue,
IT WORKS !!!!!!!!
Thank you so much,
I'm crying right now... I have been working on this for a week, and just found this website yesterday!
You have no I dea how much you and Xismn have help a beginner in C++!

THANK YOU BOTH SO MUCH!!!!!!
IT runs now but It won't compile?
It gives me and
error Id returned 1 exit status


I've never seen this one before

Got it I didn't close my execution window when I ran the program !!
Thank you all !

Topic archived. No new replies allowed.