C++ problems Need a function to remove commas while read the below data from fil
ID: 3860760 • Letter: C
Question
C++ problems
Need a function to remove commas while read the below data from file
Illinois Springfield 12,439,042
Nebraska Lincoln 1,715,369
Arkansas Little Rock 2,679,733
New Hampshire Concord 1,238,415
Ohio Columbus 11,374,540
Kansas Topeka 2,693,824
Louisiana Baton Rouge 4,480,271
Michigan Lansing 9,955,829
Florida Talahasse 16,028,890
Connecticut Hartford 3,409,535
Iowa Des Moines 2,931,923
West Virginia Charleston 1,813,077
Missouri Jefferson 5,606,260
Wyoming Cheyenne 495,304
California Sacramento 33,930,798
Alabama Montgomery 4,461,130
New Jersey Trenton 8,424,354
South Dakota Pierre 756,874
Maryland Annapolis 5,307,886
Indiana Indianapolis 6,090,782
Explanation / Answer
The below program reads a line from the file with commas(the numbers) and prints the line with number (without commas)
#include<iostream>
#include<string>
#include<sstream>
#include<fstream>
#include<vector>
using namespace std;
int main(){
ifstream fin;
string line;
string token;
vector<string> data;
int count;
string str;
fin.open("Input1.txt"); //Assuming data is present in file Input1.txt
if (fin){
while (getline(fin,line)){
istringstream ss(line);
count = 0;
while(std::getline(ss, token, ' ')) {
if (token.size() == 1)
token = " ";
data.push_back(token);
count++;
}
istringstream ss1(token);
str = "";
while(std::getline(ss1, token, ',')) {
str = str + token;
}
data[count-1] = str;
for (int j = 0; j<data.size(); j++)
cout << data[j];
cout << endl;
data.clear();
}
}
else {
cout << "Error in opening file" << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.