0

I am very new to C++. My objective is to write the following logic:

if a = yes then print "ok", else return 0

Here is my code so far:

int a;
cin>>a;                         
if (a = "Yes") {   // Error right here
    cout<< "ok"; << endl;
}else{
    return 0;
}
Jonas
  • 6,915
  • 8
  • 35
  • 53
Music Watt
  • 23
  • 2

4 Answers4

0

There are multiple errors in this code.

You need to use the comparison operator in your condition. This is denoted by the double equal sign "==". Your code is using assignment "=" which tries to assign the value "Yes" to the variable a. This is a common error in C/C++ so you need to be careful whenever you compare things.

The other error is that you have declared the variable a to be an integer, so you will get a type mismatch error when you try to compile because "Yes" is a string.

Waqar
  • 8,558
  • 4
  • 35
  • 43
Mike Collins
  • 402
  • 3
  • 13
0

First of all, what do you want your program to do?

You need to distinguish assignment and equal to operator. Please note that you need to understand the basics before proceeding to perform conditional statements.

A reasonable program should go like this:

 int a;
     cin>>a;                         
     if (a == 5) {   // 5 is an integer
         cout<< "You entered 5!" << endl; // no semicolon after "
     }
return 0; // must be out of the else statement
0

= assigns things. Use == to compare, however you are comparing an int with a string. If you are not careful, you compare the address of a char * with a number when dealing with strings. Use a std::string instead.

 #include <string>

 //.... some context I presume

 std::string a;
 cin  >>  a;                         
 if (a == "Yes") {   // Error right here
     cout<< "ok"; << endl;
 }else{
      return 0;
 }
doctorlove
  • 18,872
  • 2
  • 46
  • 62
-2

Your code is incorrect in terms of data types. You have a variable 'a' of type int which you are comparing to string "yes". Try to see it from a logical point of view; you can compare:

  • 2 numbers (for example, 2 is greater than 1)
  • 2 strings (for example, "food" is not the same word as "cat") Etc...

In your case, you are comparing a number inputted(let's assume 5) to a word "yes". When you try to input a letter for var a, you will get a compilation error. Therefore, simply change the following:

    string a;

Another problem with your code is when the if-then loop checks the condition; a comparison operator is 2 equal signs next to each other instead of a single equal sign. A single equal sign assigns the item on the right to the item on the left. For example, in:

    int num = 5;

The variable num is assigned 5. But you want to make a comparison, not assign the variable its own condition!

Your loop is always true because you set the variable to the condition it is supposed to meet. You also need to do the following:

    if (a == "yes")

This compares the value stored in var a to the value on the right side of the == .

Just some advice, I would recommend you to get some good books on c++. Search them online. You can also take online programming courses on edx, course record, etc... . There are a lot of other free learning resources online too which you can make use of. You may also want to dive into a simpler programming language; I would recommend scratch. It gives you a very basic idea about programming and can be done in less than a week.

** Note that I feel this is the simplest way; however, you can also set type of a to a char, accept input and then convert it back to a string. Good luck!

BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31