Main cpp 13 1 error expected unqualified id before token

Что делать? пишет [Error] expected unqualified-id before '{' token C++ Решение и ответ на вопрос 2158289

BananEvgeniy

0 / 0 / 0

Регистрация: 04.12.2017

Сообщений: 7

1

16.12.2017, 00:40. Показов 38389. Ответов 14

Метки dev-c++ (Все метки)


C++
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
#include <iostream>
#include <math.h>
using namespace std;
int count;
 
 
 
    int main(0);
    {
    
for (i=0,i<11,i++)
 
    {
    X[i]=rand()%25-10;
    Y[i]=rand()%25-10;
}
for (i=0,i<11,i++)
 
    if(X[i]=Y[i])
    {
    
        count++;
        cout<<X[i];
}
    return 0;
}

Ругается на самую первую {

что не так?

__________________
Помощь в написании контрольных, курсовых и дипломных работ, диссертаций здесь



0



437 / 429 / 159

Регистрация: 21.05.2016

Сообщений: 1,338

16.12.2017, 01:21

2

int main()

Добавлено через 38 секунд
в for заменить запятые на точку с запятой

Добавлено через 53 секунды
объявить массивы X и Y до их использования

Добавлено через 42 секунды
инициализировать count перед первым использованием

Добавлено через 3 минуты
и еще несколько ошибок



0



3986 / 3255 / 910

Регистрация: 25.03.2012

Сообщений: 12,104

Записей в блоге: 1

16.12.2017, 01:37

3

int main(0);
убрать точку с 3апятой



0



0 / 0 / 0

Регистрация: 04.12.2017

Сообщений: 7

16.12.2017, 22:46

 [ТС]

4

все равно не помогает, может код неправильынй какой-то?я не сильно еще разбираюсь тут



0



wareZ1400

12 / 13 / 2

Регистрация: 11.01.2015

Сообщений: 208

Записей в блоге: 15

16.12.2017, 22:58

5

BananEvgeniy,

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <math.h>
using namespace std;
int c = 0;
int X[10];
int Y[10];
 
int main()
{
    for (int i = 0; i < 11; i++)
    {
        X[i] = rand() % 25 - 10;
        Y[i] = rand() % 25 - 10;
    }
    for (int i = 0; i < 11; i++)
        if (X[i] == Y[i])
        {
            c++;
            cout << X[i];
        }
    return 0;
}



0



Kuzia domovenok

3986 / 3255 / 910

Регистрация: 25.03.2012

Сообщений: 12,104

Записей в блоге: 1

17.12.2017, 00:17

6

Цитата
Сообщение от wareZ1400
Посмотреть сообщение

C++
1
2
int X[10];
 int Y[10];

учи матчасть!

C++
1
2
int X[11];
 int Y[11];

Добавлено через 1 минуту

Цитата
Сообщение от oldnewyear
Посмотреть сообщение

int main()
Добавлено через 38 секунд
в for заменить запятые на точку с запятой
Добавлено через 53 секунды
объявить массивы X и Y до их использования
Добавлено через 42 секунды
инициализировать count перед первым использованием
Добавлено через 3 минуты
и еще несколько ошибок

wareZ1400, ты точно прочитал этот коммент и переписал с учётом всех замечаний?



0



wareZ1400

12 / 13 / 2

Регистрация: 11.01.2015

Сообщений: 208

Записей в блоге: 15

17.12.2017, 02:52

7

Kuzia domovenok, точно прочитать коммент и переписал с учетом всех замечаний.

Вот:

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <math.h>
using namespace std;
int c = 0;
int X[11];
int Y[11];
 
int main()
{
    for (int i = 0; i < 11; i++)
    {
        X[i] = rand() % 25 - 10;
        Y[i] = rand() % 25 - 10;
    }
    for (int i = 0; i < 11; i++)
        if (X[i] == Y[i])
        {
            c++;
            cout << X[i];
        }
    return 0;
}



0



0 / 0 / 0

Регистрация: 04.12.2017

Сообщений: 7

17.12.2017, 21:53

 [ТС]

8

все равно не работает
выделяет X[i] = rand() % 25 — 10; вот эту стрчоку красным и пишет [Error] ‘rand’ was not declared in this scope



0



0 / 0 / 2

Регистрация: 29.11.2017

Сообщений: 16

17.12.2017, 22:06

10

Ну правильно. Убери нахрен ; там где int main();



0



0 / 0 / 0

Регистрация: 04.12.2017

Сообщений: 7

17.12.2017, 22:46

 [ТС]

11

уже давно убрал, все равно ошибку пишет



0



Comevaha

0 / 0 / 2

Регистрация: 29.11.2017

Сообщений: 16

22.12.2017, 20:24

12

C++
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
#include <iostream>
#include <math.h>
using namespace std;
int count;
 
int X[11];
int Y[11];
 
int main(){
    for (auto i=0;i<11;i++)
    {
        X[i]=rand()%25-10;
        Y[i]=rand()%25-10;
    }
 
    for (auto i=0;i<11;i++)
    {
 
        if(X[i]==Y[i])
        {
            count++;
            cout<<X[i];
        }
    }
    return 0;
}

Берите код. Работает



0



12 / 13 / 2

Регистрация: 11.01.2015

Сообщений: 208

Записей в блоге: 15

24.12.2017, 01:37

13

Comevaha, а Вы каким компилятором компилите?



0



0 / 0 / 2

Регистрация: 29.11.2017

Сообщений: 16

24.12.2017, 14:20

14

Mingw



0



12 / 13 / 2

Регистрация: 11.01.2015

Сообщений: 208

Записей в блоге: 15

24.12.2017, 15:57

15

Comevaha, то, что я написал компилится в ms visual studio. А почему у других может не компилиться?



0



  • Forum
  • Beginners
  • Expected unqualified-id

Expected unqualified-id

Hi, I’m having some issues with my code and I am getting some odd errors.

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

int main() {

cout << "Enter three numbers: " << endl;
char cond1,cond2,cond3;
int num1, num2, num3;
cin >> num1 >> num2 >> num3;

if (num1>num2);{
cond1 = 't';}
else{
cond1 = 'f'
}
if (num1>num3){
cond2 = 't'}
else{
cond2 = 'f'}
if (num2>num3){
cond3 = 't'}
else{
 cond3 = 'f'}


if (cond1 == 't' && cond2 == 't'){
cout << num1 << " is the maximum" << endl;
else if (cond1 == 't' && cond2 == 'f'){
cout << num3 << " is the maximum" << endl;}
else if (cond1 == 'f' && cond3 == 't'){
cout << num2 << " is the maximum" << endl;}
}

if (cond1 == 'f' && cond2 == 'f') {
cout << num1 << " is the minimum" << endl;
}
else if (cond1 == 't' && cond3 == 'f'){
cout << num2 << " is the minimum" << endl;}
else if (cond2 == 't' && cond3 == 't');{
cout << num3 << " is the minimum" << endl;}
}
return 0;

Please excuse the sloppy formatting.

Here are the errors I am receiving when I try to run the code:

1
2
3
4
5
6
7
8
9
Lab3P2.cpp: In function 'int main()':
Lab3P2.cpp:15: error: expected `}' before 'else'
Lab3P2.cpp: At global scope:
Lab3P2.cpp:15: error: expected unqualified-id before 'else'
Lab3P2.cpp:18: error: expected unqualified-id before 'if'
Lab3P2.cpp:20: error: expected unqualified-id before 'else'
Lab3P2.cpp:22: error: expected unqualified-id before 'if'
Lab3P2.cpp:24: error: expected unqualified-id before 'else'
Lab3P2.cpp:28: error: expected unqualified-id before 'if' 

I have no idea what they mean by «expected unqualified-id before», and every time I add the } before «else» in line 15, I just get more errors.

Any help would be greatly appreciated.

Last edited on

Remove the ; from line 13.

You have a semicolon on line 13 that shouldn’t be there.

16, 19, 22, 25 all require semicolons (before the brace but after the letter):

cond2 = 't' ; /*<--*/ }

Thanks for the help so far. I did what you guys said, and now I’ve gotten these errors:

1
2
3
4
5
6
7
8
Lab3P2.cpp: In function 'int main()':
Lab3P2.cpp:29: error: expected `}' before 'else'
Lab3P2.cpp: At global scope:
Lab3P2.cpp:35: error: expected unqualified-id before 'if'
Lab3P2.cpp:38: error: expected unqualified-id before 'else'
Lab3P2.cpp:40: error: expected unqualified-id before 'else'
Lab3P2.cpp:40: error: expected unqualified-id before '{' token
Lab3P2.cpp:42: error: expected declaration before '}' token 

So I get a few more of the expected unqualified-id errors, but in different lines now, and I’m still not entirely sure what this is supposed to mean.

Here’s the updated code, if it helps:

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

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

int main() {

cout << "Enter three numbers: " << endl;
char cond1,cond2,cond3;
int num1, num2, num3;
cin >> num1 >> num2 >> num3;

if (num1>num2){
cond1 = 't';}
else{
cond1 = 'f';
}
if (num1>num3){
cond2 = 't';}
else{
cond2 = 'f';}
if (num2>num3){
cond3 = 't';}
else{
 cond3 = 'f';}


if (cond1 == 't' && cond2 == 't'){
cout << num1 << " is the maximum" << endl;
else if (cond1 == 't' && cond2 == 'f'){
cout << num3 << " is the maximum" << endl;}
else if (cond1 == 'f' && cond3 == 't'){
cout << num2 << " is the maximum" << endl;}
}

if (cond1 == 'f' && cond2 == 'f') {
cout << num1 << " is the minimum" << endl;
}
else if (cond1 == 't' && cond3 == 'f'){
cout << num2 << " is the minimum" << endl;}
else if (cond2 == 't' && cond3 == 't');{
cout << num3 << " is the minimum" << endl;}
}
return 0;

Last edited on

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

int main() {

cout << "Enter three numbers: " << endl;
char cond1,cond2,cond3;
int num1, num2, num3;
cin >> num1 >> num2 >> num3;

if (num1>num2){
cond1 = 't';
}
else{
cond1 = 'f';
}
if (num1>num3){
cond2 = 't';
}
else{
cond2 = 'f';
}
if (num2>num3){
cond3 = 't';
}
else{
 cond3 = 'f';
}


if (cond1 == 't' && cond2 == 't'){
cout << num1 << " is the maximum" << endl;
}
else if (cond1 == 't' && cond2 == 'f'){
cout << num3 << " is the maximum" << endl;
}
else if (cond1 == 'f' && cond3 == 't'){
cout << num2 << " is the maximum" << endl;
}

if (cond1 == 'f' && cond2 == 'f') {
cout << num1 << " is the minimum" << endl;
}
else if (cond1 == 't' && cond3 == 'f'){
cout << num2 << " is the minimum" << endl;
}
else if (cond2 == 't' && cond3 == 't');{
cout << num3 << " is the minimum" << endl;
}
return 0;
}

Missing ‘}’ at the end and a few other edits done.

Last edited on

You really need to work on indenting your code:

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

int main()
{

	cout << "Enter three numbers: " << endl;
	char cond1,cond2,cond3;
	int num1, num2, num3;
	cin >> num1 >> num2 >> num3;

	if (num1>num2)
	{
		cond1 = 't';
	}
	else
	{
		cond1 = 'f';
	}
	if (num1>num3)
	{
		cond2 = 't';
	}
	else
	{
		cond2 = 'f';
	}
	if (num2>num3)
	{
		cond3 = 't';
	}
	else
	{
		cond3 = 'f';
	}


	if (cond1 == 't' && cond2 == 't')
	{
		cout << num1 << " is the maximum" << endl;
	}// you were missing this
	else if (cond1 == 't' && cond2 == 'f')
	{
		cout << num3 << " is the maximum" << endl;
	}
	else if (cond1 == 'f' && cond3 == 't')
	{
		cout << num2 << " is the maximum" << endl;
	}

	if (cond1 == 'f' && cond2 == 'f')
	{
		cout << num1 << " is the minimum" << endl;
	}
	else if (cond1 == 't' && cond3 == 'f')
	{
		cout << num2 << " is the minimum" << endl;
	}
	else if (cond2 == 't' && cond3 == 't') //; // this doesn't belong here
	{
		cout << num3 << " is the minimum" << endl;
	}
//} // this doesn't go here	
	return 0;
} // it goes here 

Thanks CaptainBlastXD, works with your edits. Greatly appreciate all the help guys.

Topic archived. No new replies allowed.

Expected unqualified id errorThe expected unqualified id error shows up due to mistakes in the syntax. As there can be various situations for syntax errors, you’ll need to carefully check your code to correct them. Also, this post points toward some common mistakes that lead to the same error.

Go through this article to get an idea regarding the possible causes and gain access to the solutions to fix the given error.

Why Are You Getting the Expected Unqualified Id Error?

You are getting the expected unqualified-id error due to the erroneous syntax. Please have a look at the most common causes of the above error.

– Missing or Misplaced Semicolons

You might have placed a semicolon in your code where it wasn’t needed. Also, if your code misses a semicolon, then you’ll get the same error. For example, a semicolon in front of a class name or a return statement without a semicolon will throw the error.

This is the problematic code:

#include <iostream>
using namespace std;
class myClass;
{
private:
string Age;
public:
void setAge(int age1)
{
Age = age1;
}
string getAge()
{
return Age
}
}

– Extra or Missing Curly Braces

Now, if your code contains extra curly braces or you’ve missed a curly bracket, then the stated error will show up.

The following code snippet contains an extra curly bracket:

#include <iostream>
using namespace std;
class myClass;
{
private:
string Age;
public:
void setAge(int age1)
{
Age = age1;
}
}
}

– String Values Without Quotes

Specifying the string values without quotes will throw the stated error.

Here is the code that supports the given statement:

void displayAge()
{
cout << Your age is << getWord() << endl;
}

How To Fix the Error?

You can fix the mentioned unqualified-id error by removing the errors in the syntax. Here are the quick solutions that’ll save your day.

– Get Right With Semicolons

Look for the usage of the semicolons in your code and see if there are any missing semicolons. Next, place the semicolons at their correct positions and remove the extra ones.

Here is the corrected version of the above code with perfectly-placed semicolons:

#include <iostream>
using namespace std;
class myClass
{
private:
string Age;
public:
void setAge(int age1)
{
Age = age1;
}
string getAge()
{
return Age;
}
}

– Adjust the Curly Braces To Fix the Expected Unqualified Id Error

You should match the opening and closing curly braces in your code to ensure the right quantity of brackets. The code should not have an extra or a missing curly bracket.

– Wrap the String Values inside Quotes

You should always place the string values inside quotes to avoid such errors.

This is the code that will work fine:

void displayAge()
{
court << “Your age is” << getWord() << endl;
}

FAQ

You can have a look at the following questions and answers to enhance your knowledge.

– What Does a Qualified ID Mean?

A qualified-id means a qualified identifier that further refers to a program element that is represented by a fully qualified name. The said program element can be a variable, interface, namespace, etc. Note that a fully qualified name is made up of an entire hierarchical path having the global namespace at the beginning.

– What Does the Error: Expected ‘)’ Before ‘;’ Token Inform?

The error: expected ‘)’ before ‘;’ token tells that there is a syntax error in your code. Here, it further elaborates that there is an unnecessary semi-colon before the closing round bracket “).” So, you might get the above error when you terminate the statements that don’t need to be ended by a semi-colon.

– What Do You Mean By Token in C++?

A token is the smallest but important component of a C++ program. The tokens include keywords, punctuators, identifiers, etc. For example, you missed a semi-colon in your code because you considered it something that isn’t very important. But the C++ compiler will instantly show up an error pointing towards the missing “;” token.

Conclusion

The unqualified id error asks for a careful inspection of the code to find out the mistakes. Here are a few tips that’ll help you in resolving the given error:

  • Ensure the correct placement of semicolons
  • Aim to have an even number of curly brackets
  • Don’t forget to place the text inside the quotes

How to fix expected unqualified idNever write the code in hurry, you’ll only end up making more mistakes, and getting such errors.

  • Author
  • Recent Posts

Position is Everything

Position Is Everything: Your Go-To Resource for Learn & Build: CSS,JavaScript,HTML,PHP,C++ and MYSQL.

Position is Everything

Понравилась статья? Поделить с друзьями:
  • Main bios checking error
  • Mailer error could not instantiate mail function
  • Mailbox format error
  • Make error 139
  • Mailbox delivery failure policy error