I"M A NEWBIE...help me

Why is it allways says "WRONG" at the firsttime eventhough i write the correct password...help me:) hjx hjx.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 #include<iostream>
#include<cstring>
using namespace std;
int main(){
	
char password[]="phongvants1234";
char password2[50];


cout<<"WRITE PASSWORD=";cin>>password2;

			if
	(password==password2){cout<<"DO IT YEAH";}			
			
			else
	{
		do{cout<<"WRONG"<<endl<<"WRITE PASSWORD=";cin>>password2;}
		while(strcmp(password2,password)!=0);cout<<"DO IT YEAH";                                             
	}

}
line 13 - use strcmp instead of == to compare the two cstrings (like you do on line 18).
To elaborate on what wildblue says:

In line 13, password and password2 are both character arrays. This means that in the comparison password==password2, you're comparing the memory addresses of the starts of those arrays. Obviously, these will never be the same, even if the contents of the arrays are the same.

As wildblue says, use the C standard library strcmp function to compare the strings.

(Even better would be to use the C++ std::string class.)
Last edited on
Use std::string

In the example below (which I'm sure could be improved) I tried to avoid having the same cin or the same cout or the same string comparison in more than one place.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>

using namespace std;

int main()
{
    const string password = "phongvants1234";
    string password2;
 
    int  count = 0;
    bool valid = false;
    
    while (!valid && count < 3)
    {
        if (count++ > 0)
            cout << "WRONG\n";
        
        cout << "WRITE PASSWORD="; 
        cin  >> password2;
        
        if (password == password2)
            valid = true;
    } 
    
    if (!valid)
    {
        cout << "Too many attempts";
        return 0;
    }
    
    cout << "DO IT YEAH";
    
}
YOU GUYS REALLY MAKES MY DAY :)....THANKS ALL

ESSPECIALY WANA SAY THANKS TO "MIKE BOY" AND "WHILE BLUE" FOR DETAILS :)

"CHERVIL" YOUR CODE IS TOO COMPLICATED FOR ME TO UNDERSTAND... BUT I WILL DISCOVER IT IN FUTURE...
ANYWAY I REALLY APPRECIATE YOUR CODES,VERY DETAIL :)
You need to compare characters. You can create loop like
1
2
3
4
5
int i=0;
for (; i<14; i++) 
{
...
}

and then use i to access each character in the array. You need to stop the loop when you reach the end of the shortest string.

Find more examples when you google: compare char array in c
(char array used in C is not the same as std::string used in C++)
Last edited on
Here, i fixed your code, keep it cleaner next time ;)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char password[]="phongvants1234";
char password2[50];
cout<<"WRITE PASSWORD=";
cin>>password2;
if(strcmp(password,password2)==0)
    cout<<"DO IT YEAH";
else
{
    do
    {
        cout<<"WRONG"<<endl<<"WRITE PASSWORD=";
        cin>>password2;
    }while(strcmp(password2,password)!=0);
    cout<<"DO IT YEAH";
    }
}
i love u all....but i got a big prob
i need it to say "OK"...but it always say"A"...please help me


#include<iostream>
using namespace std;
int main(){
system("color e9");

bool a=true;
int x;


cout<<"x= ";cin>>x;

         

         while(a){
															
                       if(x=5){cout<<"A";}
																
                       else{a=false;}
 																
                      }
				
cout<<"OK";}
Last edited on
if(x=5){cout<<"A";}. Replace it with if(x==5){cout<<"A";}

Alright now let me explain.

x=5 makes x become 5;
while x==5 is either true or false.
The first one is an expression and the second one is a comparison. ;)
Giving you the same tip, format your code better, avoid lots of blank lines and use tabs properly. Here is your fixed code, I hope this helped you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#include<iostream>
#include <stdlib.h> //for the system() thing you got there.
using namespace std;
int main(){
system("color e9");

bool a=true;
int x;
cout<<"x= ";
cin>>x;
         while(a){									
                       if(x==5)
                            cout<<"A";
                       else
                            a=false;												
                      }	
cout<<"OK";
}

Also in ifs with just one instruction curly brackets {} are not neccesary. Also, if you still have any issues you can ask me personally on discord: https://discord.gg/rwhEXqJ
Have fun coding!
Also in ifs with just one instruction curly brackets {} are not neccesary.

They're not necessary, but I'd advise using them anyway. It's very, very easy to introduce a bug into your code by changing an if block to include a second statement, and forgetting to add the braces. Putting the braces in even when you only have one line, protects you from making that mistake in the future.
See also duplicated topic discussed in this thread:
http://www.cplusplus.com/forum/beginner/220381/
is it ok if i post duplicated topic...is it ban ? i acttually have not known it
Last edited on
If it was up to me, I'd ban you for the braindead title.
It's not "banned" as such; I doubt the admins would take any action over it.

But it's a stupid thing to do. It wastes everyone's time, because people answer in one thread, not knowing that someone else has already answered in another. Also, it fragments the discussion across two threads, meaning you're less likely to get the best answer.

Why on earth would you think it was a good thing to do?
Last edited on
MikeyBoy wrote:
Putting the braces in even when you only have one line, protects you from making that mistake in the future.

IMHO, these words should be set in stone. Correcting a code where the programmer skimped on braces can become a nightmare.
> Correcting a code where the programmer skimped on braces can become a nightmare.

Writing correct code is much simpler when we use all the support we can get from tools.
Use an editor that can auto indent, compile at high warning levels, and you have a much better chance of writing correct code than if you rely on whole lot of silly code formatting rules.

1
2
3
4
5
6
7
8
#include <iostream>

int main( int argc, char* [] )
{
    if( argc == 1 ) 
        std::cout << "foo\n" ;
        std::cout << "foo foo\n" ;
}

main.cpp: In function 'int main(int, char**)':
main.cpp:5:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
     if( argc == 1 ) 
     ^~
main.cpp:7:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
         std::cout << "foo foo\n" ;
         ^~~

http://coliru.stacked-crooked.com/a/0b829c82f197fe28
Use an editor that can auto indent, compile at high warning levels

These are also useful tools, yes.
whole lot of silly code formatting rules
I’m not a computer :) I find formatting rules helpful for me - maybe I’m silly.
And, yes, I’m a creature of habit and I stuck on my personal set of rule for naming and for indentation, but I wasn’t talking about that; I was just saying, when you need to modify an existing code, if you also need to add a lot of braces, it’s boring. It doesn’t happen only on errors, but even when the code is correct, but you want to add a second line after your if, else, for..., you’ll have to get back and wrap the existing line into the braces. Just boring. Perhaps I shouldn’t have said a nightmare. Sorry.
ok boys. i think this argument depends om the personal opinions....
is it ok if i close that topic ???
> this argument depends on the personal opinions....

Yes.


> is it ok if i close that topic ?

Yes.
Topic archived. No new replies allowed.