For looping,alphabetical

#include <iostream>
#include<string>
using namespace std;

int main(){
int number;
string name;
int a[10],value;


cout<<"How many people are there (1-10)?"; // this will be looping because if the user enter it wrongly then it will repeat the question
cin>> number;
while ((number <1)|| (number>10))
{

cout<<"Maximum size should be between 1 and 10 "<<endl; //now telling the user number of people range only 1 until 10
//cout <<"Maximum size should be between 1 and 10"<<number;
cout<<"Please re-enter the number of people :";
cin>>number;

}
cout<<""<<endl;
cout<<"Enter person 1's name :";
cin>> name;
getline(cin,name);

for (int i=1; i<=10;i++)
{
cout<<"Enter the next person's name :" <<endl;
cin>>value;
a[i]=value;
}


return 0;
}




I need to for loop the Enter the next person's name.
Now my solution this statement it came out directly after you press enter.


the output suppose to look like this.

Sample Output:

How many people are there (1 to 10)? 50
Maximum size should be between 1 and 10.
Please re-enter the number of people: 60
Maximum size should be between 1 and 10.
Please re-enter the number of people: -12
Maximum size should be between 1 and 10.
Please re-enter the number of people: 5

Enter person 1's name: Julie
Enter the next person's name: Popo
Enter the next person's name: Helmi
Enter the next person's name: Henry
Enter the next person's name: Frodo

Frodo is at the head of the line.
Popo is at the end of the line.
Press any key to continue . . .




stuck from the line that I bold with...
- use the code-tag
- describe your problem

it makes it easier to help

so far there are a few problems:
you read in the number of names, but your loop goes from 1 to 10, you read in the name in the loop as ints, not as strings and with i = 10 you are out of your array bounds.
arrays of size N go from index 0 to index N-1
I'm sorry I don't get it how you mean.
My error occur when I press enter
it output

How many people are there (1 to 10)? 50
Maximum size should be between 1 and 10.
Please re-enter the number of people: 60
Maximum size should be between 1 and 10.
Please re-enter the number of people: -12
Maximum size should be between 1 and 10.
Please re-enter the number of people: 5

Enter person 1's name: Julie

//then it output

Enter the next person's name:
Enter the next person's name:
Enter the next person's name:
Enter the next person's name:

//this came out right after key in any names two times then suddenly cames out enter the next person name 5 times directly.


Are you sure about the while condition? The while conditions states to run as long as the "enter input" is less than 1 OR greater than 10. As per my understanding,it should be...
 
while (number>=1&&number<=10)
Last edited on
I think it should be so:
while ( (number > 0) && (number < 11) );
@magneto & codekiddy
The first while loop is okay (it loops as long as the input is not okay)

The first problem is what Mathes mentioned: out of bounds and not taking int account the number entered by the user.

The second problem is mixing getline(cin,name); and cin>>value;

Solution:
for (int i=1; i<=10;i++) -> for (int i=0; i<number;i++)

cin>>value; -> getline(cin,name);
Thank you fellow friends. Wait I try to compile again . I'll be back soon.
Thank you I got it right. ! Time to continue to my work to try how to get output


Frodo is at the head of the line.
Popo is at the end of the line.
#include <iostream>
#include<string>
using namespace std;

int main(){
int number;
string name,first,last;
int a[10],value;


cout<<"How many people are there (1-10)?"; // this will be looping because if the user enter it wrongly then it will repeat the question
cin>> number;
while ((number <1)|| (number>10))
{

cout<<"Maximum size should be between 1 and 10 "<<endl; //now telling the user number of people range only 1 until 10
//cout <<"Maximum size should be between 1 and 10"<<number;
cout<<"Please re-enter the number of people :";
cin>>number;

}
cout<<""<<endl;
cout<<"Enter person 1's name :";
cin>> name;
getline(cin,name);

for (int i=0; i<number;i++)
{
cout<<"Enter the next person's name :" ;
getline(cin,name);
a[i]=value;
}

cout<< " " << name << " is at the head of the line \n";
cout<< " " << name << " is at the end of the line \n";

if (name<first)
{
first=name;
}

else if (name>first)
{
last=name;
}


return 0;
}





My problem now the one i have bold .
question was about that once all the names have been read, the program will report which person will be at the front of the line, and which person will be at the end of the line. You may assume there will be no two persons of the same name.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int main(){
int number;
string m_name , name[10];
int a[10],value;


cout<<"How many people are there (1-10)?"; // this will be looping because if the user enter it wrongly then it will repeat the question
cin>> number;
while ((number < 1) ||  (number >10))
{

cout<<"Maximum size should be between 1 and 10 "<<endl; //now telling the user number of people range only 1 until 10
//cout <<"Maximum size should be between 1 and 10"<<number;
cout<<"Please re-enter the number of people :";
cin>>number;

}
cout<<""<<endl;
for (int i=1; i<=number;i++)
{

	cout<<"Enter person  name :";
	cin>> name;
	getline(cin,m_name);
	name[i] = name ; 

	cout<<"Enter the next person's name :" <<endl;
	cin>>value;
	a[i]=value;
}


return 0;
}
Sorry my solution when wrong on the part output that i have bold

How many people are there (1 to 10)? 50
Maximum size should be between 1 and 10.
Please re-enter the number of people: 60
Maximum size should be between 1 and 10.
Please re-enter the number of people: -12
Maximum size should be between 1 and 10.
Please re-enter the number of people: 5

Enter person 1's name: Julie
Enter the next person's name: Popo
Enter the next person's name: Helmi
Enter the next person's name: Henry
Enter the next person's name: Frodo

Frodo is at the head of the line.
Popo is at the end of the line.

Press any key to continue . . .




my solution for part that i have bold and i didnt get as what the output suppose to be..

int main(){
int number;
string name,first,last;
int a[10],value;


cout<<"How many people are there (1-10)?"; // this will be looping because if the user enter it wrongly then it will repeat the question
cin>> number;
while ((number <1)|| (number>10))
{

cout<<"Maximum size should be between 1 and 10 "<<endl; //now telling the user number of people range only 1 until 10
//cout <<"Maximum size should be between 1 and 10"<<number;
cout<<"Please re-enter the number of people :";
cin>>number;

}
cout<<""<<endl;
cout<<"Enter person 1's name :";
cin>> name;
getline(cin,name);

for (int i=0; i<number;i++)
{
cout<<"Enter the next person's name :" ;
getline(cin,name);
a[i]=value;
}

cout<< " " << name << " is at the head of the line \n";
cout<< " " << name << " is at the end of the line \n";

if (name<first)
{
first=name;
}

else if (name>first)
{
last=name;
}


return 0;
}
Topic archived. No new replies allowed.