0

I'm building a program for my class the requires us to import data from a .txt file and the item names/ prices will be used to calculate saltes tax and grand total. My teacher put this up as an example, but I can't get it to run.

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

int main()
{ // Beginning of main function

string name;
ifstream data_in;
ofstream data_out;

int cnt=0;
int number;

struct Item
{
    int item_n;
    char disc[50];
    float price;
};

Item store[999];

data_in.open("cost.txt");
data_out.open("file_out.txt");

while(!data_in.eof())
{
    //cout << "Enter in the item number: ";
    data_in >> store[cnt].item_n;
    //cout << "Enter in the description for item number " << store[cnt].item_n << ": ";
    data_in >> store[cnt].disc;
    //cout << "Enter in the price for the " << store[cnt].disc << ": $";
    data_in >> store[cnt].price;
    cnt++;
}
cout << endl << endl;

number = cnt;
for (cnt=0; cnt<number; cnt++)
{
    name = store[cnt].disc;
    cout << setw(5) << store[cnt].item_n << "   " << store[cnt].disc << setw(16-name.length()) << "$" << setw(9) << store[cnt].price << endl;
}

for (cnt=0; cnt<number; cnt++)
{
    name = store[cnt].disc;
    data_out << setw(5) << store[cnt].item_n << "   " << store[cnt].disc << setw(16-name.length()) << "$" << setw(9) << store[cnt].price << endl;
}


return 0;
}

And this is the information in the cost.txt file

Books 45.01 Pens 21.03 Pencils 10.90 Hats 50.00 Caps 800.00 Food 1.00

Bnstates
  • 13
  • 1
  • 1
    Did you check to make sure your input and output files actually opened? – Stephen Apr 22 '16 at 23:35
  • 1
    Be more precise please. "I can't get it to run" is useless - What does it do ? – Unimportant Apr 22 '16 at 23:37
  • When you used the debugger, which statement was causing the issue? – Thomas Matthews Apr 22 '16 at 23:37
  • 2
    And if you got `while(!data_in.eof())` from a teacher you have my permission to fire a big raspberry at that teacher. More here: http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – user4581301 Apr 22 '16 at 23:39
  • Define "I can't get it to run". What does that mean? – Lightness Races in Orbit Apr 22 '16 at 23:39
  • Agreed with user4581301. That while loop is completely ineffective and wrong. Also, C-strings. And `Item store[999];`? Magic numbers, yay. Can probably forgive that for an introductory class but not ideal. – Lightness Races in Orbit Apr 22 '16 at 23:40
  • Sorry. What i meant when I can't get it to run is.. I build it, it builds completely fine. When I run it, however, a blank screen comes up. Sorry, I'm not really that good witht his stuff yet. – Bnstates Apr 22 '16 at 23:51

2 Answers2

0

Try the following changes, the rest of your code seems to work:

data_in.open("cost.txt");
data_out.open("file_out.txt");
if (!data_in.is_open() || !data_out.is_open())  //Test if files opened correctly...
{
    cout << "Failed to open a file!\n";
    return 1;
}

float SalesTotal = 0;
while(true)
{
     if (!(data_in >> store[cnt].disc))  
     {
         //Failed to read first element in this record - could be eof or format error
         if (data_in.eof())
             break;         //eof - all records read.

         cout << "Format error first field\n";
         return 1;          //format error on first field.
     }

     if (!(data_in >> store[cnt].price))
     {
         cout << "Format error second field\n";
         return 2;          //format error on second field.
     }

     store[cnt].item_n = cnt + 1; //Item number is not a field in your file, use the counter...

     SalesTotal += store[cnt].price;
     cnt++;
}

if (!cnt)
{
    cout << "No records read\n";
    return 3;   //No valid records read.
}

float GrandTotal = ((SalesTotal / 100) * 6) + SalesTotal;
cout << "Sales total: " << SalesTotal << " Grand total:" << GrandTotal << "\n";
Unimportant
  • 2,076
  • 14
  • 19
  • Do you think you might be able to help me with one other thing? In the program, I have to calculate the sales total of all the items, add 6% tax, and grand total. What formula could I use? I tried the standard total_sales = total_sales + store[cnt].price , which is how i did it on other projects, but it doesnt work with the store[cnt].price – Bnstates Apr 23 '16 at 01:33
  • @Bnstates : Updated my answer. – Unimportant Apr 23 '16 at 01:43
0

The code you've written reads three things for each Item: the item number, the description, and price.

The sample data file you shown contains only two things for each item: what looks to be a description, and a price.

The expected data format does not match the contents of the input file. This code will never work, as is. One or the other is wrong. Plus all the other problems with the code, as mentioned in the comments.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148