HELP knse2c.cxx:28:1: error: expected unqualified-id before ‘{’ token

closed account (ENhkSL3A)
Why do I keep getting this error?
knse2c.cxx:28:1: error: expected unqualified-id before ‘{’ token
Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iomanip>
using namespace std;
int anum, bnum, iresult;
{
cout<< "Exercise 2C" << endl;
cout<< "Kaitlin Stevers" << endl;
cout<< "September 5, 2016" << endl;
cout<< "This program will illustrate the results of division and modulus operators using integer and real values."

cout<< setprecision(2) << fixed;
cout<< "Input, give two integer values, separated by a space:" << endl;
cin>> anum >> bnum;
cout << "The integer values read in are: anum bnum" >> endl;

return 0;
}
Where's your main?

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

using namespace std;

int main()
{
  int anum, bnum, iresult;
  ...
  return 0;
}


And cout << "The integer values read in are: anum bnum" >> endl; isn't how you'd print them out.
cout << "The integer values read are: " << anum << ", " << bnum << endl;
closed account (ENhkSL3A)
Thank you so much! I figured I forgot something and I had a feeling that other line was wrong! :)
closed account (ENhkSL3A)
I am now getting a new error.
knse2c.cxx:42:67: warning: ‘iresult’ may be used uninitialized in this function [-Wmaybe-uninitialized]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	int anum, bnum, iresult;
cout<< "Exercise 2C" << endl;
cout<< "Kaitlin Stevers" << endl;
cout<< "September 5, 2016" << endl;
cout<< "This program will illustrate the results of division and modulus operators using integer and real values." << endl;

cout<< setprecision(2) << fixed;
cout<< "Input, give two integer values, separated by a space:" << endl;
cin>> anum >> bnum;
cout << "The integer values read are: " << anum << ", " << bnum << endl;
cout<<
"Integer result of integer/integer \t"<<anum<<" / "<<bnum<<" = "<<iresult<<endl;
return 0;
}
You should initialize your variables
 
int anum = 0, bnum = 0, iresult = 0;

If you don't give it an initial value, it'll just have whatever crap value was in memory where it is stored, so you could get unexpected results if you try to use it before you modify its value.
closed account (ENhkSL3A)
Where should I put that in? Also if I put that in will the user still be able to put their own values in?
Just put it in right when you declare them. Yes, the user can still put their own values in, it's just forcing it to have an initial value that you specify. It's still better to initialize your variables even if you know you'll be overwriting the value right away.
You didn't assign any value to iresult

Maybe you want

1
2
3
4
iresult = anum/bnum;
cout<<
"Integer result of integer/integer \t"<<anum<<" / "<<bnum<<" = "<<iresult<<endl;
return 0;


or just

1
2
3
cout<<
"Integer result of integer/integer \t"<<anum<<" / "<<bnum<<" = "<<anum/bnum<<endl;
return 0;


Since you state "Integer result of integer/integer" you might not know that integer/integer will always gives integer value result.
Last edited on
This is not totally related to your question, but this is an example why you should not initialize variable when declare them, you will not get warning if you initialize them at first place, and you might think it's other logic error that you get 0 for any input.

Do not initialize variable to meaningless value, do this only when you really know what value it should be (like iteration count in loops).
Last edited on
closed account (ENhkSL3A)
Hey guys! I have a question about loops if you all can help! It is posted here: http://www.cplusplus.com/forum/beginner/198613/ Thanks!
Topic archived. No new replies allowed.