-4

here is what I attempted to throw together, unfortunately it's not doing what I want it to. What I want it to be doing is checking the entered text vs a few words that I consider correct. So, for example, if I want the only correct answers to be "thanks" or "please", how would I make the program check if the word the user entered is either "thanks" or "please"?

I have a feeling I can't just write B == 'funs etc.

help me out please:

#include <iostream>
using namespace std;

int main ()
{

string B;

for (;;)
{cout << "enter text here" << '\n' ;
cin >> B ;

if (B == 'fUNS'|| B == 'funs' || B == 'funzies')
{
    cout << "correct!!!!!!" << endl;

    break;
}

else
     {
    cout << "sorry, please try again" << endl;

    continue;


}
}
return 0;
}
  • 2
    1) Define what you mean by "_not working_". Such statement, on its own, carries no meaning. 2) `char` can only hold a single `char`, not a sequence of them. Did you intend to use `std::string`? – Algirdas Preidžius Jul 10 '18 at 06:59
  • @AlgirdasPreidžius what I mean is it doesn't seem to compare the words entered vs the words I wrote in the If statement. I want it to be able to check if the word entered is one of the three words I wrote in the If statement. – gaybowflex Jul 10 '18 at 07:01
  • @AlgirdasPreidžius Yes, if string would allow me to take the input and evaluate it as one string of characters then I should have used that. But how would I make it so that it checks the entirety of the text entered against a few words that I consider to be correct. – gaybowflex Jul 10 '18 at 07:03
  • Again, `char` have no capacity to store words. It can only store a single character. Did you mean to use `std::string`? Consider learning from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead of coding randomly. You can't learn C++ by trial and error. – Algirdas Preidžius Jul 10 '18 at 07:04
  • *"I have a feeling I can't just write `B == 'funs'`"* -- see [string literal](https://en.cppreference.com/w/cpp/language/string_literal) – Joseph D. Jul 10 '18 at 07:07
  • @AlgirdasPreidžius Sorry I'm about 2 weeks into my C++ class and I'm just going through the textbook on my own. I will come back to trying to do this in the future. Thanks anyway – gaybowflex Jul 10 '18 at 07:09
  • 1
    @codekaizer thank you I am looking into that now! – gaybowflex Jul 10 '18 at 07:15

2 Answers2

2

Unlike some languages using ' or " to enclose a sequence of characters produces very different results. A single quote defines a single character literal e.g:

char a = 'A';

You can use multiple characters to define the value of an integer (although this is non-standard):

int a = 'ABCD';

A double quote defines a string literal which is a sequence of characters in an array:

const char str[5] = "ABCD";

Note the literal has a hidden null character at the end which is why it has 5 elements rather than 4. String literals are comparable and assignable with std::string:

std::string test( "ABCD" );
std::cout << test == "ABCD";
test = "EFGH";
std::cout << test == "ABCD";
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
1

I have a feeling I can't just write B == 'funs etc.

Yes, you can, since B is a std::string, which has an operator== defined. You just need to use " (which is used to define string literals) instead of ' (which is used to define character literals), eg:

if (B == "fUNS" || B == "funs" || B == "funzies")
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770