Missing ; before else

Hey, I am a 14 year old newbie in C++ and I tried to code a program that will dountdown from n, however, if you type abort, the program will end quicker.
When I debugged my program, I ended up with an error that I couldn't resolve. At the moment I am in school therefore I can't give the exact info, but it was something along these lines:

expected primary function (something something)
expected ; before else
(one line of error here)



Any help will be appreciated as I don't think that the mistake is in the missing ; but because of the way I laid out my code. Also, I will update the error log as soon as I get home.

#include <iostream>
#include <string>
using namespace std;

string stop;

int main(){

string stop = 0;
int n = 0;

cout << "This program has been done by Milan.";
cout << "/n";
cout << "Enter starting number" << "/n";
cin >> n;

if(n>0){
cout << n;
if(stop == "abort"){
cout << "program has stopped closure" << endl;
system("pause");
}
n--;

else{
cout << "/n" << endl;
cout << "The program has terminated";
}
}

system("pause");

return 0;

}
You'll never be able to properly diagnose errors if you summarize them as "something something" and "one line of error". Even if they're a bit cryptic, the error log contains vital information; both for yourself, as for others if you ask their help.

Anyway, judging on your code, I'd say the problem is here:

1
2
3
4
}
n--;

else{

An "else" can only appear right after an if('s closing bracket). The --n will have to be done somewhere else.
You need a loop for your countdown, like so:
1
2
3
4
for(int i = n; i > 0; n--)
{
...
}


Remove that else and all the braces around that terminated message (which remains outside the loop)

Since the countdown would end far too quick you need a pause other than system("pause");. Use Sleep(1000 /* 1 second*/); for windows

Oh and use code tags: [code]Your code[/code]
Last edited on
@Gaminic Thanks for the reply, I know I didn't describe the error properly but as I mentioned I am in school right now and therefore I can't give you the exact error log as we don't have a C++ program, eg: Visual Studio. Also, I will edit my error log that wasn't very good as soon as I get home and run my debugger. On the other hand, I used your advice and moved the n-- a little above:


Thanks for the advice you gave me :) I know that it was hard to give advice because of the crappy error log given.

cout << "This program has been done by Milan.";
cout << "/n";
cout << "Enter starting number" << "/n";
cin >> n;
if(n>0){
cout << n;
n--; //n here!!!
Sleep(1000);
if(stop == "abort"){
cout << "program has stopped closure" << endl;
system("pause");
}

else{
cout << "/n" << endl;
cout << "The program has terminated";
}
}

system("pause");

return 0;

}


@coder777 hey, Thanks for the reply as well. Also, thank you for the advice of using Sleep(1000); , however, the system("pause"); is there to stop the program, not necessarily pause and run the program again, however, I used the line of code given here:

cout << "This program has been done by Milan.";
cout << "/n";
cout << "Enter starting number" << "/n";
cin >> n;
if(n>0){
cout << n;
n--;
Sleep(1000); //to wait one second here
if(stop == "abort"){
cout << "program has stopped closure" << endl;
system("pause"); //to stop program
}

else{
cout << "/n" << endl;
cout << "The program has terminated";
}
}

system("pause"); //program to stop here, system("pause"); to stop it closing

return 0;

}

Thank you both for the advice, I will make an edit of the post as soon as I get home and check for bugs and fix them. :)
Hm, i think you confused about what a debugger is. Likely you mean the compiler. You can use a debugger when the compiler/linker has no error to determine what your program is actually doing (with breakpoints and watching the contents of the variables)

And: without the loop you don't have a countdown

however, the system("pause"); is there to stop the program
Sure, therefore I told you not to use it...

Please use code tags: [code]Your code[/code]
a27Stealth: Coder777's "don't use system("pause")" has a good reason!
http://www.cplusplus.com/articles/j3wTURfi/
Thanks, I'll have a look at it during break :)
Hi, I had a look at the article and the link in that article, however I don't understand how can system("pause"); create compiling errors. I've used it before and I didn't have to include any extra files that the article mentioned. Also, I have no idea what to replace system("pause"); with. Maybe by getch(); ? Also, you mentioned I need a loop. I thought that the
1
2
3
4
5
6
7
8
if(n>0){
if(stop == "abort"){
}

else{
}
}


is the actual loop. Thuogh, when I used the code you gave me
1
2
3
4
for(int i = n; i > 0; n--)
{
...
}


I got this error log:
prog.cpp: In function ‘int main()’:
prog.cpp:18: error: expected `)' before ‘;’ token
prog.cpp:18: warning: unused variable ‘i’
prog.cpp:18: warning: statement has no effect
prog.cpp:18: error: expected `;' before ‘)’ token
prog.cpp:38: error: expected `}' at end of input

I gave you this error log because of ideone.com, hwich is an online C++ compiler. However, I noticed that sometimes it shows errors that are different to the ones
on my compiler- Dev C++.
We can't really help much if we don't know the entire code (in this case).

You did a bit too-direct copy-paste of the loop; you don't actually need 'i' in this case, just 'n'.

The loop looks like this:
for (; n > 0; --n) cout << n << endl;
And it will print:
<n>
<n-1>
<n-2>
...
1
Ok, the loop i gave you contains an typo and the dots are not meant to be part of the code...

I think the easiest code to understand is (this should compile
1
2
3
4
5
6
for(int i = 0; i < n; i++) // standard loop
{
  cout << n - i << endl;
  // you cannot ask for "abort" here since that'd stop the countdown until the user enters something
  Sleep(1000); // for this you need to #include <Windows.h>
}
Thanks to you both, I managed to make the loop by using while(){ instead of if(){. However, I have no clue on how I could resolve the

1
2
3
4
5
6
7
	if(stop == "abort"){
		cout << "program has stopped closure" << endl; 
		system("pause");
		}
		else{
		n--;
		}



Do you have any clues on how to do this?
Why don't you simply use a for loop,
1
2
3
4
5
6
7
8
9
for(int i=n; i>=0; i--)
{
	printf("%d\n",i); // or rather, cout<<i<<endl;
	if ( stop == "abort" )
	{
		break;
		//put system pause or anything that you want to use to hold the screen here
	}
}

Ah, but yes, it is as coder777 explained, you can't use a cin/scanf in the for loop, as it would wait for user input between each count.
Last edited on
Because in the beginning, the string abort = 0; therefore you need to set
abort = "abort" and to do this, you need to have the user input "abort" which is a bad choice as the program will wait for the user to input something before it continues, per second. And I use a while loop because for me it makes more sense because the translation would be: While n is bigger then 0, do this... - which is a loop too.
Topic archived. No new replies allowed.