Question:

This C++ code wont work!!!?

by  |  earlier

0 LIKES UnLike

Im a begginer to c++ and im using this code

#include <iostream>

using namespace std;

int main()

{

int question;

cout<<"Would you like to learn c++???: ";

cin>> question;

cin.ignore();

if (question == yes) {

cout<<"Then ask windows how!\n";

}

else if (question == no) {

cout<<"Fine!!!\n";

}

else {

cout<<"are u that stupid???\n";

}

cin.get();

}

and i get the error `yes' undeclared (first use this function)

and `no' undeclared (first use this function)

 Tags:

   Report

4 ANSWERS


  1. include &lt;iostream&gt;

    using namespace std;

    int main()

    {

    int answer = -1;

    cout&lt;&lt;&quot;Would you like to learn c++???: &quot;;

    int yes = 1 ;

    int no = 0 ;

    cout&lt;&lt;&quot;Enter 1 for YES  OR 0 for NO &quot;;

    cin&gt;&gt; answer ;

    cin.ignore();

    if (answer  == yes) {

    cout&lt;&lt;&quot;Then ask windows how!\n&quot;;

    }

    else if (answer  == no) {

    cout&lt;&lt;&quot;Fine!!!\n&quot;;

    }

    else {

    cout&lt;&lt;&quot; You did not answer  the question well...\n&quot;;

    }

    cin.get();

    }

    //please don&#039;t call you program user a stupid !


  2. Put quotes around yes and no.

    if (question == &quot;yes&quot;) {

    else if (question == &quot;no&quot;) {  

  3. the statement

    if question == yes

    question is integer, yes is not declared

    you can add following lines in your code

    int question; //already exists

    int yes = 1;

    int no = 2;

  4. The way your code is written, yes and no are variables, and you haven&#039;t declared them.  (Just what your compiler says.)

    And you declared question to be an int, so you&#039;d better be expecting the input to come in as an int or you&#039;ll never match.

    You could instead put the yes and no in quotes so that you are trying to match a text string, but you still have the problem with question being an int.  Never gonna match.

    You either need question to be a text string, or you need to let your user know that he needs to input an integer.

    You need to tell your user how to answer.  It&#039;s pretty common to see something like

    cout &lt;&lt; &quot;\nWould you like to learn c++ (1=yes, 0=no)? &quot;;

    which implies that the answer is 1 or 0

    Lots of other checking you could do, but gives you the idea.  And usually you don&#039;t tell the user he&#039;s stupid, you gently say something like &quot;Answer must be 1 or 0, please try again&quot; and loop back.

Question Stats

Latest activity: earlier.
This question has 4 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.