Newbie C++ errors expected ;

Hey, when I compile script I get these errors:

13:16 [Warning] character constant too long for its type (NOT AN ISSUE!)
In function `int main()':
15 expected primary-expression before "else"
15 `;' before "else"

This is my script:

#include <iostream>
using namespace std;

int main (){
char stop = 0;
int n;
cout << "Enter the starting number > " << endl;
cin >> n;

while (n>0){
cout << n << ", " << endl;
cin >> stop;
if (stop = 'abort'){
break;
else{
continue;
}
}

--n;
}

cout << "End of program.";
cout << " ";
system("pause");
return 0;
}
script

It's not a script. Scripts are interpreted. You're writing code, to be compiled.

Try this instead. I'll let you spot the differences.

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
#include <iostream>
#include <string>
using namespace std;

int main (){
string stop;
int n;
cout << "Enter the starting number > " << endl;
cin >> n;

while (n>0){
cout << n << ", " << endl;
cin >> stop;
if (stop == "abort"){
break;
}else{
continue;
}


--n;
}

cout << "End of program.";
cout << " ";
system("pause");
return 0;
}


(NOT AN ISSUE!)

Only if you don't mind overwriting other variables (probably n) with garbage.
Last edited on
Thanks, that got rid of the errors. I can see that what you've done was was including the string library and changed the char into a string as a string can hold more data then a char and in line 1.4 you added an extra = (not sure what the difference is). However, the main point of the program was to make it count down to 0, hitting the 0 making the program end. I know that it doesn't count down because of the break; operation but I can't see how can that affect the program if I don't make stop == abort. I read something about it, I guess I'll go on investigating and while writing this I found out how to make it count down. Altogether, I thank you very much for your help, I used it to rewrite my code properly :)
= is the assignment operator.

This
stop = "abort"
sets the value of stop to "abort".

== is the comparison operation.

This
stop == "abort"
means "is the current value of stop equal to "abort"?"

That's a very, very big difference.

Your program doesn't count down because you aren't changing the value of n inside the loop.
Last edited on
That and also

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//this block's syntax is incorrect.
if (stop = 'abort'){
break;                             
else{
continue;
}
}

//The proper SYNTAX (and not code) will be

if (stop = 'abort'){
break;
}                         //Inserted an bracket!!
else{
continue;
}



//The proper SYNTAX (and not code) will be


I maintain that people do write C++ code. The only people who sit down to write C++ syntax is the ISO language definition group. Syntax concerns grammar; it is the rules of a given language or notation. C++ code has rules, but C++ code is not those rules.

if (stop = 'abort')
That's incorrect. The ' notation goes around a single character. Putting it around a multi-character string is bgad. Perhaps you meant
if (stop = "abort"')
Last edited on
@Moschops
Sorry if you got me wrong!! I was ONLY trying to tell the OP about the structure of an if-else block and I ignored other details because I assumed that the OP will be overwhelmed to see all those details at once.
That's OK! :) I managed to do some research and got the loop to work and pretty much everything. EXCEPT for one thing. This is, finding a way where the user can input something within a limited time and not until the user inputs something. By this I mean that something like:
cin >> abort;
will not work as it wont continue until the user inputs something.

So, to summarize, is there a way to giving the user a certain time-limit within which he has to input something or the program will continue?
Topic archived. No new replies allowed.