Jump to content

Help me with C++

TECHNICAL GAMER
Go to solution Solved by Eigenvektor,
11 minutes ago, HotLoli42069 said:

changing the variable and adding == operator did work, but now it does not show the output in the condition.

if (anime=="oshi no ko") {
    cout << "Thats great!";
}

please help this little boy.

As I said above, you generally cannot compare strings with == in C++. This condition would only be true if the variable anime and the constant string "oshi no ko" refer to the same instance (which isn't possible here).

 

You need to use something like

if (strcmp(anime, "oshi no ko") == 0) {
    ...
}

which will compare the contents of the strings, rather than their instances.

I have made a simple program, as I am learning c++, but it does not work. I have 3 inputs and only 2 work. Please help.

main.cpp - CPlusPlus - Replit

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name, sum;
    bool yn, anime;
    cout << "Enter your name:\n";
    cin >> name ;
    cout << name << " is a nice name\n";
    cout<< "Do you have watched any animes? Reply in yes & No\n";
    cin >> yn;
    
    if (yn)  "yes"; {
        cout << "Which was your favourite?";
        cin >> anime;
    }

    if (anime="oshi no ko"); {
        cout << "Thats great!";
    }
    if(anime="hi") {
        cout << "Good evening.";
    }
    
    return 0;
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

20 minutes ago, HotLoli42069 said:
if (yn)  "yes"; {

That doesn't look like valid C++, I'm surprised it even compiles. Remove that semicolon before the braces and add some form of comparison here (like ==).

 

if (anime="oshi no ko");

Comparison requires two equal signs, meaning "if (variable == value)". With a single equals sign, you're doing an assignment. And again, remove the semicolon before the braces.

 

On top of that, you can't compare strings with equals, because that compares instances, rather than their contents. So it would only be equal if both strings are the same instance of string. Not if they are the same content wise. You'll need to do something like

if (strcmp(anime, "oshi no ko") == 0) {
   ...
}

 

By adding a semicolon, you're completing the if-statement. You're basically saying: If the condition is true, do nothing. Then you're just adding a code block to execute (regardless of whether the condition is true or not)

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, Eigenvektor said:

==

changing the variable and adding == operator did work, but now it does not show the output in the condition.

if (anime=="oshi no ko") {
    cout << "Thats great!";
}

please help this little boy.

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name, sum, anime,yn;
    
    cout << "Enter your name:\n";
    cin >> name ;
    cout << name << " is a nice name\n";
    cout<< "Do you have watched any animes? Reply in yes & No\n";
    cin >> yn;
    
    if (yn=="yes") {
        cout << "Which was your favourite?";
        cin >> anime;
    

    if (anime=="oshi no ko") {
        cout << "Thats great!";
    }
    if(anime=="hi") {
        cout << "Good evening.";
    }
    }
    return 0;
}

main.cpp - CPlusPlus - Replit

Link to comment
Share on other sites

Link to post
Share on other sites

11 minutes ago, HotLoli42069 said:

changing the variable and adding == operator did work, but now it does not show the output in the condition.

if (anime=="oshi no ko") {
    cout << "Thats great!";
}

please help this little boy.

As I said above, you generally cannot compare strings with == in C++. This condition would only be true if the variable anime and the constant string "oshi no ko" refer to the same instance (which isn't possible here).

 

You need to use something like

if (strcmp(anime, "oshi no ko") == 0) {
    ...
}

which will compare the contents of the strings, rather than their instances.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, Eigenvektor said:

On top of that, you can't compare strings with equals, because that compares instances, rather than their contents. So it would only be equal if both strings are the same instance of string. Not if they are the same content wise. You'll need to do something like

if (strcmp(anime, "oshi no ko") == 0) {
   ...
}

Using == to compare C++ strings is correct since the == operator is overloaded by the std::string class. The strcmp function is required instead of == if the code is using C-style strings, i.e. plain char arrays.

Computer engineering grad student, cybersecurity researcher, and hobbyist embedded systems developer

 

Daily Driver:

CPU: Ryzen 7 4800H | GPU: RTX 2060 | RAM: 16GB DDR4 3200MHz C16

 

Gaming PC:

CPU: Ryzen 5 5600X | GPU: EVGA RTX 2080Ti | RAM: 32GB DDR4 3200MHz C16

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...
On 9/3/2023 at 2:11 AM, HotLoli42069 said:

changing the variable and adding == operator did work, but now it does not show the output in the condition.

if (anime=="oshi no ko") {
    cout << "Thats great!";
}

please help this little boy.

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name, sum, anime,yn;
    
    cout << "Enter your name:\n";
    cin >> name ;
    cout << name << " is a nice name\n";
    cout<< "Do you have watched any animes? Reply in yes & No\n";
    cin >> yn;
    
    if (yn=="yes") {
        cout << "Which was your favourite?";
        cin >> anime;
    

    if (anime=="oshi no ko") {
        cout << "Thats great!";
    }
    if(anime=="hi") {
        cout << "Good evening.";
    }
    }
    return 0;
}

main.cpp - CPlusPlus - Replit

 

The issue is that you are using cin << for input which is tokenized by spaces. Even if I enter "oshi no ko" it will only read "oshi" as my input (trying echoing your input with cout). The comparison will fail regardless if you use the overloaded == operator or strcmp.

 

You need buffered input https://en.cppreference.com/w/cpp/string/basic_string/getline

 

Pro-tip: don't ever use 'using namespace std'. Just get used to typing std::

#include <iostream>
#include <string>

int main()
{
    std::string name, anime, yn;
    
    std::cout << "Enter your name:" << std::endl;
    std::getline(std::cin, name);
    //std::cin >> name;
    std::cout << name << " is a nice name" << std::endl;
    std::cout<< "Do you have watched any animes? Reply in yes & No" << std::endl;
    std::getline(std::cin, yn);
    //std::cin >> yn;
    
    //try commenting out the std::getline and uncommenting the std::cin lines above
    //and see what happens.
    //After that uncomment the two lines below and try again
    //std::cin.clear();
    //std::cin.ignore(INT_MAX, '\n');

    if (yn=="yes") {
        std::cout << "Which was your favourite?";
        std::getline(std::cin, anime);

        //when in doubt, echo your input
        //std::cout << anime << std::endl;

        if (anime=="oshi no ko") {
            std::cout << "Thats great!" << std::endl;
        }
        if(anime=="hi") {
            std::cout << "Good evening." << std::endl;
        }
    }

    return 0;
}

 

main(i){for(;i<101;i++)printf("Fizz\n\0Fizzz\bBuzz\n\0%d\n"+(!(i%5)^!!(i%3)*3)*6,i);}

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×