Need help with reading a txt file that has commas


so the txt file has numbers 90, 80, 20, 45, 18,
if the numbers didnt have commas in between my program could read it fine, but if i didnt remove the commas and i ran the program, it would only output 90.
Case one can only read 90. same with case 2. Anyway to fix this? Keep in mind that this is really beginner lvl c++ so if you do have solutions, can you dumb it down.

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main(){
int choice = 0; // for switch statement
int money; // user input


ifstream dataFile("newdata.txt");
vector<int> salary;


int value;
for (int k = 0; k <= 4; k++) {
dataFile >> value;
salary.push_back(value);
}
int largest = salary[0];
int lowest = salary[0];

cout << "Please select 1,2,3,or 4: \n";
cout << "Search Salary Enter 1 \n";
cout << "Generate stats file enter 2 \n";
cout << "Print salaries, Enter 3\n To quit, enter 4 \n";
cin >> choice;

switch (choice){
case 1: cout << "What is salary\n";
cin >> money;
for (unsigned int x = 0; x < salary.size(); x++) {

if (money == salary[x])
{
cout << "Matching Salary Found\n";
if (money != salary[x])
{
cout << "You entered the wrong salary\n"; // if it doesn't match, output "wrong salary"
}
}

}break;
case 2:{
for (unsigned int num = 0; num <= 4; num++){ //finds highest salary
if (salary[num] > largest){
largest = salary[num];
}
}
cout << "Highest score is " << largest << endl;

for (int lnum = 0; lnum <= 4; lnum++){ // finds lowest salary
if (salary[lnum] < lowest){
lowest = salary[lnum];
}
}
cout << "Lowest score is " << lowest << endl;
ofstream newFile("stats.txt"); //creates new file
newFile << largest << lowest; //stores into new file
newFile.close();
cout << "Stats.txt has been generated\n The highest and lowest salaries are: " << largest << " " << lowest << endl;
}
break;
case 3:cout << "The salaries are: " << endl; //outputs all salaries
for (int m = 0; m <= 4; m++){
cout << salary[m] << endl;
}

break;
case 4: exit(0); break;

default:cout << "Choose programs 1-4\n";
}
return 0;
}
Instead of using the overloaded operator>>, take a look at std::getline( std::istream &, std::string &, char delim ), the default delimiter is the newline character '\n', you may now replace '\n' with the comma separator, such that you have

1
2
3
4
5
6

std::string value;
while( std::getline( dataFile, value, ',' ) )
{
salary.push_back( std::stoi( value ) ); // std::stoi means string to integer
}
it works now, thanks man!
Topic archived. No new replies allowed.