string as array

hi everyone .

i am newbie in CPP .
i have a problem for my program .
i try to use getline in order to detect string from user .
but i wanna use it as array .
so user could input as many as he want .
here is my example program .
actually , it didn't show the error .
but pass the getline command .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
using namespace std;

int main () {
  string str[3];
  char quit;
  int count;

  for (count = 0; count <3; count++)
  {
  cout << "Please enter full name: ";
  getline (cin,str[count]);
  cout << "Thank you, " << str << ".\n";
  }
  return 0;
}


or is there any other way to set string as array ?
Why don't you just do

1
2
3
4
5
6
cout<<"Please enter full name: ";
getline(cin, str);
cout<<"Thank you, " < str << ".\n";
}
cin.get();
}


Waaayyy easier than going through the trouble of making it an array. Do you actually want to make it an array or are you just seeing if it's possible? The definition of a string is an array of characters.

Also, you'll need cin.get(); at the end of your program or it'll close out after the last message is printed, i.e. "thank you *name* ".
You could use a vector of strings.
std::vector<std::string> mystrings;

Don't forget to include <vector> and <string>

Using cin.ignore( numeric_limits< streamsize > ::max(), '\n' ); is a better way to pause the program until the user hits a key. (Don't forget to include <limits> if you use this method)
Last edited on
@ eker676 :
could u please explain more detail ?
what is the function of vector ?

@warrior :
i want to make an input where user could enter his name not only once .
Topic archived. No new replies allowed.