I am trying to enter something like "one,two,three" on a single line and have my tempStr[3] contain those three values that were typed in. I cant seem to get it to work. Plz Help. Im pretty sure my issue is with "cin.getline(tempStr, 256, ',');" but I have not been able to find a solution.
That still dosent work. I want the input to be something like "one,two,three" and the output to be
one
two
three
There needs to be something such as get or getline which realizes the delimeter ',' and then stores next string in the array. I cant seem to get the getline to work for me.
I would create an array of strings of length 3 to store each word. string word[3]; //array to store the words
You can use getline(cin, word[index], ','); //read but don't store the ,
to read the first two words since they will have a comma after them. (Hint - use a for loop to get these 2 words).
To read the last word, word[2] , you can use a normal cin statement :)
P.S. This is assuming the input is coming in the form - one,two,three
#include <iostream>
usingnamespace std;
int main()
{
cout << "Enter Data" << endl;
char tempStr[3][256];
int count = 0;
// get the 1st 2 words which are followed by ,'s
while(count < 2)
cin.getline(tempStr[count++], 256, ',');
// get last word
cin.getline(tempStr[count++], 256);
for(int i=0; i<count; ++i)
cout << tempStr[i] << endl;
cout << endl;
return 0;
}