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.
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.
#include <iostream>
void answar(); // moved the semi-colon and added () before
//also changed to "void" from std::string since you're not returning anything
usingnamespace 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.";
elseif (answar == "b")
cout<< "hey, the word bactericide starts with that letter";
else
cout<< "The word apostraphe starts with that letter";
}
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";
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";
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.
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.
// Answer.cpp : main project file.
#include <iostream>
#include <string>
#include <stdio.h>
usingnamespace 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;
elseif (answar == "b")
cout<< "hey, the word bactericide starts with that letter" << endl;
else
cout<< "The word apostraphe starts with that letter" << endl;
}