Very quick and simple only one error need help

Hi! I am new to the forum and I have a problem. I have a simple code just so that I can practice. Its silly and random so don't think I'm weird :P.
Anyways; I use xcode for my c++ programming and the problem is on the same line as the {, it shows an error message and says "expected unqualified-id" I will star the line in the code in which it says it. Thanks in advance guys!!! I really appreciate it.
Btw, if neccesary, What I want it to do is, ask the first question "are you dumb or smart" and then receive answer and respond, and then ask the second question and receive answer and respond. Thats all lol im training to make an AI. Thank you again in advance!!!!



#include <iostream>
std::string answer;
using namespace std;
int main()
{
cout<< "are you dumb or smart?";
cin>> answer;
if (answer == "dumb")
std::cout<< "I completely agree";
else
cout<<"good to hear";

}
string answar;
{ ***error on this line***
cout<< "If given a, b, or c, what is your favorite letter?";
cin>>answar;
if (answar == "c");
cout<< "the word catastrophe starts with that letter.";
else if (answar == "b")
cout<< "hey, the word bactericide starts with that letter";
else
cout<< "The word apostraphe starts with that letter";
}








oh and by the way, guys, if you guys wouldnt mind answering: what are the different c++s? I saw like c++/.net and other ones. Plz explain if you can.

TYSM in advance!!!! -Michael
Last edited on
the difference between c++ and c++.net is that the .net is microsofts attempt at making all languages one.
you should delete the } before the you declare the string answar.
Hi Yangfizz. But im trying to make it into two seperate parts. Wouldnt deleting that conjoin both of the questions? I want to ask first question and answer, and then ask second question and answer. Since there are two ifs else ifs and elses then wouldnt the build fail? Tysm in advance and thanks for all answers.
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>
void answar(); // moved the semi-colon and added () before
//also changed to "void" from std::string since you're not returning anything
using namespace std;
int main()
{
cout<< "are you dumb or smart?";
cin>> answer;
if (answer == "dumb")
std::cout<< "I completely agree";
else
cout<<"good to hear";
answer(); // function call

return 0; // always return 0 (ERROR_SUCCESS) from main()
}
void answar() // removed semi-colon and added () instead, since you want it be a function
//also changed to void here, same reason
{ ***error on this line***
cout<< "If given a, b, or c, what is your favorite letter?";
cin>>answar;
if (answar == "c");
cout<< "the word catastrophe starts with that letter.";
else if (answar == "b")
cout<< "hey, the word bactericide starts with that letter";
else
cout<< "The word apostraphe starts with that letter";
}


I haven't tested it, and too lazy to fix indentation.
http://cplusplus.com/doc/tutorial/functions/ <- for the function part, they cover it way better than I just did here.
http://cplusplus.com/doc/tutorial/ <- you should start reading here too, to learn more :)

Hopefully I didn't misunderstand what you wanted.

(edit: just saw some things that will be errors, but they aren't that hard to fix and I'm too lazy to fix them)
Last edited on
Yangfizz's solution sounds good to me. Just get rid of that brace. You'd also need to delete the opening brace a couple of lines down from that.

The user will still be asked the first question, then the second.

You could also save a bit of space by reusing the answer variable to store the second input, rather than declaring a new variable.
Last edited on
splux, that didn't work. Because, you didnt declare answer.. Cuz answer and answar are dif variables. What should I do from there?
Yangizz and ihutch, I did that but yet for these two starred lines, it says error: expected expression. ty in advance.

#include <iostream>
std::string answer;
using namespace std;
int main()
{
cout<< "are you dumb or smart?";
cin>> answer;
if (answer == "dumb")
std::cout<< "I completely agree";
else
cout<<"good to hear";


string answar;

cout<< "If given a, b, or c, what is your favorite letter?";
cin>>answar;
if (answar == "c");
cout<< "the word catastrophe starts with that letter.";
else if (answar == "b") ********here it says expected expression error
cout<< "dude, the word bactericide starts with that letter";
else *********here it says expected expression error
cout<< "The word apostraphe starts with that letter";

}
@MW130

Remove the semicolon on the if(answar=="c"); line. It should work okay then.
Why are you two times declaring variable answer: one global and other local?

std::string answer;
using namespace std;
int main()
{
cout<< "are you dumb or smart?";
cin>> answer;
if (answer == "dumb")
std::cout<< "I completely agree";
else
cout<<"good to hear";


string answar;
Last edited on
vlad:
That's why I assumed he tried to make it a function... apparently, I was wrong.

MW130:
I did say that I noticed an error but I was too lazy to fix it :)

And I still don't really know what it is you are trying to do, making it into functions? Having everything inside main()?

Just by looking at your code it looks like you're either trying to use a global variable, or trying to forward declare a function.

Or maybe I'm just stupid that needs a bit more explanation, that's a possibility.
I have no idea what global and local are. I want to make it ask the first question, get answer and then ask second question. Stupid on my part, i didnt know what to name the two so i named answEr and answAr lol diff letter there. I just declared them and made the prog.

Sent from iphone
hi whitenite, i removed semicolon, but it didnt fix lol same problem.
Are there any more solutions plz ? I tryed all of these different things but none worked ;(
@MW130

There were still quite a few errors.

I fixed up the code to run and it has a function. The function had the same name as a variable, and that messes things up. You needed to add #include <string> since you were using a string variable. I also added remarks for the changes/additions, etc.

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
// Answer.cpp : main project file.

#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;

void get_answar(); // moved the semi-colon and added () before
//also changed to "void" from std::string since you're not returning anything

int main()
{
string answer; // Declare the string variable
	cout << "are you dumb or smart?" << endl;
cin >> answer;
if (answer == "dumb")
cout<< "I completely agree" << endl; // Don't really need the std:: since you declared 'using namespace std'
else
cout<<"good to hear" << endl;
get_answar(); // function call.. Was calling unknown routine of answer().
// Changed it to get_answar() since variable AND function had same name
return 0; // always return 0 (ERROR_SUCCESS) from main()
}

void get_answar() // removed semi-colon and added () instead, since you want it be a function
//also changed to void here, same reason
{ //***error on this line***
string answar="";  // Declare the other string variable
cout<< "If given a, b, or c, what is your favorite letter?" << endl;
cin >> answar;
if (answar == "c")
cout<< "the word catastrophe starts with that letter." << endl;
else if (answar == "b")
cout<< "hey, the word bactericide starts with that letter" << endl;
else
cout<< "The word apostraphe starts with that letter" << endl;
}
Topic archived. No new replies allowed.