How can a user input words?

Posts 1–15 of 15 · Page 1 of 1
How can a user input words?
Hey guys, im working on a program that you type a password and than you can proceed with the program but what do i type instead of int? (srry i forgot what the int was. i know its an integer but what was it.) coz with int,double you can put numbers, bool, true/false and with char only 1 char, but how do i insert a full word?
Code:
char passwd[50];
std::cin >> passwd;
If(passwd == "the actual pass"){
std::cout << "Access Granted”;
} else {
std::cout << "Access Denied";
}
This should work, but I haven't used C++ for like 2 months, so it might contain some small syntax errors. (I've been using Java lately).
isnt it with cin?
for example
Code:
int blabla;
cout << "This is a random line";
cin >> blabla;
Forgive me if im wrong.
I just started learning C++
Quote Originally Posted by Fly3r View Post
isnt it with cin?
for example
Code:
int blabla;
cout << "This is a random line";
cin >> blabla;
Forgive me if im wrong.
I just started learning C++
Yup I learned this with cout<< for outputs and cin >> for user input.

But as I also changed to Java, maybe...
Quote Originally Posted by Fly3r View Post
isnt it with cin?
for example
Code:
int blabla;
cout << "This is a random line";
cin >> blabla;
Forgive me if im wrong.
I just started learning C++
An int doesn't hold a word.
A array of chars or a string does.
O.o i created something like this (Just to try if the pw thing works) but when i run it and write kana it says access denied. plz help.
Code:
#include <iostream>
#include <Windows.h>

using namespace std;

int main()
{
	char passwd[50];
		cin >> passwd;
	if(passwd == "kana"){
		cout << "Access Granted";
	}else{
		cout << "Access Denied";
	}
}
Cookie thanks for correcting me .. i ve just started
Quote Originally Posted by Auxilium View Post
#include <string>
then use std::string type
it should be better if he uses using namespace std; into the top with includes hearders lol


for example

Code:
#include < blah blah my hearder>

using namespace std;


int main()

{
                      cout << "Hello my first message in output";
// output ( message )

			cin >> inicial >> final >> razao;

// input ( when the person enter something into the screen)


}
That is absolutely incorrect. Polluting the global namespace (e.g., using namespace std;) is highly frowned upon. There is reason why the standard library is implemented within the std namespace.

If you MUST import names, do so in the most local scope possible using a using declaration, not a using directive.
You can't compare two words like that, a char array is nothing but a pointer, seeing as the pointers to both strings will be different even if the text is the same, you'll have to use one of the following:
Code:
if(!strcmp(passwd, "kana") // strcmp returns 0 if both strings are the same
{
//code
}
Or:
Code:
if(std::string(passwd).Compare("kana"))
{
}
Code:
#include <iostream>
#include <string.h>
using namespace std;

int main()
{
	string passwd;
		cin >> passwd;
	if(passwd == "kana"){
		cout << "Access Granted";
	}else{
		cout << "Access Denied";
	}
}
If he has using namespace std; and #include <string.h> than he doesnt need to prefix std:: to all of his strings.
thx guys for your help.
#include <string.h> did not help at all btw.....

#include <string> did help but i tried it before... it didnt work than o.O.

Code:
#include <iostream>
#include <string>

using namespace std;

int main(){
	string acc, pw;
	cout << "Username: \n";
	cin >> acc;
	cout << "Password: \n";
	cin >> pw;
	if(acc == "MPGH"){
		cout << "Username correct\n\n";
	}
	else if(acc != "MPGH"){
		cout << "Username incorrect\n\n";
	}

	if(pw == "cplusplus"){
		cout << "Password correct\n\n";
	}
	else if(pw != "cplusplus"){
		cout << "Password incorrect\n\n";
	}
	if(acc != "MPGH" && pw != "cplusplus"){
		cout << "Username or/and password incorrect, are you sure you got the valid info?" << endl << "contact me for the password and username\n" << endl;
	}
	else if(acc == "MPGH" && pw == "cplusplus"){
		cout << "Information correct. Press any key to see your code." << endl;
		system("pause");
		cout << endl;
		cout << "Code : gHahwsejGKfndfj6893860339gDhbJeGhtdhzxvnG" << endl << "paste this on the forum\n\n";
	}
	system("pause");

}
That what i came up with and i think its pretty cool.
Now i need to find out somewhere what to use it for xD.
probably last post for now sry for the multiple posts
.-. Really simple:

Code:
#include<iostream>
#include<cstdlib>// to add Pauses, if u don't want them then erase em.
#include<string>
#include<windows.h>//I think u need this to erase everyting on the screen. //don't remember

using namespace std; // so u can just write cout instead of std::cout

int main()
{
   string username = "Guest"; // declare a string variable and initialize it.
   string password = "MPGH";
   string userInput1;
   string userInput2;

   int security;

// a string variable holds letters. Which in this case it's what you want

   cout << "username: ";
   cin >> userInput1;

   system("CLS"); // erases everything on the screen. 

   cout << "Password: ";
   cin >> userInput2;

   if(userInput1 == username)
   {
       security = 5;
       
       if(userInput 2 == password)
       {
       security = security + 5;


       }

   }
   else 
   {
   security = 0;

   }
   
   if(security == 10)
   {
      cout << "Welcome guest!\n\n";

   }
   if(security == 5)
   {
        cout << "incorrect password or username. Shutting down.\n";
        system("PAUSE");
        return 0;
   }
   if(security == 0)
   {
        cout << "error Shutting down\n\n";
        
        system("PAUSE");
        return 0;


   }

 system("PAUSE");
 return 0;
}
I think this should work. If it doesn't tell me. remember to #include<string>
if u don't it won't work.

oops. Didn't see your last post XD sry then

a error I saw. first u forgot one little curly brace at the end. and u put
system("pause"); instead of system("PAUSE");
and sudgestion, when u name functions try naming them so u remember what they're for. And so that your code is more readable to others. and something people tend to do is that the first word in a variable name starts with a none capital letter and the other words in a variable name are capital letters.
like this:

int aVariable = 100;

oh and if you name more than one variable by the same type, name them in different lines so it's more readable to others.

like this:

instead of:
int pie, cake;

try:
int pie;
int cake;

I think that's it
"system("CLS");" <------- DONT DO IT.

Couldn't word it well so here:
"It's frowned upon because it's a platform-specific hack that has nothing to do with actually learning programming, but instead to get around a feature of the IDE/OS - the console window launched from Visual Studio closes when the program has finished execution, and so the new user doesn't get to see the output of his new program."
Posts 1–15 of 15 · Page 1 of 1

Post a Reply

Tags for this Thread

None

Talk with us