A program that can read words (not number)

Hi everyone. I'm new to this forum and also new to the world of C++. Allthough I some knowledge about it. Okay so i'm getting confused in characters in c++. I want to make a program that will read my password and if its correct, it will excute the rest of the code. I will try to give a rough example:

So my password for example is "cplusplus". When i type in "cplusplus", the program would show me "Welcome".

I hope you understand me.
Thanks
I understand. But unfortunatley, I'm having the same problem, too. :(
you would want to read in a string using the std::cin object and test if it matches your word. A quick example would be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

const std::string PASSWORD = "cplusplus";  //define your constant

int main()
{
     std::string Temp;    //this string holds user input
     std::cout << "Enter the secret password: "; //ask for input
     while(Temp != PASSWORD) //run this loop while the password is incorrect
     {
            std::cin >> Temp //this line takes in input from the console 
            //and is stored in the string
     }
     std::cout << "Correct! now executing program...";
     // your code here
     return 0;
}
Last edited on
There are three ways, I recommend you the third, it's usually the best:

1.- Character arrays, like this (One word only, can be done in different ways):
1
2
3
4
5
6
7
8
9
char Array[10];
cin >> Array;
cout << "You entered: \"";
int x = 0;
while(x < 11){
    cout << Array[x];
    x = x+1;
}
cout << "\".\n";


2.- Pointer character strings, may be (VERY) buggy when scanning (Still only 1 word when scanning):
1
2
3
char* String;
cin >> String;
cout << "You entered: \"" << String << "\".\n";


3.- Std strings, you need to #include <string> library and using namespace std; (or use std:: when using string and getline()):
1
2
3
string MyString;
getline(cin, MyString);
cout << "You entered: \"" << MyString << "\".\n";



You can assign and compare strings (and char arrays and pointer strings) like if they were numbers, but with text between " s:
1
2
3
4
5
6
7
string MyString = "Hello everyone!";

if(MyString == "Hello everyone!"){
    cout << "\n Hi!";
}

MyString = "\n Now I'm different!\n";
Last edited on
string should use string::compare to compare strings

string::compare
http://cplusplus.com/reference/string/string/compare/



char array (c string) should use strcmp to compare

strcmp
http://cplusplus.com/reference/clibrary/cstring/strcmp/
It's completely unneccesary. In most cases it's possible to just compare them directly with text between "".
Last edited on
Thanks everyone for your help. I will try everything you said in my program.

However a stupid question, whenever i #include <string> , i get "Unable to open include file "STRING".. any help?

PS: I already have every include files and i use Turbo c++ for programming.
Please don't use Turbo C++, it's outdated. Try downloading and installing Code::Blocks with mingw: http://www.codeblocks.org/downloads/26

For the library problem, you can try <string.h>, <String>, <cstring> and <String.h>. One of those should work, probably string.h, but if in case it doesn't, try the others.
Last edited on
Okay i just installed code blocks but can someone say me how to use it for c++ ?
Open a cpp file or create one, when making a file or project you need to select C/C++ -> C++, and choose where to create it. It's simple, has a nice IDE, try to get used to it! :D
Last edited on
Topic archived. No new replies allowed.