[Error] expected unqualified-id before '{' token

what am i doing wrong here what does the program needs

#include<iostream>
#include<cstdlib>
#include<cstdio>


using namespace std ;
int main()
{
int number; int digit ;
}
// take input from user
{
cout<<"enter a 4 digit int number:" ;
cin >>number;

digit=number%10 ;
cout<<" the digits are:";
cout<<digit<<",";

number=number/10;

digit=number%10;
cout<<digit<<",";

number=number/10;

digit=number%10;
cout<<digit<<",";

number=number/10;


digit=number%10
cout<<digit;
}

[/code]
You have extraneous braces. Just remove them. (And use code tags to post code.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main() {
    long number;
    cout << "Enter a positive integer: ";
    cin >> number;

    cout << "The digits are: ";

    while (number > 10) {
        cout << number % 10 << ',';
        number /= 10;
    }
    cout << number % 10 << '\n';
}

Intuitive indentation and exact error messages.
1
2
3
4
5
6
7
8
9
10
#include<iostream>

int main()
{
    int number; int digit ;
} // <- the main() ends here

{ // <- Is this the offending '{' ?
    std::cout << "enter a 4 digit int number:" ;
}

The compiler does tell the line, where it sees a syntax error. That is useful information, both for you and us.

[Edit]
A compiler says (about my code):
 In function 'int main()':
5:9: warning: unused variable 'number' [-Wunused-variable]
5:21: warning: unused variable 'digit' [-Wunused-variable]

 At global scope:
8:1: error: expected unqualified-id before '{' token

"At global scope": lines 8-10 are not inside any function. What did we forget or misplace?


1
2
3
4
5
6
7
8
9
10
11
#include<iostream>

int main()
{
    int number;
    int digit ;

    {
        std::cout << "enter a 4 digit int number:" ;
    }
} // <- the main() ends here 

Last edited on
Topic archived. No new replies allowed.