0

The programs description: Write a program that reads a data file named sales.dat. The program should then display a bar graph comparing each store’s sales and also output the sales bar graph to a file names results.dat. Create each bar in the bar graph by displaying a row of asterisks. Each asterisk should represent $100 of sales.

It's using 2 resource files in visual studio, sales.txt and results.txt. So basically, I had the program working, and I submitted it for my assignment. I re-opened it today to mess with it, and it's completely stopped working. It appears to not be reading from sales.txt. sales.txt just has random integers in it on each line. example, 1000, 1200, 1400. The output would show 10, 12 and 14 asterisks on each line in results.txt and in the console.

the issue is that it says store 1: store 2: and so forth, but shows no asterisks.

       int main() {

int storeAmount = 0;
double sales = 0.0;
int starAmount = sales / 100; //gets the amount of stars needed
ifstream inFile("sales.txt"); //opens the file sales.txt which is a resource file
ofstream outFile("results.txt"); //opens the file results.txt which is our output file
string outputStars = ""; //holds the stars for the bar graph

cout << "Please input the amount of stores:" << endl; //input the amount of stores
cin >> storeAmount;

    cout << "SALES BAR CHART:" << endl; //header output
    cout << "Each * = $100" << endl;

    for (int storeNum = 0; storeNum < storeAmount; storeNum++) { //loops to the max store amount

        inFile >> sales; //variable sales holds the value of each of the lines in sales.txt


        for (int i = 0; i < starAmount; i++) { //adds stars onto the string
            outputStars += "*";
        }

        cout << "Store " << storeNum + 1 << ": " << outputStars << endl; //ouputs in the console
        outFile << "Store " << storeNum + 1 << ": " << outputStars << endl; //outputs to the file

        if (inFile.eof()) { //stops the duplication of the last line if the store amount is greater than the numbers in sales.txt
            break;
        }

    }

outFile.close(); //closes the files
inFile.close();

system("pause");
return 0;
}
Cory
  • 23
  • 1
  • 8

2 Answers2

0
int starAmount = sales / 100;

Needed to be after I assign sales.

jpo38
  • 20,821
  • 10
  • 70
  • 151
Cory
  • 23
  • 1
  • 8
0

Combining all the advices together - I have slightly changed your code adding comments about changes made and some tasks to be done to finish the program

#include <string>
#include <iostream>
#include <fstream>

//format your code properly
int main() {

    std::ifstream inFile("sales.txt"); //opens the file sales.txt which is a resource file
    std::ofstream outFile("results.txt"); //opens the file results.txt which is our output file
    //todo - check that files are opened

    std::cout << "Please input the amount of stores:\n"; //input the amount of stores

    int storeAmount = 0; //declare var as close to usage as possible
    std::cin >> storeAmount;
    //todo use if(std::cin >> storeAmount) instead to check that input was proper

    std::cout << "SALES BAR CHART:\n"; //header output
    std::cout << "Each * = $100\n";

    for (int storeNum = 0; storeNum < storeAmount; storeNum++) { //loops to the max store amount

        //declare var as close to usage as possible
        double sales = 0.0;
        //check the result of input operation before using the value read
        //instead of using eof postfactum
        if(inFile >> sales) { //variable sales holds the value of each of the lines in sales.txt

            //declare variable in the smallest scope possible
            int starAmount = sales / 100; //gets the amount of stars needed

            //use constructor instead of loop
            std::string outputStars(starAmount, '*'); //holds the stars for the bar graph

            std::cout << "Store " << storeNum + 1 << ": " << outputStars << '\n'; //ouputs in the console
            outFile << "Store " << storeNum + 1 << ": " << outputStars << '\n'; //outputs to the file
        }
        else {
            break;
        }
    }

    outFile.close(); //closes the files
    inFile.close();

    return 0;
}
Artemy Vysotsky
  • 2,694
  • 11
  • 20