Conditional Operations help

#include <iostream>
#include <string>
using namespace std;
int main(){

cout << "You want me to do you a favor? ";
cout <<endl;

string x;
cout << "Please enter the magic word: ";
getline(cin,x);
cout << x <<endl;

cout << "Yes I will do you a favor. ";
cout <<endl;

if ( x != x ){
cout << "No I will not do you a favor! ";
cout <<endl;
}

cout <<endl;

return 0;
}




Running Test 1
Test 1 passed
==================================================
==================================================
Running Test 2


==================== YOUR OUTPUT =====================
0001: You~want~me~to~do~you~a~favor?
0002: Please~enter~the~magic~word:~just~do~it!
0003: Yes~I~will~do~you~a~favor.


=================== MISMATCH FOUND ON LINE 0003: ===================
ACTUAL : Yes~I~will~do~you~a~favor.
EXPECTED: No~I~will~not~do~you~a~favor!
======================================================

Adjust your program and re-run to test.

I am working on this, but am at a deadend for what to do. Can someone help me with this?
What is the "magic word" supposed to be?

In this example, it is please.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

int main(){
    std::cout << "You want me to do you a favor?\n"
              << "Please enter the magic word: ";
        
    std::string line;
    std::getline(std::cin, line);
    std::cout << line << '\n';
    
    if ( line == "please" ) { // if the word "please" was entered
        std::cout << "Yes I will do you a favor.";
    } else { // otherwise
        std::cout << "No I will not do you a favor! ";
    }
}
You cannot do it without assigning the magic word to a string as the following code shows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string magic_word = "whatever";

string x;
cin >> x;

// Compare the input string (in our case 'string x') with the actual key word.
if(x == magic_word)
{
    // the input string matches the key word (int this case "whatever")
    // Do him a favor
}
else
{
    // wrong key
}
Last edited on
Topic archived. No new replies allowed.