Homework Help

I need some help. This program needs to be able to read sales data from a text file. The items it needs are a product number which is an integer, a product name which is a string no longer than 12 characters and which does not contain spaces, a unit price which is a sold, a number of units sold which is an integer. I have done all these things and cannot get it to read properly from the text file. :/

int main() {

ifstream salesFile;
string productName;
int unitsSold, productNumber;
double unitPrice;
Data* productArray[100];
Data* aProduct;
int numProduct = 0;
salesFile.open("sales.txt");

while (salesFile >> productName )
{

salesFile >> unitPrice >> unitsSold >> productNumber >> productName;
aProduct = new Data;
aProduct->name = productName;
aProduct->number = productNumber;
aProduct->sold = unitsSold;
aProduct->price = unitPrice;

productArray[numProduct++] = aProduct;

cout << aProduct->number << " " << "The Product is " << aProduct->name << " With Total Sales of $ " << aProduct->sold << " " << endl;
}
Last edited on
> The items it needs are a
>> product number which is an integer,
>> a product name which is a string no longer than 12 characters and which does not contain spaces,
>> a unit price which is a sold,
>> a number of units sold which is an integer.
So,
a) why are you reading productName first, when apparently the first field is a number.
b) why are you reading produceName twice.

You start with this, to convince yourself that you can read the file properly and consistently.
1
2
3
while ( salesFile >> productNumber >> productName >> unitPrice >> unitsSold ) {
  cout << "Data=" << productNumber << endl;
}


Only then do you start adding all your data processing code.

//Coded by: Mitchell Godbold
// This program will allow you to read sales data from a given text file and which will display the name and revenue
// of the top two selling products given.

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

using namespace std;


struct Data
{
string name;
int number;
double price;
int sold;
};
int main() {

ifstream salesFile;
string productName;
int unitsSold, productNumber;
double unitPrice;
Data* productArray[100];
Data* aProduct;
int numProduct = 0;
salesFile.open("sales.txt");

while (salesFile >> productNumber >> productName >> unitPrice >> unitsSold)
{


aProduct = new Data;
aProduct->name = productName;
aProduct->number = productNumber;
aProduct->sold = unitsSold;
aProduct->price = unitPrice;

productArray[numProduct++] = aProduct;

cout << productNumber << " " << productName << " " << unitPrice << " " << unitsSold << endl;

}

So I FINALLY figured out the issue but I still need to figure out where to place a bubble sort in the program to sort them by revenue and then find a spot to place a function to find the first highest revenue and the second. If you could help me figure out the code for these or even where to place them it would be a major help
If you do nothing else, PLEASE learn to use code tags. They make it easier to read and comment on your source.

http://www.cplusplus.com/articles/jEywvCM9/

Hint, you can edit your posts and add code tags.
Why do you have an array of pointers?
Topic archived. No new replies allowed.