whenever i run the below program it asks me to type in a password and it should go through all that could possibly be the password but if i type in a password of 2 or more digits it only displays the first letter of the password.

#include <iostream>

using namespace std;

int main()
{
    static char a;
    a = 'A';
    
    char password;
    cout << "type in password: ";
    cin >> password;
    
    while (a != password)
    {
          a = a + 1;
          cout << a << "\n";
}
    

system("PAUSE");
return 0;
}

Replace char password; with char password[31]; And change cin >> password; to cin.getline(password, 30); If you declare password like char password then you tell your compiler to reserve memory for one character only, but we want a set of characters (a string, an array of characters), so we write something like: char password[31]; (this string will be capable of holding 30 characters (plus the null-terminator))

BTW, Read my remark on the use of system("pause"); (it's in my signature)

variable password is only a single character, which means it will accept only one character. If you want it to accept many more characters then declare it as an array of characters, or a std::string object, like this: char password[255] = {0}; . That lets you type up to 254 characters as the password.

[edit]^^^ I'm too late :) [/edit]

commented: But your info is excellent! +7

now i get this error on line 14: ISO C++ forbids comparison between pointer and integers"

this is the code:

#include <iostream>

using namespace std;

int main()
{
    char a;
    a = 'A';
    
    char password[31];
    cout << "type in password: ";
    cin.getline(password, 30);
    
    while (a != password)
    {
          a = a + 1;
          cout << a << "\n";
}
    

system("PAUSE"); //cin.get()
return 0;
}

>14: ISO C++ forbids comparison between pointer and integers
Yep, the error message is correct, since the name of an array can be treated as a constant pointer to the first element of that array.

How can we avoid this?
Well, can you first tell us what your program is intended for?

it asks the user to type in something, then it goes through everything the user could have possibly typed in then it prints it to the screen. i know theres no point for it but its just pracice.

it asks the user to type in something, then it goes through everything the user could have possibly typed in then it prints it to the screen. i know theres no point for it but its just pracice.

But if you start with an uppercase 'A' then you won't process all the possible things he could have typed in, by the way, this could take a very very long time, if you want to check all possible combinations (and you're doing something else wrong as well), I would try something else :)

>> while (a != password)

Just for completeness, the above line should be written like this: while( password[0] != a ) That will remove the error, but not the logic of why you would want to code something like that. What about all the other characters the user typed?

If you want to compare the password you type with some pre-determined password then do something like this:

char password[31] = {0};
do
{
    cout << "Enter password\n";
    cin.getline(password, sizeof(password));
} while( strcmp(password, "abcdefg") != 0);

>If you want to compare the password you type with some pre-determined password then do something like this
Correct, but the strange thing is that he doesn't want to do that :D

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.