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..
>> it say i have to use vector, and i try but have errors The errors are because your code is wrong. >> i can't use getline(cin,names).could anyone tell me why? Can't use as in you've been told not to, or can't use as in you get errors? If the latter then it's because your code is wrong.
which line in my code is wrong? i dun know because it can run but it abort when i try to enter data at"enter the first name".
You can't store multiple names in one string. Well, you can, but not in the way you're trying to do it. You're trying to use array semantics: Code: cin >> names[i]; so names has to be an array of some kind, either a static array or something that implements operator [] to provide an array-like syntax. I can't tell you what's wrong with your vector code because you didn't post it. Also at the above line of code i is undefined. Variables are not automatically initialised to zero when you declare them; if that's what you want, then that's what you must type. Code: int i; // this is undefined and can AND WILL contain anything. You cannot assume it will contain 0. int j=0; // this is initialised to zero printf("%d %d\n",i,j); // try it - see what this displays. It will be a junk value and zero. Given that the code enforces a limit of 10 names the easiest solution here would be to keep the code as it is, initialise i to zero at the top and make names an array of 10 strings, i.e. Code: string names[10]; With vectors you have to use push_back() and the like, you cannot extend a vector by using [] syntax. The only way to store multiple names in one string is to concatenate them with a suitable separator, for example Code: string names="Alan, Bert, Charlie, Denzil"; but that's trickier to extract when you need them; names[2] won't give you "Bert".
"int people,i;" this line i declare int i. "for (i=1;i < people;i++)" this line i initialize i=1. i didn't put the code with vector because i not sure about how to do it and try it randomly.
i don't declare names[10] because it suppose to be declare during runtime according to the user input.
OK, well if you're sure that i is automatically set to zero, then I must be wrong. Why not add a printf statement (or cout if you prefer) to display the value, then you can prove it to me.
this is the code that i add the dynamic array.did you know how to arrange the name so it can be display like the output. Code: #include<iostream> #include<string> using namespace std; int main() { int people; int i=1; string* newname; string names; cout << "How many people are there? (1 to 10) " << endl; cin >> people; while(people<1 || people>10) { cout << "Maximum size should be between 1 to 10" << endl; cout << "Please re-enter the number of people: " << endl; cin >> people; } newname = new string[people]; cout << "Enter first person's name: " << endl; cin >> names; for(i=1; i<people; i++) { cout << "Enter the next person's name: " << endl; cin >> newname[i]; } delete [] newname; newname = 0; system("pause"); return 0; }
In C++ arrays start from zero, not 1. So an array[10] comprises elements [0] through [9], and assigning to array[10] causes undefined behaviour. I don't know why you would want to store the first name in "names" and the subsequent names in "newname". It makes more sense to store them all in "newname" and remove "names" altogether. Not sure I understand your last question, but if you're asking what I think you're asking, then let me point out that you already know how to do a for loop, how to display stuff on the screen and how to reference individual elements of an array. Maybe you could think about how to combine that stuff to get the result you want rather than just asking me to do it for you.
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 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 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.