ifstream empties file

closed account (EwCjE3v7)
For some reason the following program prints out No data?! Even when I have data in the file. It empties the file input.txt that has the data. Here is the main function and the class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Keyboard keys \ |
#include <iostream>
#include <fstream>
#include "Sales_data.h"
using std::cout; using std::cerr; using std::endl; using std::cin;
using std::ifstream; using std::ofstream;

int main(int argc, char *argv[])
{
	ifstream input (argv[0]);
	ofstream output (argv[1]);
	Sales_data total;
	if (read(input, total)) {
		Sales_data trans; // define trans to work more or the same book 

		while (read(input, trans)) { // while we get input into trans
			if (total.isbn() == trans.isbn()) {     // if we are proccessing the same book
				total.combine(trans);
			} else { // if we get input for another book
				print(output, total); // print the results of the previous book

				// assign total the value of the new book
				total = trans;
			}
		}
		print(output, total); // print the results of the previous book
	} else 
		cerr << "No data?!" << endl;

	return 0;
}

#ifndef SALES_DATA_H
#define SALES_DATA_H

#include <iostream>
#include <string>

class Sales_data; // declaration for the following class

std::istream &read(std::istream &, Sales_data &); // declaration for read, used inside the class

class Sales_data {
public:
  friend std::istream &read(std::istream &, Sales_data &);
  friend std::ostream &print(std::ostream &, const Sales_data &);
  friend Sales_data add(const Sales_data &, const Sales_data &);

  // CONSTRUCTORS
  Sales_data() = default; // needed

  Sales_data(const std::string &s, unsigned n, double p) : bookNo(s), units_sold(n), revenue (p*n) {} // if we get a string, unsigned and a double then initialize the members of the class

  Sales_data(const std::string &s) : Sales_data(s, 0, 0) {} // if we just get a string as a initializer then assign that to bookNo

  Sales_data(unsigned n, double p) : Sales_data("", n, p) {}


  Sales_data(std::istream &is) // if we get an istream
  {
    read(is, *this); // call read to assign the current object
  }


  std::string isbn() const // funtion to call for the ISBN of the current book
  {
   	 return bookNo; // return the book numbers
  }

  Sales_data& combine (const Sales_data&rhs) // to combine two Objects of Sales_data
  {
  	units_sold += rhs.units_sold; // add the amount of books sold
  	revenue += rhs.revenue;       // for adding the revenue
  	return *this;                 // return the object that the funtion was called on
  }

public:
	inline double avg_price() const // for calculating the average price
  {
		if (units_sold)
      return revenue/units_sold; // return: devide revenue by the amount of units sold
  	else
  	 return 0;                   // else return nothing
  }

  std::string bookNo;      // for holding our book number/ISBN
  unsigned units_sold = 0; // for holding the amount of books sold
  double revenue = 0.0;    // for holding the price they were sold at
};


std::istream &read(std::istream &is, Sales_data &item) // funtion for getting input into a Sales_data object
{
  double price = 0;                              // for getting our price that the objects were sold at
  is >> item.bookNo >> item.units_sold >> price; // get input
  item.revenue = price * item.units_sold;        // calculate the revenue
  return is;
}

std::ostream &print(std::ostream &os, const Sales_data &item) // funtion for output
{
  os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price(); // print out values
  return os;        // return the output stream
}

Sales_data add(const Sales_data &lhs, const Sales_data &rhs) // for adding to Sales_data objects
{
  Sales_data sum = lhs; // sum for returning
  sum.combine(rhs);     // combine sum with rhs
  return sum;           // return the combined object
}

#endif // SALES_DATA_H
The command line parameters are not being used correctly.
argv[0] is the name of the current program
argv[1] is the first command-line parameter
argv[2] is the second command-line parameter

Also, you should check argc==3 to confirm that the parameters are actually present.
Last edited on
closed account (EwCjE3v7)
Oh, thank you that fixed it. And I thought my book was wrong for writing argv[1] and 2 :)
Topic archived. No new replies allowed.