How to write program that reads a file and prints lines of symbols based on its contents?

0 votes
168 views
asked Apr 14, 2021 by Betina Jessen (15 points)  
edited Jun 20, 2021 by Hitesh Garg

I'm a beginner and am working on an assignment with the following prompt: "Write a program to read from a file. The file contains pairs of a number and a symbol on each line. Your program must print out the symbol number times separated by a space for each pair (number, symbol) in the input file."

For example, with an input file that looks like this:
1 x
2 *
3 !
4 $
5 #
5 O
3 @

The output would look like this:
x
* *
! ! !
$ $ $ $

# # #

O O O O O
@ @ @

Quite frankly, I'm not sure where to start, but I at least have this code as a base:

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

int main() {
  string fname = "";
  do {
    cout << "Enter a compressed filename base (.cmp extension): ";
    cin >> fname;
  } while(fname == "");
  fname.append(".cmp");

  ifstream fin;
  fin.open(fname);
  if(fin.fail()) {
    cerr << "Could not open file " << fname << endl;
    return 0;
  }

Please log in or register to answer this question.

...