0

I'm a c++ newbie and have spent half a day googling this. Most posts seem vague or complex. And I have a feeling that there's a simpler way to do this. Any help is appreciated. A simple program that calculates tax based on the state entered by user:

#include <iostream>

int main()
{
    std::cout << "Enter your order amount: ";
    int amount{};
    std::cin >> amount;

    std::cout << "Which state do you reside in? ";
    std::string state{};
    std::cin >> state;

    if (state == "WI") //THIS IS SIMILAR TO HOW I WOULD DO IT IN PYTHON | C++ doesn't work this way
        std::cout << "Your tax is 5.0$" << std::endl;
        std::cout << "Your total is " << amount + 5.0;

    else
        std::cout << "Your tax is 0.0$" << std::endl;
        std::cout << "Your total is " << amount;

    return 0;
}
12
  • Your code is pretty nice for a newby. A lot of info can be found on en.cppreference.com/w/cpp/string/basic_string Commented Nov 3, 2019 at 21:48
  • 5
    yes c++ works this way. You can compare a std::string to another string with the operator == . You however need parentheses around all the things that are in the if statement. In C++ you need to use {} instead of indentation to indicate scope. Commented Nov 3, 2019 at 21:48
  • This has nothing to do with strings, but with proper C++ grammar. C++ is not Python, and indentation is utterly meaningless in C++. You need to use braces. See your C++ book for more information. Commented Nov 3, 2019 at 21:50
  • 1
    @constraintAutomaton welcome to modern C++... en.cppreference.com/w/cpp/language/initialization Commented Nov 3, 2019 at 21:51
  • 1
    @Omnifarious thank you for clarifying that. I was aware that C++ didn't have any rules on indentation. I simply carried over the indentation style from python to make it readable, as you said. Commented Nov 3, 2019 at 23:57

1 Answer 1

4

Note the addition of { and } to group the statements after the if and else

#include <iostream>

int main()
{
    std::cout << "Enter your order amount: ";
    int amount{};
    std::cin >> amount;

    std::cout << "Which state do you reside in? ";
    std::string state{};
    std::cin >> state;

    if (state == "WI")
    {
        std::cout << "Your tax is 5.0$" << std::endl;
        std::cout << "Your total is " << amount + 5.0;
    }
    else
    {
        std::cout << "Your tax is 0.0$" << std::endl;
        std::cout << "Your total is " << amount;
    }

    return 0;
}

After an if (condition) or else the language only allows a single statement. The braces { and } enclose a sequence of statements into a Compound Statement.

For possibly clearer language see Compound statements here: https://en.cppreference.com/w/cpp/language/statements

Sign up to request clarification or add additional context in comments.

1 Comment

Maybe an explanation of the "compound statement" would be good here?

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.