expected Unqualified id error

not 100% sure what's going wrong or why it's stating the "expected unqaulfied id before 'int' the error is on line 20

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  //Program by Garrett Mathew Demers
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int doOneSet();

int main()
    {
        srand(time(0));
        doOneSet();
    }



    int doOneSet()
    {

    int num1=0, int num2=0, maximum100;
    int userinput;
    int
    for (int count =0, count <=5, count++)
    {
    num1=rand()% (maxnum+1);
    num2=rand()% (maxnum+1);
    cout <<num1 << "+" <<num2 << "="<< cin <<answer endl;
    }
    int num1=0, int num2=0, maximum100;
    for (int count =0, count <=5, count++)
    {
    num1=rand()% (maxnum+1);
    num2=rand()% (maxnum+1);
    cout <<num1 << "+" <<num2 << "="<<endl;
    }
    int num1=0, int num2=0, maximum100;
    for (int count =0, count <=5, count++)
    {
    num1=rand()% (maxnum+1);
    num2=rand()% (maxnum+1);
    cout <<num1 << "+" <<num2 << "="<<endl;
    }
    int num1=0, int num2=0, maximum100;
    for (int count =0, count <=5, count++)
    {
    num1=rand()% (maxnum+1);
    num2=rand()% (maxnum+1);
    cout <<num1 << "+" <<num2 << "="<<endl;
    }
    int num1=0, int num2=0, maximum100;
    for (int count =0, count <=5, count++)
    {
    num1=rand()% (maxnum+1);
    num2=rand()% (maxnum+1);
    cout <<num1 << "+" <<num2 << "="<<endl;
    }
    int num1=0, int num2=0, maximum100;
    for (int count =0, count <=5, count++)
    {
    num1=rand()% (maxnum+1);
    num2=rand()% (maxnum+1);
    cout <<num1 << "+" <<num2 << "="<<endl;
    }
    int num1=0, int num2=0, maximum100;
    for (int count =0, count <=5, count++)
    {
    num1=rand()% (maxnum+1);
    num2=rand()% (maxnum+1);
    cout <<num1 << "+" <<num2 << "="<<endl;
    };


Ummm, I don't know what you're trying to do here. Try making your variable separate and not on one line like this:

1
2
3
int num1=0;
int num2=0; 
int maximum100; (or int maximum = 100)
Hi,

Welcome to Cplusplus :+)

First up, if you have compilation errors, then please post them in full. We like to see exactly what the compiler is saying.

I understand you are a beginner so understand I am not trying to be harsh, it is just that I write in a terse fashion sometimes.

There are a number of problems with your code:

You have unused variables, uninitialised variables, and non-declared variables.

What is the purpose of cin on line 27?

You also have a lot of repeated code - can you figure out how to use another loop to reduce your code by about 50 lines? Or, put another way: How would you write the code to process 1000 numbers instead of only 8? Would you have 5000 lines of code?

Which compiler are you using? Try to sort out how to set the warnings to their maximum level.

Don't have magic numbers like 5 in your code - make them const variables instead:

const int NumOfRandValues = 5;

the normal idiom for a for loop is :

for (int count =0 ; count < NumOfRandValues ; count++) {}

If you use the <= operator, it will run 1 more time than what you specify. In your case (had you used the semicolons) it would run 6 times.

I strongly suggest you read the tutorial on this site - look at the link on the top left of this page.

Hope all is well :+)
Sorry about that, thanks for being easy on me.
Here's the complete list of errors
||In function 'int doOneSet()':|
|25|error: expected unqualified-id before 'for'|
|31|error: redeclaration of 'int num1'|
|20|error: 'int num1' previously declared here|
|31|error: expected unqualified-id before 'int'|
|32|error: expected initializer before '<=' token|
|32|error: expected ';' before '<=' token|
|32|error: expected primary-expression before '<=' token|
|32|error: expected ';' before ')' token|
|34|error: 'maxnum' was not declared in this scope|
|38|error: redeclaration of 'int num1'|
|20|error: 'int num1' previously declared here|
|38|error: expected unqualified-id before 'int'|
|39|error: expected initializer before '<=' token|
|39|error: expected ';' before '<=' token|
|39|error: expected primary-expression before '<=' token|
|39|error: expected ';' before ')' token|
|41|error: 'maxnum' was not declared in this scope|
|45|error: redeclaration of 'int num1'|
|20|error: 'int num1' previously declared here|
|45|error: expected unqualified-id before 'int'|
|46|error: expected initializer before '<=' token|
|46|error: expected ';' before '<=' token|
|46|error: expected primary-expression before '<=' token|
|46|error: expected ';' before ')' token|
|48|error: 'maxnum' was not declared in this scope|
|52|error: redeclaration of 'int num1'|
|20|error: 'int num1' previously declared here|
|52|error: expected unqualified-id before 'int'|
|53|error: expected initializer before '<=' token|
|53|error: expected ';' before '<=' token|
|53|error: expected primary-expression before '<=' token|
|53|error: expected ';' before ')' token|
|55|error: 'maxnum' was not declared in this scope|
|59|error: redeclaration of 'int num1'|
|20|error: 'int num1' previously declared here|
|59|error: expected unqualified-id before 'int'|
|60|error: expected initializer before '<=' token|
|60|error: expected ';' before '<=' token|
|60|error: expected primary-expression before '<=' token|
|60|error: expected ';' before ')' token|
|62|error: 'maxnum' was not declared in this scope|
|66|error: redeclaration of 'int num1'|
|20|error: 'int num1' previously declared here|
|66|error: expected unqualified-id before 'int'|
|67|error: expected initializer before '<=' token|
|67|error: expected ';' before '<=' token|
|67|error: expected primary-expression before '<=' token|
|67|error: expected ';' before ')' token|
|69|error: 'maxnum' was not declared in this scope|
|72|error: expected '}' at end of input|
||More errors follow but not being shown.|
||Edit the max errors limit in compiler options...|
||=== Build failed: 50 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
yikes:
the cin is for the user to input the answer the program itself is meant to generate random math problems and ask the user to solve them.
I'm using code blocks, I'll work on the loops.
i did as you suggested for the number of inputs
Hi,

The errors don't match the line numbers with the code you posted.

25|error: expected unqualified-id before 'for'|


line 22 has a problem.

|31|error: redeclaration of 'int num1'|


One can only declare variables once. line 29

|31|error: expected unqualified-id before 'int'|

you need semicolons between statements, as in :

int num1=0; int num2=0; int maximum100;

You probably meant to do this:

int num1 = num2 = 0;

Better yet don't do that - do this:

1
2
int num1 = 0;
int num2 = 0;


|32|error: expected initializer before '<=' token|
|32|error: expected ';' before '<=' token|
|32|error: expected primary-expression before '<=' token|
|32|error: expected ';' before ')' token|



This is because of the comma's instead of semicolons in the for loop.

|34|error: 'maxnum' was not declared in this scope|


as it says maxnum was not declared.

|72|error: expected '}' at end of input|


You are missing the closing brace for your function. Try to get your IDE to automatically add closing braces and parentheses brackets etc. Or get into the habit of typing {}, then going back to fill in.

So think about what your program does: It generates 2 random numbers then it is supposed to add them (it doesn't) and output the result. It does this 5 times. Then it does what it has already done, 8 times.

So you could have an outer loop that that does the inner loop 8 times.

the cin is for the user to input the answer the program itself is meant to generate random math problems and ask the user to solve them.


Ok, that is not how you store a value - assign the value to a variable:

1
2
int answer = num1 + num2;
cout << num1 << "+" <<num2 << "="<< answer << sendl;


I'm using code blocks


That is your IDE Integrated Development Environment which includes an editor, compiler, linker and debugger amongst other things. Your compiler is hopefully g++, if so set the warnings to -Wall -Wextra -pedantic you will have to try & find out where in your system to do this.

:+)
Last edited on
I'm not sure how I buggered the copy paste bit for the code.
I think i've fixed the Num1/num2 issues

the goal of the program is to generate two random numbers and ask the user to perform an arithmetic function ( I started with addition) I will then elaborate upon it with multiplication and subtraction
I've fixed my intitial issue but now the check doesn't work
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
//Program by Garrett Mathew Demers
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int doOneSet();

int main()
    {
        srand(time(0));
        doOneSet();
    }



    int doOneSet()
    {

    int num1=0;
    int num2=0;
    int maximum=100;
    int userinput;
    int answer;
    int num3;
    const int numofvalues = 5;
    for (int count =0; count<numofvalues; count++)
    {
    num1=rand()% (maximum+1);
    num2=rand()% (maximum+1);
    cout <<num1 << "+" <<num2 << "="<< endl;
    cin>> answer;
num3= num1 + num2;
if ( num3 = answer){
    cout << "correct"<< endl; // i don't really understand what's happening here but i can't get the errors to stop
// it just keeps putting correct regardless of whether or not it is
} 
    else{
cout << "incorrect" << endl;
}
}
    }



Last edited on
= is assignment always true

== is equality comparison

Read the tutorial
That means you use == in 'if' statements and simple = to give a variable a value:
1
2
3
4
5
int i = 0;
if(i == 0)
{
    [...]
}
Last edited on
Line 20:
int num1=0, int num2=0, maximum100;

Should be:
int num1=0; int num2=0; int maximum100;

Or:
int num1=0, num2=0, maximum100;
Last edited on
The issues i was having with this segment of the program have been worked out,
Now i just have to workout how i'm going to make it perform the other operations with this style int main
int main()
{
srand(time(0));
doOneSet('+');
doOneSet('-');
doOneSet('*');
}
but that's separate issue
thanks for the help
-Garrett
Registered users can post here. Sign in or register to post.