+1 (315) 557-6473 

Parsing And File I/O Homework Help in C++

The following solutions are products of a Parsing and File I/O homework help task that tests on how to use the fstream library in C++. Our C++ Homework Helper writes a program that manipulates a large stream of data into smaller chunks using the gentline function. He also writes another program that sums the ints of different lines to achieve excellent C++ Homework Help.

Parsing And File I 0 Homework Help

Writing A Program That Takes an Output File of a Specified Format in C++

Question: Write a C++ program that takes an expected output file named "input.txt" of the exact format;

(int), (int), (int)
(string)
(int), (int), (int)
(string)
(int), (int), (int)
(string)
(int), (int), (int)
(string)
(int), (int), (int)
(string)
Solution.
1,2,3
ham
1,1,1
let
Writing A Program That Sums the ints Of the Program in C++

Question: The program should then sum the ints of each line and output the immediately following line's sting, that many times, separated by comma so that;

123
ham
becomes:
ham, ham, ham, ham, ham, ham
Note: you may also add trailing commas at the end of the line if it's easier for you.
Your final output format should not be the console. Instead, your program should open a second file and write your output in a file named "output.txt" which should be five lines of comma-separated words.
Solution.
#include
#include
#include
#include
// Entry point of the program
int main()
{
 // Open and process the input file
 std::ifstream inFile("input.txt");
 std::ofstream outFile("output.txt");
 if (!inFile.is_open())
 {
  std::cout << "Error: Failed to open input.txt\n";
  return 0;
 }
 if (!outFile.is_open())
 {
  std::cout << "Error: Failed to open output.txt\n";
  return 0;
 }
 // Process each line in the file
 std::string line;
 while (getline(inFile, line))
 {
  std::stringstream ss(line);
  // Extract 3 ints and sum them up
  int total = 0;
  std::string token;
  while (getline(ss, token, ','))
  {
   std::stringstream ssInt(token);
   int value;
   ssInt >> value;
   total += value;
  }
  // Print the next string in file based on total
  getline(inFile, line);
  for (int i = 0; i < total; i++)
  {
   outFile << line;
   if (i + 1 < total)
    outFile << ",";
  }
  outFile << "\n";
 }
 inFile.close();
 outFile.close();
 std::cout << "Processing complete.\n";
 std::cout << "Output written to output.txt file.\n";
 return 0;
}
Related Blogs

C# or C++ better for game design?While there are hundreds of programming languages that can be used for gaming applications, game developers use only a few. C sharp (C #) and C + + are among the most popular languages in game design, which begs the question, “Is C# or C++ better for game design?” We...

2020-07-25
Read More

Parsing Arrays in C++An array can be defined as a data structure consisting of a group of elements. Basically, all these elements have the same data type, for example, a string or integer. Arrays are used in C + + and other programming languages to organize data or information, so that, values and v...

2020-07-25
Read More

Scheduling algorithms in C++Algorithm scheduling is the process of determining which algorithm or line of code will be executed in the Central Processing Unit (CPU) and which one will be put on hold awaiting processing. In C + + programming, scheduling ensures that there is always an algorithm avail...

2020-07-25
Read More