![]() |
string problem
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>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.. |
Re: string problem
>> 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. |
Re: string problem
Read the link in my sig before you post again.
|
Re: string problem
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". |
Re: string problem
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];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.Code:
string names[10];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"; |
Re: string problem
"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. |
Re: string problem
i don't declare names[10] because it suppose to be declare during runtime according to the user input.
|
Re: string problem
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.
|
Re: string problem
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> |
Re: string problem
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[i]". 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. |
| All times are GMT +5.5. The time now is 04:29. |