I wrote the code below and it runs well. However, how do I make it so that 'Jeanne' enters her own name instead of the numeral 01 to get the message 'Welcome Jeanne'?
Thank you.

// This program assigns a code to 'Jeanne' (analogous to a pass code)

Actual program starts just below

#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	
	int counter=0;
	int code;
	
	cout<<"Please enter your code\n";
	cin>>code;
		
	do
	{ // 'do' opens
	while (code!=01)
			{ // 'while' opens
				cout<<"Please enter correct code\n";
				cin>>code;
		if (code==01)
		cout<<"Welcome Jeanne\n";
			} // 'while' closes
	} // 'do' closes
	while (counter<=4);
	return 0;
}

Recommended Answers

All 8 Replies

Very simple. Why don't you try something and if it doesn't work, post it.

Very simple. Why don't you try something and if it doesn't work, post it.

Thanks for your reply WaltP. I tried to fix the program as follows, but I had limitted success:

#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	
	// int counter=0;
	char (Jeanne);
	char name;
	//name=Jeanne; If I include this part, I get the message: "warning C4700: uninitialized local variable 'Jeanne' used"
	cout<<"Please enter your name\n";
	cin>>name;

	{
	if (cin>>Jeanne)
		cout<<"Welcome "<<name<<"\n";
	}

	// When I type 'Jeanne', I get this "Welcome J". The program is reading just the 1st entry.

Good guess; a char is a single character, though. If you want to enter more than one character, you'll need a std::string, or an array of chars.

I'm a bit lost with this: char (Jeanne); What is that for? You have created an object of char type, named Jeanne, and not set it to any value. Tell us what you're trying to do there, and we'll point you in the right direction.

And this also: if (cin>>Jeanne) That will always return true and always execute the following statements (unless the cin fails, but if you typed more than one character previously, there will already be characters to read in, so it will pass; if you only typed one character, it will simply wait for you to type more). I doubt this is what you wanted to do.

Good guess; a char is a single character, though. If you want to enter more than one character, you'll need a std::string, or an array of chars.

I'm a bit lost with this: char (Jeanne); What is that for? You have created an object of char type, named Jeanne, and not set it to any value. Tell us what you're trying to do there, and we'll point you in the right direction.

And this also: if (cin>>Jeanne) That will always return true and always execute the following statements (unless the cin fails, but if you typed more than one character previously, there will already be characters to read in, so it will pass; if you only typed one character, it will simply wait for you to type more). I doubt this is what you wanted to do.

Hello Moschops. I have changed the code a bit. In a nut shell, what I'm trying to do is to get the program to accept the name 'Jeanne' and display the 'welcome' message. If a different name is typed, the message 'You are not Jeanne' should display. I can't seem to get the program to make the connection between 'name' and 'Jeanne', as in name=Jeanne.
Thanks. Here it is:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
using std::string;

int _tmain(int argc, _TCHAR* argv[])
{
	char name[]="Jeanne";
	//char string;
	cout<<"Please enter your name\n";
	cin>>name;
	
		if (name==name)
		cout<<"Welcome\n";
		else 
			cout<<"You are not Jeanne\n";
	
return 0;
}

What moschops was referring to was creating a variable of type string, rather than one of type char[]. That way, you'd be able to read in any name and test whether or not it's equal to "Jeanne" using the compare function.

Along with this, if you're initializing name to "Jeanne", then you shouldn't have the user try to change name. Lastly, you're testing name being equal to name, which is always going to be true. This doesn't actually compare the strings, themselves, but the address where they're located.

You are taking in the user's input, and overwriting the value you mean to compare it to.

Take a step back and think about what you're trying to do first, then code it.

1) create variable that the user will put data into - use a std::string
2) create different variable, with different name, that is the value to check against - use a std::string and make it equal "Jeanne"
3) get user to put data into that first variable
4) test to see if the two variables are now equal
4a) If they are, print out some words
4b) If they're different, print out other words.

If you take each of those steps and turn them into code, it will do exactly what you need. I think the issue is that you haven't solved the problem (i.e. worked out what you're going to do) before you started coding.

Writing the correct syntax is easy once you've had some practice with a language; the difficulty in programming is not in learning the language, but in learning how to think about problems and solve them in a way that you can code.

making things clear

#include <iostream>
#include <string>

using namespace std;

void showresult(string name){
	string take;
			if("rose" == name){
	       cout <<"name found...... Access granted\n";}
			else{
		cout <<"Alarm alarm !!\a\n";
	}}

int main(int argc, char *argv[]){
	string nameTake;
	while (true){
		cout << "enter your name plz  or enter to quit\n";
		getline(cin,nameTake);
		if(nameTake.empty()||nameTake=="quit"){
			cout <<"Programe terminated by user\n";
			break;
		}
		showresult(nameTake);
	}
	return 0;
}

Thanks for your help!! Your program incorporates things I would have never thought of. I knew that I needed to work with a 'string', but I'm new at this. I'll look at your code and learn from it.

making things clear

#include <iostream>
#include <string>

using namespace std;

void showresult(string name){
	string take;
			if("rose" == name){
	       cout <<"name found...... Access granted\n";}
			else{
		cout <<"Alarm alarm !!\a\n";
	}}

int main(int argc, char *argv[]){
	string nameTake;
	while (true){
		cout << "enter your name plz  or enter to quit\n";
		getline(cin,nameTake);
		if(nameTake.empty()||nameTake=="quit"){
			cout <<"Programe terminated by user\n";
			break;
		}
		showresult(nameTake);
	}
	return 0;
}
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.