goto statement not working

Hi everyone,
I'm relatively new to c++ programming and I was trying to create a program to convert things, such as a number of degrees celcius to fahrenheit. But I have been running into problems. First of all, I was trying to use the goto statement like here:
1
2
3
4
5
6
7
8
9
int main ()
{
cout << "Welcome";
strart:
...
...
goto start;
return 0;
}

but it hasn't been working. So I tried replacing it with a while statement:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
cout << "Welcome";
int i = 0;
while (i <=0 )
{
...
char something;
cin >> something;
if (something == 'y')
{
i++;
}
else if (something == 'n')
{
...
}
else
{
return 0;
}
}
}

but that still isn't working. Could someone please help me?
What do you mean it isn't working? Are you getting an error when you compile? Are you getting a run-time error?
I'm getting an error when it compiles.
That's eaither beacause you named the label strart, not start, or because of the ...s
Sorry, didn't mean to write 'strart' in the post, but that is not my problem. I wrote it correctly in the file. And the ...s are just there to replace some code and make the focusing on the problem easier.
Your problem is because you have a return statement within the while statement, but not after it. The compiler doesn't like it because if theoretically the while loop finished without the final else{return 0;} block being executed, then int main() wouldn't return anything, which is illegal for anything except void functions.
EDIT: Also, you forgot the beginning brace for main(), but maybe that was just a typo.

Try:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
cout << "Welcome";
int i = 0;
while (i <=0 )
{
...
char something;
cin >> something;
if (something == 'y')
{
i++;
}
else if (something == 'n')
{
...
}
else
{
return 0;
}
}
return 0;
}

I think that may be your problem. If not, go ahead and post the error please :]
Last edited on
Topic archived. No new replies allowed.