开发者

If conditioning and String [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 9 years ago.

Improve this question 开发者_如何学编程

I am very new at C++ and deeply appreciate your help!

I am trying to make a 'If' condition for a string, that is, for example:

#include <iostream>

using namespace std;

int main()
{
string message = "HELP";
int password;
cout<<"Please enter password";
cin<<password;
if (password = message);
}
else {
cout<<"Please try again...";
}
cin.ignor()
}

However Int is not for strings I believe and of course on Code::Blocks it posts the error that it doesn't function in such case. So basicaly just when someone on C++ saves a variable for int X; X = 3; for example, how can we do that with letters so that I can pop if condition message boxes!

Again thanks for helping! =D


= is assignment. == is comparision. Also, don't put semicolon after the if statement.


#include <iostream> 

using namespace std; 

int main() 
{ 
   string message = "HELP"; 
   string password; 
   cout << "Please enter password"; 
   cin >> password; 
   if (password != message) 
   { 
      cout << "Please try again..."; 
   }
   return 0; 
}  

Should work a little better.


This is the first problem:

int password;

The data type of password should be std::string as well, because message is std::string (which contains the valid password).

So the first fix is this:

std::string password;

Second problem is that you're using '=' in if. Use '==' (equality operator), not '=' (assignment operator).


First off, if you want the password to be anything, not just a number, use std::string. To compare two values, use == NOT =.

#include <string>
#include <iostream>

int main()
{
    std::string s1("First string");
    std::string s2("Second string");

    if(s1 != s2) {
        std::cout << "Strings don't match!" << std::endl;
    }
}

In your code, you also didn't properly close all blocks and misspelled cin.ignore().


What you probably want to do is:

#include <iostream>

// using namespace std; don't do this (its just lazy)
// Also its a bad habit to get into.

int main()
{
    std::string message = "HELP";

    // int password;  I assume you want a password to be a string
    std::string password;

    std::cout << "Please enter password"\n; // Added \n to make it print nicely.
    // cin<<password; The << is wrong should be >>
    //
    //                Bu this reads a single word (separated by spaces).
    //                If you want the password to hold multiple words then you need
    //                 to read the line.
    std::getline(std::cin, password);


    // if (password = message);  This is assignment. You assign message to password.
    //                           Also the ';' here means that nothing happens 
    //                           if it was true
    //
    //                           You are then trying to test the result of the 
    //                           assignment which luckily does not work.
    //                           Use the '==' to test for equality.
    if (password == message)
    {
    }
    else
    {
        cout << "Please try again...";
    }

    // cin.ignor()  This does nothing.
    //              What you are trying to do is get the program to wait 
    //              for user input. You should do something like this:

    std::cin.ignore(std::numeric_limits<std::streamsize>::max()); // Flush the buffer
    std::cout << "Hit enter to continue\n";
    std::cin.get(); 
}


You used the wrong operator on the if condition. You used = which is the assignment operator. You need to use == which is comparison. You also have a semi-colon after the if which doesn't belong and your braces for the statements aren't right,

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜