reading txt file into vector

Hello. I have to read an unknown amount of integers, which are separated by commas (which is my problem here) into a vector. If there are no commas, I can do that. But I don't know how to do it with commas. I'm not allowed to use a lot of stuff, such as functions or classes yet. I can only use very simple code.
Here is how I am trying to read data into the vector. Nothing happens, however. Can anyone give me some pointers?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  
	ifstream input;
	input.open("D:\\newdata.txt");
	if (input >> data){
		ch = input.peek(); //peek at the first character
		
			if (!isdigit(ch)){
				cout << ch;
				
			}

			else{
				input >> data; 
				salaryData.push_back(data);
				}
			
		}

	
But I don't know how to do it with commas

Possibly the easiest method would be to extract and discard the comma.

Something similar to the following, don't forget to make sure your file actually opens.
1
2
3
4
5
6
7
8
9
   ifstream input("newdata.txt");

   char comma;
   int number;

   while(input >> number >> comma)
   {
      // Do something with number.
   }


Last edited on
jlb, you're a lifesaver!
Topic archived. No new replies allowed.