#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
    const int SIZE=81;
    char input[SIZE];
    fstream nameFile; 
    nameFile.open("Bands.txt", ios::in);
    if (!nameFile)
       cout << "Error: Cannot open file." << endl;
       return 0;
    nameFile.getline(input, SIZE);
    while (!nameFile.eof())
       cout << input << endl; 
       nameFile.getline(input, SIZE);


    return EXIT_SUCCESS;

Bands.txt :
1 10 Led Zeppelin
3 9 Guns 'n Roses
10 1 Mariah Carey
5 6 Pink Floyd
6 5 Pearl Jam
4 8 Boston
7 3 Bon Jovi
8 3 TLC
9 2 MC Hammer
2 9 AC/DC

I'm new at Programming and am entirely unsure of what to do. All I managed to do was print exactly what is in the txt file, but I have no idea how to loop to make them in order or change the sales to *'s.

The Question:

Read the file (there is an unknown number of bands) from Bands.txt
The number of sales is from 0 to 10
The band name is any number of strings long

Output the band information in a graphic to both the screen and a file called "results.txt" as so
1 ********** Led Zeppelin
and they are placed by rank 1-10

The file is in no particular order - at the end of the file print out total sales and the number one band based on rank.

Edited by Ilyana: n/a

6 Years
Discussion Span
Last Post by Ilyana

The first column represents the rank of the band in sales, the second is the number of sales (in thousands, millions, whichever). Led Zeppelin is Ranked #1 because he sold 10 million records. But instead of putting it like that, you must put 10 *'s instead.

So the output in the new text file should be:
1 ********** Led Zeppelin
2 ********* AC/DC
3 ********* Guns 'n Roses


Edited by Ilyana: n/a

std::string line;
while( getline(infile,line) )
   int rank;
   int sales;
   std::string name;
   stringstream str(line);
   str >> rank >> sales;
   cout << rank;
   for(int i = 0; i < sales; i++)
     cout << '*';
   cout << " " << name << '\n';

Trying it out now. Thank you!

Edited by Ilyana: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.