#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);
    }
    

    nameFile.close();

    
    system("PAUSE");
    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.

Recommended Answers

All 4 Replies

The file you posted contains three columns -- two numbers and a string. What do the first two columns represent?

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

etc.

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

Trying it out now. Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.