954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help C++ my program!!!

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..

[php]
#include//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

}
[/php]

lotsofsloths
Light Poster
44 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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!!??!!??

lotsofsloths
Light Poster
44 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 
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 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;
}
Ravalon
Posting Whiz in Training
213 posts since Dec 2006
Reputation Points: 84
Solved Threads: 15
 

old compilers dont support youll need to make a character array

jbennet
Moderator
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 601
 
old compilers dont support youll need to make a character array


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

Ravalon
Posting Whiz in Training
213 posts since Dec 2006
Reputation Points: 84
Solved Threads: 15
 

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

jbennet
Moderator
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 601
 
not really, i have MS Visual C++ 5 and it supports iostream but not string


That's good to know, thanks. :)

Ravalon
Posting Whiz in Training
213 posts since Dec 2006
Reputation Points: 84
Solved Threads: 15
 

jbennet get yourself devshed.

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

Cheers.

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

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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

jbennet
Moderator
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 601
 

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

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

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
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.

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 
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? ;)

Ravalon
Posting Whiz in Training
213 posts since Dec 2006
Reputation Points: 84
Solved Threads: 15
 
Maintaining legacy code written with those crap compilers? ;)


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

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 
I don't want to think about it. :cheesy:


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

Ravalon
Posting Whiz in Training
213 posts since Dec 2006
Reputation Points: 84
Solved Threads: 15
 
Me neither, but I still end up doing it. ;)

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

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 
If lotsofsloths' compiler supports then it supports or needs to be thrown away. ;)

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

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You