Ok i just started c++ with no knoledge about any other programming language, so bear with me.
I am trying to make a simple program that you enter a password
and if it is correct, it says welcome. If it is wrong, then it says try again. Simple? Well when ever i type anything it says welcome,here is my code, see if you can find out whats wrong please..

#include<iostream>//structure
 
usingnamespace std;//structure
 
int main ()//structure
 
{
int password;//declaring variables
 
int pass;//the password to type
cout << "Please Enter The Password:";//telling to enter password
 
cin >> password;//input
 
 
if (password==pass)//if else statment
 
cout << "Welcome!";
 
else
 
cout << "Try Again.";//if else statment
 
 
return 0;//terminate program
 
}

Recommended Answers

All 16 Replies

Passwords are normally a string of characters, which could be almost any character you can type on the keyboard. An int is an integer that holds numeric information only. If you just want to type numbers and not letters then an int will probably be ok unless you type a number that is too big.

In your program, what is the difference between variables pass and password ? After entering password you are comparing it to an uninitialized variable pass. I guess pass is the correct password the user is supposed to enter, but it is never initialized to anything so it just contains some random number.

Yes pass is the password the user is supposed to enter.
how do i now change int into something that can hold a letter value?
OK, so i know that "char" is for non-numerical values...
and i replaced the ints, with chars...
But still whenever i run it and type in an invalid password,
it still says welcome!!??!!??

Yes pass is the password the user is supposed to enter.
how do i now change int into something that can hold a letter value?

There's a string type in the <string> header that's pretty intuitive to use.

#include <iostream>
#include <string>

using namespace std;

int main()
{
  string password;
  string pass = "letmein1";

  cout << "Please Enter The Password: ";
  cin >> password;

  if ( password == pass )
    cout << "Welcome!\n";
  else
    cout << "Try Again.\n";

  return 0;
}

old compilers dont support <string> youll need to make a character array

old compilers dont support <string> youll need to make a character array

If lotsofsloths' compiler supports <iostream> then it supports <string> or needs to be thrown away. ;)

not really, i have MS Visual C++ 5 and it supports iostream but not string

not really, i have MS Visual C++ 5 and it supports iostream but not string

That's good to know, thanks. :)

Member Avatar for iamthwee

jbennet get yourself devshed.

If your compiler don't support <string> you're not learning c++ at all.

Cheers.

btw do you of any good IDEs for c++ and java in linux?

eclipse is a good ide for c++ and java in linux i think....
kdevelop also does c++ i think...

Member Avatar for iamthwee

excellent, now I just gotta try to unpack the damn thing.

Do I have to log on as the root or summat?

Do I have to log on as the root or summat?

Probably, as most large programs require root access for installation. Eclipse requires oodles of RAM, so you may want Kdevelop instead if computer resources are in short supply on your system. Personally, I prefer Anjuta. :)

Yes, and agreed with iamthwee, if a compiler doesn't support string, who knows what else you're missing. There's no excuse for programming with crap compilers when there's so many good free ones out there.

There's no excuse for programming with crap compilers when there's so many good free ones out there.

Maintaining legacy code written with those crap compilers? ;)

Maintaining legacy code written with those crap compilers? ;)

I don't want to think about it. :cheesy:

I don't want to think about it. :cheesy:

Me neither, but I still end up doing it. ;)

Me neither, but I still end up doing it. ;)

Everything in fair in Love, War and a programming job....:D

If lotsofsloths' compiler supports <iostream> then it supports <string> or needs to be thrown away. ;)

if it doesn't support either of them it needs to be thrown away...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.