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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <fstream>
usingnamespace 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;
}
Your "code as a base" seems way over the top for the minimal file requirements to do your problem. Just open a file; e.g. ifstream fin( "input.txt" );
Then just keep reading an int (say, n) and a char (say, c ) until the input file is exhausted and, for each pair, use a simple loop to output c and a space n times.