Hello

Hello, I am asked to create this simple program for my c++ class. Having some difficulty. Below is the question.

Ask the user for five names. If the user enters the same name twice,
give a warning and continue asking. For example:
Enter 5 different names...

Last edited on
Hello jordandamon,

That is nice, but Having some difficulty. Does not tell me what part of your code you are having trouble with.

Post what you have done and point which part(s) you are having trouble with.

If you do not know where to start then start with this: Ask the user for five names. Do what is needed to get this part to work first before you add
If the user enters the same name twice, give a warning and continue asking.

Hope that helps,

Andy
Yes that definitely helps!

so i guess what i need help with is, when the user enters the same name what do i do and also displaying the 5 names in the end. What loop would i use or while statement.
Last edited on
Hello jordandamon,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Looking over your program It will not work.

You have defined "name" as a single string followed by entering five times into the same string. This will leave you with "name" containing the last entry made.

"name" needs to be an array or vector. Right now I am not sure what you have used if either.

Next the program could use a function to check for a duplicate name. Which would need to be called after you enter the second name and each name after that.
At the end of main you can use another for loop to print the names.

Hope that helps,

Andy
Hey jordandamon,

I think we are in the same class... I was having some issues with this one as well... this is what I got for it... hope this helps!

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int checkrepeat (string names [],string name){
int counter=0;
while (counter < 5) {
if (names [counter] == name){
return 1;
}
counter ++;
}
return 0;
}


int main() {
string names [5];
string name;
int counter = 0;

cout << "Enter 5 different names..."<< endl;
while (counter < 5) {
cout <<"Enter name # "<< counter +1<<": ";
cin >> name;
cout <<name << endl;
int result = checkrepeat ( names, name);
if (result == 0){
names[counter] = name;
counter ++;
}
else {
cout <<"You already entered that name!" << endl;
}
}
cout <<"Here are the names:"<< endl;
counter = 0;
while (counter < 5) {
cout <<names [counter] << endl;
counter ++;
}

return 0;
}
Hey Jordan,

How familiar are you with arrays and loops? I think you should create an array of strings to hold the values entered by the use. I would use a loop to collect the user input, I would create another loop nested within the input loop that tests for duplicate entries by comparing the current element to all prior elements of the array. I would then create another loop to print the results to the console.

The nest loop should have a control structure that tests for a match between the value at the current index and the value of the previous elements in the array. If the values are found to be equivalent then the index value should be decremented so that the string can be re-entered and the nested loop should exit immediately.
Thank you everyone!!! bk1303 message me, it doesnt allow me to send you a message.
Hello bk1303,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Looks like it will work, but I will have to test it.

Your use of while loops will work, but a simple for loop is all yo really need fo this.

Andy

P.S. It is not nice to invade someone else's topic. Also the idea is to help someone learn not give them the answer.
Topic archived. No new replies allowed.