For some reason, it works when I just do the entering the name part it works fine, but with the rest of my program I'm having a problem. I'm trying to have the program make it where they enter a password to get in the program (Which I got to work, even though I know it's not extremely secure.) and after that I want them to enter their full name for later use in the program, but for some reason it just skips the part where they're supposed to input their name. Could someone tell me what I did wrong/what I need? (Also, if this is in the wrong place, I apologize greatly. I've never posted on here before.)

#include<iostream>
#include<windows.h>
#include <string>
using namespace std;
string password;
char name [200];

int main()
{
	cout << "Enter your password.\n";
	cin >> password;

	if(password == "testpassword")
	{
	cout<<"Please enter your first and last name: \n";
	cin.get(name, 200);
	cin.ignore(250, '\n');
        cout << "Welcome back, " << name;
	system("pause");

	}
		else
		{
			system("CLS");
			system("title Incorrect Password");
			cout << "Incorrect password.\n";
			cout << "This program will now close.\n";
			Sleep(5000);
		}
return 0;
}

Recommended Answers

All 14 Replies

Why not use cin>>name;?

If I do it doesn't remember both the first and the last name. It only remembers the first name.

Instead of

cin.get(name, 200);

Because it'll only remember the first word, and not the second.

If I do it doesn't remember both the first and the last name. It only remembers the first name.

What do you mean? You are only asking for 1 input, meaning you would type your name as Jimmy Rourke, i.e. with a space.

It would only remember their first name. It would only remember the Jimmy and not the Rourke.

Ok sorry, its 2 am here :D.
Make name a char* or a string and use cin.getline(char* name) or getline(cin, string)

Be aware of mixing getline and cin though. There are many side effects.

Ok sorry, its 2 am here :D.
Make name a char* or a string and use cin.getline(char* name) or getline(cin, string)

Be aware of mixing getline and cin though. There are many side effects.

I've never done those before, so I'm unsure how to do them. Could you give me an example? If not, that's fine. I can try to do more research.
Either way, thank you for all of your help. =]

I would recommend just using a string, instead of char array.

string name;
getline(cin,name);

When I do that it's giving me the same problem I described in my original post. :(

You must use getline(cin,string) everywhere you get input. So you must change where you ask for the password to

getline(cin,password);
commented: Helped me with a problem I was having. +1

Ah. It's working now. Thank you very much. =]

Ah. It's working now. Thank you very much. =]

Just want to add, you should do some research as to why you had to change both/ other ways to do the same thing. :)

Just want to add, you should do some research as to why you had to change both/ other ways to do the same thing. :)

It might be because I used cin.get(name, 21); for the name, but cin>>password; for the password.

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.