Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   string problem (http://www.go4expert.com/forums/string-problem-t27183/)

chocolatsprintz 21Nov2011 08:53

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>
#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..

xpi0t0s 22Nov2011 04:51

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.

xpi0t0s 22Nov2011 04:51

Re: string problem
 
Read the link in my sig before you post again.

chocolatsprintz 22Nov2011 11:30

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".

xpi0t0s 22Nov2011 12:03

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];
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".

chocolatsprintz 22Nov2011 12:22

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.

chocolatsprintz 22Nov2011 12:27

Re: string problem
 
i don't declare names[10] because it suppose to be declare during runtime according to the user input.

xpi0t0s 22Nov2011 12:54

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.

chocolatsprintz 22Nov2011 13:05

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>
  #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;
  }


xpi0t0s 22Nov2011 15:18

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 10:04.