hi! smart people. It is my 11 days of reading and studying this book turbo c/c++, and I'm almost in the last part of the book. It is fun learning computer programming using c++.

I'm having problem on how to apply the principles, that i've learned,so I need an example.

Here is my request about an exapmle program for a password, if it is the correct pasword then password accepted else not. If anyone could help me. your highly appreciated.

I already started some but it wont work.


Thank you

>>I already started some but it wont work.
Can't help you if you don't show us your code.

Anyway Ancient dragon I thank you. Here is my code.

#include <iostream.h>
main()
{
char pass
char word []="pope" // I assumed this is the setted password
cout << "Enter Password:";
cin >> pass;
if ( pass==word) //I'm stablishing a condition here,but it doesn't work   
    cout << "Password Accepted";
else
    cout << "Password Denied";
return 0;
}

Anyway Ancient dragon I thank you. Here is my code.

#include <iostream.h>
main()
{
char pass
char word []="pope" // I assumed this is the setted password
cout << "Enter Password:";
cin >> pass;
if ( pass==word) //I'm stablishing a condition here,but it doesn't work   
    cout << "Password Accepted";
else
    cout << "Password Denied";
return 0;
}

Did you copy and paste directly because you are mising a semicolon on line 4, it should be char pass; even then it's only one character so if you tried to input pope and then compare it the comparison would be if ('p'=="pope") because it will only take in the first character and dump the rest.

Also main() should be int main() or int main(void) since it is returning an int and that is the only ISO C++ way to do it aside from int main(int argc, char *argv[]) which you don't have to worry about.

A better way to do it would be to use strings, also take out the .h since it antiquates the header:

#include <iostream>
#include <string>
using namespace std;
int main(void)
{
    string password = "pope";
    string input;
    cout<<"Enter a word: "<< endl;
    cin>> input;
    if(input==password)
        cout<<"Welcome!"<< endl;
    else
        cout<<"Sorry, the password you entered is incorrect."<< endl;
    cin.get();
    cin.get();
    return 0;
}
int main(void)

In C++ this is considered bad style. In fact, the main(void) style has been called an "abomination" by Bjarne Stroustrup, the creator of C++, Dennis Ritchie, the co-creator of C, and Doug McIlroy, head of the research department where Unix was born.

i n t f (v o i d ); // error: abomination

That has too be the funniest error I have seen aside from "Stopping compilation, too many errors. There must be something terribly wrong with your code."

Anyway Ancient dragon I thank you. Here is my code.

#include <iostream.h>
main()
{
char pass
char word []="pope" // I assumed this is the setted password
cout << "Enter Password:";
cin >> pass;
if ( pass==word) //I'm stablishing a condition here,but it doesn't work   
    cout << "Password Accepted";
else
    cout << "Password Denied";
return 0;
}

Instead write this code

#include <iostream.h>
#include<string.h>
main()
{
char pass
char word []="pope" // You assumed this is the setted password
cout << "Enter Password:";
cin >> pass;
if (strcmp(pass,word)==0) 

/*Just comparing strings by '=='  operator dosen't work it only works for numbers.... strcmp() defined in string.h compares each and every alphabet and returns 0 if the characters are equal eg. if it compares 'A' and 'A' (ASCII=65) it will return 0 cause 65-65=0. comparing 'A'(ASCII=65) and 'Z'(ASCII=91) it will return 26 cause 91-65=26!=0*/

    cout << "Password Accepted";
else
    cout << "Password Denied";
return 0;
}
Comments
Way too late with the unformatted and poor style answer - better answers already posted.

But using C-style strings and strcmp() in a C++ program just sucks when a perfectly reasonable solution using std::string has already been posted.

Yes you can use == when you're using a std::string.

But using C-style strings and strcmp() in a C++ program just sucks when a perfectly reasonable solution using std::string has already been posted.

Yes you can use == when you're using a std::string.

sorry i have not read strings yet so didnt know...
can you tell me more about that??

thanx...

sorry i have not read strings yet so didnt know...
can you tell me more about that??

thanx...

Then wait until you read about strings.

Until then use strcmp() or strncmp() which works for your definitions.

This article has been dead for over six months. Start a new discussion instead.