need help with if and else if

Questions regarding game mechanics and graphic programming should go here.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

need help with if and else if

Postby lestatx2 » Fri Jun 19, 2009 1:57 am

ok so i am almost completely new to c++, and i am just messing around with some code to learn, so here is the problem. it keeps going to the password is incorrect even when you type in the correct password, here is the code
Code: Select all
#include <iostream>

int main() {
char password[50];
int x;
x = 1;
while ( x = 1 ){
std::cout<< "enter password ";
std::cin>> password;
if(password == "lolcat"){
   std::cout<< "yay you got the password!!!!!"<<std::endl;
   x = 2;
}
else if (password != "lolcat") {
   std::cout<< "thats not the correct password, you suck!!"<<std::endl;
}
}
system("PAUSE");
    return 0;
    return EXIT_SUCCESS;}
lestatx2
 
Posts: 1
Joined: Fri Jun 19, 2009 1:53 am

Re: need help with if and else if

Postby Alvaro » Fri Jun 19, 2009 6:15 am

while ( x = 1 ){

That's an assignment... Doesn't your compiler warn you about it? g++ does if you use option `-Wall' (which you should).
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Re: need help with if and else if

Postby Lawand » Fri Jun 19, 2009 8:45 am

@lestatx2, as Alvaro said,
while ( x = 1 ){

Is an assignment so you sould replace it with:
while ( x == 1 ){
What is my signature?
User avatar
Lawand
 
Posts: 33
Joined: Fri Mar 13, 2009 1:44 pm

Re: need help with if and else if

Postby Alexanders01 » Sat Jun 20, 2009 3:01 pm

C and C++ do not really provide the feasible option of comparing two C-style strings by just using the '==' operator. I'm sure a book or one of the fine persons on this site can give a much better explanation of exactly why that is, but here are a couple of things you can do in the meantime to make your code work.

I do not know how much you have fiddled with the C++ string class, but it offers some good string comparison and manipulation functions that can be incredibly useful. One such function:
Code: Select all
strcmp( const char *_str1, const char *_str2 )

Takes two null-terminated strings and compares them. It returns 0 if they are identical, < 0 if string1 is less than string2, and > 0 if string1 is greater than string2. You could use that in this fashion:
Code: Select all
#include <iostream>
#include <string>  // You must include the string header to use strcmp()

int main()
{
   char password[50] = "lolcat";  // The word is stored in the variable this time
   char guess[10];  // The user's guess is stored here for comparison

   while( 1 )  // The way I use while loops, but it does not matter so much here
   {
      std::cout << "enter password ";
      std::cin >> guess;
      // If the function returns 0, they are identical
      if( strcmp( password, guess ) == 0 )
      {
         std::cout << "yay you got the password!!!!!" << std::endl;
         break;  // Break out of the while loop
      }
      // Else if the function does not return 0, they are not identical
      else if ( strcmp( password, guess ) != 0 )
      {
         std::cout << "thats not the correct password, you suck!!" << std::endl;
      }
   }
   system("PAUSE");
   return 0;
   return EXIT_SUCCESS;
}

An alternate way that you could utilize the string header would be to use the C++ string class, since it actually does offer direct comparison with the '==' operator. A nice quality of the string variable is also that you do not need to state how long the array is expected to be. That means that theoretically you could store any length character array in the string you need; essentially, you tell it first what you want it to be, and then it determines all on its own how long it needs to be to do what you want.
Code: Select all
#include <iostream>
#include <string>  // You must include the string header to use the string object

int main()
{
   // Here are our new string variables; notice there is no pre-determined length
   std::string password = "lolcat";
   std::string guess;

   while( 1 )
   {
      std::cout << "enter password ";
      std::cin >> guess;

      if( guess == password )
      {
         std::cout << "yay you got the password!!!!!" << std::endl;
         break;
      }

      else if ( guess != password )
      {
         std::cout << "thats not the correct password, you suck!!" << std::endl;
      }
   }
   system("PAUSE");
   return 0;
   return EXIT_SUCCESS;
}

I hope that helps. If you have any other questions, feel free to ask.
Alexanders01
 
Posts: 102
Joined: Wed Jun 25, 2008 11:20 am

Re: need help with if and else if

Postby MXP » Mon Jun 22, 2009 1:20 pm

Nitpick: strcmp() lives in <cstring> not <string>. <string> provides the std::string class while <cstring> provides the string functions from the C standard library. I dont believe <string> necessarily includes <cstring> and so you shouldnt rely on it doing so.
Need information on a function I've posted? Chances are it's at the MSDN.
MXP
 
Posts: 6506
Joined: Mon Sep 22, 2003 5:27 pm

Re: need help with if and else if

Postby Alexanders01 » Mon Jun 22, 2009 2:16 pm

Oh, good call. My mistake. I let myself get too used to things like that sometimes. Thank you for pointing it out.
Alexanders01
 
Posts: 102
Joined: Wed Jun 25, 2008 11:20 am

Re: need help with if and else if

Postby OverRated » Sat Jul 04, 2009 3:46 am

Just so you know this isnt a good way to secure a program XD can be cracked with note pad lol
OverRated
 
Posts: 41
Joined: Wed Oct 01, 2008 12:05 pm

Re: need help with if and else if

Postby Alexanders01 » Sat Jul 04, 2009 9:15 am

Security is an entirely different monster to tackle. If you are just new to C++, then I would suggest not thinking too much about it and just focusing on the basics.
Alexanders01
 
Posts: 102
Joined: Wed Jun 25, 2008 11:20 am


Return to Games and Graphics

Who is online

Users browsing this forum: No registered users and 0 guests