Quote:
Originally Posted by chocolatsprintz View Post
the error is it can't take the next name that user input.
and can someone tell me how and explain to sort the name that user input in alphabetically order then print the first name and last name in the order. i try search the google, but i can't understand it.
it say i have to use vector, and i try but have errors.
and i can't use getline(cin,names).could anyone tell me why?


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


int main()
{
     int people,i;
     string names;

     cout << "How many people?(1 to 10)?"<<endl;
     cin >> people;

     while(people < 1 || people > 10)
    {
          cout <<"Max size between (1 to 10)" <<endl;
          cout <<"Please enter again :"<<endl;
          cin>>people;
    }

     cout << "enter the first name:" <<endl;
     cin >> names[i];


     for (i=1;i < people;i++)
     {
     cout <<"enter next name:"<<endl;
     cin >> names[i];
     }

return 0;
getchar();
}
example the 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.

thanks..
Sorry for pitching late,
I think there were two major hitches in your code:
1. As already pointed out, your index i was not initialized which was causing the seg fault/crash.
2. Following code snippet:
Code:
     cout << "enter the first name:" <<endl;
     cin >> names[i];
Since 'names' is of data-type 'string', names[i] means the i-th character of the string 'names', and hence stdin would just grab 1st character from the string you inputted. As in your example, your input string is "Julie" and cin >>names[i] would only grab character 'J' from stdin.
stdin still has 5 characters (i.e. u,l,i,e,\n) with it, which are grabbed automatically during further cin's resulting in skipping the user inputs as you observed.

I hope it clears the point. As per your requirement, the solution could be either take 'names' as a string array or allocate memory to it at runtime which you've already mentioned in your last post.