Question
I have the following C++ code that is close to working. The program takes an input file in the format below. (int),(int),(int) (string) It generates
I have the following C++ code that is close to working. The program takes an input file in the format below.
(int),(int),(int)
(string)
It generates an output.txt file but for some reason mine is blank. How do I get this output.txt to display what I want it to? The rules I were following were as follows:
Your program should then sum the ints of each line and output the immediately following line's string, that many times, seperated by commas such that: 1,2,3 ham becomes: ham,ham,ham,ham,ham,ham
#include
int myAtoi(std::string str) { int result = 0;
for(int i=0; i< str.length(); ++i) result = result * 10 + str.at(i) - '0';
return result; }
int main() { std::fstream file1, file2; std::string word, filename1, filename2;
filename1 = "input.txt"; file1.open(filename1.c_str()); file2.open("output.txt",std::fstream::out);
for(int i=0; i<5; i++) { int sum = 0; file1 >> word; sum += myAtoi(word); file1 >> word; sum += myAtoi(word); file1 >> word; sum += myAtoi(word); file1 >> word; for(int j=0; j file2 << " "; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started