mercredi 25 mars 2015

How do I use an if statment to check if a string literal is a certain word or phrase

I am still new to coding and was playing around trying to figure out simple conversations and i get the following errors while compiling:


error: could not convert 'str.std::basic_string<_CharT, _Traits, _Alloc>::operator=, std::allocator >(((const char*)"good"))' from 'std::basic_string' to 'bool' if (str = "good") {


and


error: could not convert 'str.std::basic_string<_CharT, _Traits, _Alloc>::operator=, std::allocator >(((const char*)"bad"))' from 'std::basic_string' to 'bool' else if (str = "bad") {


I get those errors from the Following code; Bare in mind I'm still very new to this:



// random practice on conversation
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string str;
string bad;

cout << "How has your day been? \n";
cin >> str;
if (str = "good") {
cout << "Thats good to hear!\n";
}
else if (str = "bad") {
cout << "That's too bad, what happened? \n";
cin >> bad;
cout << "I'm sorry to hear that...\n";
}
else {
cout << "I'm sorry, I couldn't understand you...\n";
}
}

Aucun commentaire:

Enregistrer un commentaire