Store input into an array using delimeters

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

int main ()
{
  cout << "Enter Data" << endl;
   char tempStr[3]; 
   int count = 0;

   while((cin != NULL) && (count < 3))
   {
      cin.getline(tempStr, 256, ',');
      count++;
   }
   count = 0;

   cout << tempStr[2] << endl;

   return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

using namespace std;

int main ()
{
  cout << "Enter Data" << endl;
   char tempStr[3]; 
   int count = 0;
  

   for(int i = 0; i < 3; i++)
   {
   char s;
      cin >> s;
      tempStr[i] = s;
      count++;
      if(count == 3){
               break;
               }
      
   }
   
   for(int i = 0; i < 3; i++){
           
  

   cout << tempStr[i] << ", ";
}
   system("PAUSE");
   return 0;
}


?
That outputs "o, n, e," and I want it to output something like
"one
two
three"

without the comma. Maybe I should use strings but I would also like to use the get or getline function since it has the delimter feature.
Last edited on
Oh i tough you said with comma. Okay easy enough:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

using namespace std;

int main ()
{
  cout << "Enter Data" << endl;
   char tempStr[3]; 
   int count = 0;
  

   for(int i = 0; i < 3; i++)
   {
   char s;
      cin >> s;
      tempStr[i] = s;
      count++;
      if(count == 3){
               break;
               }
      
   }
   
   for(int i = 0; i < 3; i++){
           
  

   cout << tempStr[i] << endl;
}
   system("PAUSE");
   return 0;
}
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
Last edited on
closed account (D80DSL3A)
Maese909's method looks good if you can use std::string.
Here's some code which is working for the char[] storage:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace 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;
}

Output:

Enter Data
one,two,three
one
two
three
Last edited on
Topic archived. No new replies allowed.