-6

I'm beginning c++, and I want to do if statements with strings, but how.

#include <iostream>
using namespace std;

int main()
{
  int a;

  cout << "Do you like the food?"<< endl;
  cin>> a;

  if (a == "yes"){
      cout<<"Thank you!"<<endl;
  }

  if (a == "no"){
    cout << "That's mean!"<<endl;
  }

  return 0;
}

What do you think I should do? I like experimenting when learning a new programming language, but most of the time I can figure out how to do what I want to do, but my streak is up and I now need help. So, how do I do this properly? I think I should be able to do this as with some of the ideas I have involve more things like this, so please help me out here.

Thanks in advance!!!

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • 14
    You declared `a` to be `int`. If you want a string, maybe use `std::string a;`? (you need `#include ` as well, of course) – Daniel Frey Sep 30 '13 at 07:26
  • `using namespace std` is usually considered [bad practice](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Cyrille Sep 30 '13 at 07:30
  • 1
    @user2801154: To do things properly, how about reading a book instead of cruising yourself? – legends2k Sep 30 '13 at 07:30
  • 3
    What's with the -1's people? Just because he's a beginner? And not a single comment explaining why the down votes. – Asaf Sep 30 '13 at 07:32
  • 1
    @Asaf Downvotes are because this question clearly shows no research effort. – undu Sep 30 '13 at 07:44
  • 1
    @undu And how exactly will this user learn from the downvotes without anyone telling him that? – Asaf Sep 30 '13 at 08:03
  • @asaf By looking at the tooltip in the downvote button (although _that_ would be some research effort). – Daniel Daranas Sep 30 '13 at 08:36
  • @Asaf You're totally right, but am I the one to blame ? I didn't downvote the question and still explained why there are downvotes. – undu Sep 30 '13 at 08:59
  • @undu Not blaming :) just explaining my POV – Asaf Sep 30 '13 at 09:26
  • 1
    @DanielDaranas That's a hell of a way to welcome new users. People can learn from their mistakes if you give them a chance. Not everyone has spent years in SO and knows how and when questions should be asked. Please don't make SO an elitist website for experienced users only. – Asaf Sep 30 '13 at 09:31
  • @Asaf To improve that experience you can always point them to the Help Center. I will do it now. – Daniel Daranas Sep 30 '13 at 09:41
  • 1
    Hello, @user2801154, and welcome to SO. Please visit the [Help Center](http://stackoverflow.com/help), especially the [Asking](http://stackoverflow.com/help/asking) section. – Daniel Daranas Sep 30 '13 at 09:46
  • @Asaf agreed, it took me a minute to get used to the Stack Overflow format. In fact, no one knows if the OP is a 13-year-old who is just interested in learning how to code. I know for a fact that I had about 0 experience when I was that young, so maybe it's worth taking the extra 30 seconds to give an insightful answer instead of drive-by downvoting because you think they made no effort (doesn't seem like they did, I agree). However, it may be that they don't know how, or where to look for the correct usage of data types. Keep an open mind, a least that's my thought. – Chris Cirefice Oct 13 '13 at 21:37

1 Answers1

5

The type of a should be std::string and not int.

So just change the declaration to:

std::string a;
Asaf
  • 4,317
  • 28
  • 48
  • In conjunction with Asaf's answer, I would like to include the following two pages for reference on data types, just so that you can gain an understanding of how they work in general, as well as specifically in C++ (since that's what you're learning). I believe that [this website](http://pc.net/helpcenter/answers/primitive_and_non_primitive_data) is a good starting point for general primitive data types (used in all languages), and [this website](http://www.cplusplus.com/doc/tutorial/variables) is good for learning C++ data types and how to use them in the context of the language. – Chris Cirefice Oct 13 '13 at 21:44