8. CODING (20 points): Write an entire C++ program to solv containing an arbitra
ID: 3735454 • Letter: 8
Question
8. CODING (20 points): Write an entire C++ program to solv containing an arbitrary number of rows. On each row, a city, color, and cou there will be no spaces in the cities or colors. Do not create any functions. The main() will be responsible for: e this problem. The program will read from a file nt will appear. You should assume that -Opening input file ("exam.txt") for reading. No need to check for file failure. Assume it opened okay. With each row of data, the following should be performed if the color is not purple: o Create output file with a filename of the city followed by "txt". No need to check for file failure Assume it opened okay. o Write color to the file count times, with each occurrence appearing on a separate row. o Write filename to the screen. o Close output file Continue reading the input file until EOF is reached Closing input file The following is a sample input file: boston portland florence The program would create 2 output files. The first file, "boston.txt", would consist of 5 lines of "blue" blue 5 purple 15 green 8Explanation / Answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
string line;
ifstream myfile ("file.txt");
string city,color,count;
int f = 1;
while ( myfile>>line ) {
if(f%3==1) {
city = line + ".txt";
}
else if(f%3==2) {
color = line;
}
else {
count = line;
stringstream geek(count);
int cnt = 0 ;
geek >> cnt;
if(color!="purple") {
cout<<city<<endl;
ofstream outFile;
outFile.open(city.c_str());
while(cnt--) {
outFile<<color<<endl;
}
}
}
f++;
}
myfile.close();
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.