0

I'm a beginner, so please excuse my silly mistakes.

I'm trying to get a specific output when I input a specific name using strings, but I keep getting an error that my name wasn't declared in the scope. I also want to be able to add different responses for different inputs.

I tried looking up how to use strings, and I tried all sorts of combinations, but I'm still confused as to what I did wrong.

I'm sorry if this doesn't make sense, I'm just really bad at explaining things.

#include <iostream>
#include <cmath>
#include <string>

int main() {

    std::string firstname;
    
    std::cout << "please state your name. \n";
    std::cout << firstname;
    std::cin >> firstname;

    if (firstname == leo) {
        std::cout << "oh, you.";
    }
    
    return 0;
}
2
  • 2
    if (firstname == "leo")? Commented Nov 3, 2022 at 19:22
  • Without quotation marks, leo is interpreted as the name of a variable that has not been defined. Commented Nov 3, 2022 at 19:23

1 Answer 1

3

First, std::cout << firstname; is a no-op since firstname is empty at that point.

Second, there is no name in your code. Is the error referring to leo? That should be wrapped in double-quotes since you meant it to be a string literal, not a variable.

Try something more like this:

#include <iostream>
#include <string>

int main() {

    std::string firstname;
    
    std::cout << "please state your name. \n";
    std::cin >> firstname;

    if (firstname == "leo") {
        std::cout << "oh, you.";
    }
    else {
        std::cout << "hello, " << firstname;
    }
    
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.