Program not reading file

It has been a while since I looked at c++ so my memory is seriously lacking. I'm probably making a really stupid mistake. I had this working in a single file but when I tried breaking it out into a .cpp and an .h file, it won't read my file correctly anymore. The program compiles but when I enter a valid inventory item, it isn't reading it as valid. Any help is really appreciated!

.h File
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
#ifndef STORE_H
#define STORE_H
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class Store
{
private:
     int productNumber;
     string description;
     double price;
     char tax;
public:
	Store(){}
	
	Store(int n, string d, double p, char t)
	{
		productNumber = n;
		description = d;
		price = p;
		tax = t;
	}
          
	void Store::File(int n, string d, double p, char t)
        {
	        Store invList[50];
	        Store receipt[50];
	        ifstream inFile;
	        inFile.open("Invent.txt");

	       for (int index = 0; index < 50; index++)
	       {
                     inFile >> invList[index].productNumber;
		     inFile >> invList[index].description;
		     inFile >> invList[index].price;
		     inFile >> invList[index].tax;
	       }
	      inFile.close();
       }    

    void Store::setProductNumber(int n)
     { productNumber = n; }
     
     void Store::setDescription(string d)
     { description = d; }

     void Store::setPrice(double p)
     { price = p; }

     void Store::setTax(char t)
     { tax = t; }

     int Store::getProductNumber() const
     { return productNumber; }

     string Store::getDescription() const
     { return description;}

     double Store::getPrice() const
     { return price; }

     char Store::getTax() const
     { return tax; }
};

#endif 


.cpp File
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
#include <iostream>
#include "Store.h"
#include <iomanip>
#include <fstream>

int main()
{
     int number;
     int count = 1;
     bool found;
     char doContinue;
     double subtotal = 0;
     double total = 0;
     double totalTaxes = 0;
     int times = 0;

     ofstream outFile("Receipts.out");
     Store product[50];
	
   do
    {
	   outFile << "Customer " << count << endl;
       outFile << setw(13) << left << "Item " << setw(9) << "Quantity " << setw(6) << "Cost " << setw(8) << "Total" << endl;
       do
       {
		   cout << "Enter Product Number or 0 to exit: " << endl;
           cin >> number;
           found = false;

           for (int i = 0; i < 50; i++)
           {
			   if (number == product[i].getProductNumber())
               {
                cout << "Quantity: " << endl;
                cin >> times;
                // if (times < 1 || times > 100)
                // outFile << " *** Quantity Invalid *** " << endl;
                found = true;
                outFile << setw(13) << left << product[i].getDescription() << setw(4) << times << setw(5) << "@ " << setw(6)
                        << product[i].getPrice() << setw(8) << times * product[i].getPrice() << setw(3) << product[i].getTax() << endl;

                subtotal = subtotal + (product[i].getPrice() * times);
                if (product[i].getTax() == 'T')
					totalTaxes = totalTaxes + ((product[i].getPrice() * times) * 0.075);
                }
                         
               if (found == false)
               {
                outFile << "*** Item ***" << product[i].getProductNumber() << " not in inventory *** " << endl;
                }
		   }
	   } while (number != 0);
	   count++;
       total = subtotal + totalTaxes;

       outFile << setw(14) << right << "Subtotal " << setprecision(2) << fixed << subtotal << endl;
       outFile << setw(14) << "Tax " << setprecision(2) << fixed << totalTaxes << endl << endl;
       outFile << setw(6) << "Total " << setprecision(2) << fixed << total << endl;
       outFile << "--------------------------------------------------------------------------" << endl << endl;

       subtotal = 0;
       totalTaxes = 0;
       total = 0;

       cout << "Would you like to enter another customer (Y/N)? " << endl;
       cin >> doContinue;
   } while (doContinue != 'n' && doContinue != 'N');
	cout << "Thank you" << endl;
    return 0;
};


Inventory File
1
2
3
4
5
11111 Milk 3.49 T
22222 Bagels 4.99 T
33333 Butter 1.29 T
44444 Bread 2.59 N
55555 Eggs 2.99 T
Last edited on
Hello shawnap,

Until I can load the program into my IDE and give it a good test this is what I see for now.

In the header file you do not need the "#include"s. There may be some rare occasions where this is need, but The "#include"s should be in the ".cpp" files.

Lines 17 - 25 look OK for now.

Lines 27 on defined the way they are should be in a ".cpp" file. It is OK if you define these functions in the class like you have, but the "Store::" qualifier is not needed because you are in the class. Done in a separate ".cpp" file the qualifier is needed.

On line 32 in the class you open a stream, but never check to see if it is open. When opening an input file you always need to check that the file has opened before you try to use it. Otherwise the program will continue, but nothing will be input.

In main line 17 you define and ofstream and open the file on creation of the stream. On an output stream you do not need to check if the file is open because if the file does not exist it will create the file, but I still like to check to be sure the file opened.

The rest of main looks OK until I test it.

Hope that helps,

Andy

P.S. This is worth reading:
http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/
Last edited on
Thanks for the response Andy,

When I remove the includes from the h file it blows up with about 80 errors. I realized I did not have the using namespace std in my cpp file. Once I added that (and removed it from my h file) it dropped it down to about 27. I've done a lot of copying and pasting and moving around. I didn't even realize I had put that there. I will take a peek at that article though. ;) This may help identify some underlying issues. Also, when I remove the Store:: qualifier it gives me an error saying it isn't defined. I will pick at this some more when I get home in a bit.
Hello shawnap,

With two changes in main first adding the header file "<string>" and second see the comment on the very last line.

The header file I broke up into two files, the ".h" file for the class, and the, ".cpp" file, for the functions. Notice where I put the "#include" statements. And notice the absence of "using namespace std;" in any file. This line WILL get you in trouble some day. Best to learn the use "std::" to qualify what is in the std name space and learn what is in this namespace early while it is easy.

These are the files I have used:

Main.cpp
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
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

#include "Store.h"

int main()
{
	int number;
	int count = 1;
	bool found;
	char doContinue;
	double subtotal = 0;
	double total = 0;
	double totalTaxes = 0;
	int times = 0;

	std::ofstream outFile("Receipts.out", std::ios::trunc);
	Store product[50];

	do
	{
		outFile << "Customer " << count << std::endl;
		outFile << std::setw(13) << std::left << "Item " << std::setw(9) << "Quantity " << std::setw(6) << "Cost " << std::setw(8) << "Total" << std::endl;
		do
		{
			std::cout << "Enter Product Number or 0 to exit: " << std::endl;
			std::cin >> number;
			found = false;

			for (int i = 0; i < 50; i++)
			{
				if (number == product[i].getProductNumber())
				{
					std::cout << "Quantity: " << std::endl;
					std::cin >> times;
					// if (times < 1 || times > 100)
					// outFile << " *** Quantity Invalid *** " << std::endl;
					found = true;
					outFile << std::left << std::setw(13) << product[i].getDescription() << std::setw(4) << times << std::setw(5) << "@ " << std::setw(6)
						<< product[i].getPrice() << std::setw(8) << times * product[i].getPrice() << std::setw(3) << product[i].getTax() << std::endl;

					subtotal = subtotal + (product[i].getPrice() * times);
					if (product[i].getTax() == 'T')
						totalTaxes = totalTaxes + ((product[i].getPrice() * times) * 0.075);
				}

				if (found == false)
				{
					outFile << "*** Item ***" << product[i].getProductNumber() << " not in inventory *** " << std::endl;
				}
			}
		} while (number != 0);
		count++;
		total = subtotal + totalTaxes;

		outFile << std::setw(14) << std::right << "Subtotal " << std::setprecision(2) << std::fixed << subtotal << std::endl;
		outFile << std::setw(14) << "Tax " << std::setprecision(2) << std::fixed << totalTaxes << std::endl << std::endl;
		outFile << std::setw(6) << "Total " << std::setprecision(2) << std::fixed << total << std::endl;
		outFile << "--------------------------------------------------------------------------" << std::endl << std::endl;

		subtotal = 0;
		totalTaxes = 0;
		total = 0;

		std::cout << "Would you like to enter another customer (Y/N)? " << std::endl;
		std::cin >> doContinue;
	} while (doContinue != 'n' && doContinue != 'N');

	std::cout << "Thank you" << std::endl;

	return 0;
}  // <--- ; Not needed. 


Store.h
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
#ifndef STORE_H
#define STORE_H

class Store
{
private:
	int productNumber;
	std::string description;
	double price;
	char tax;

public:
	Store();
	~Store();

	Store(int n, std::string d, double p, char t);
	void File(int n, std::string d, double p, char t);

// ****** Setters ************
	void setProductNumber(int n);
	void setDescription(std::string d);
	void setPrice(double p);
	void setTax(char t);

// ****** Getters ************
	int getProductNumber() const;
	std::string getDescription() const;
	double getPrice() const;
	char Store::getTax() const;
};

#endif  


Store.cpp
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
#include <iostream>
#include <fstream>
#include <string>

#include "Store.h"

Store::Store()  // <--- Default ctor.
{
	productNumber = 0;
	description = "";
	price = 0.0;
	tax = '\0';
}

Store::~Store() {}  // <--- Default dtor.

Store::Store(int n, std::string d, double p, char t)  // <--- Overloaded ctor.
{
	productNumber = n;
	description = d;
	price = p;
	tax = t;
}

void Store::File(int n, std::string d, double p, char t)
{
	Store invList[50];
	Store receipt[50];
	std::ifstream inFile;
	inFile.open("Invent.txt");

	for (int index = 0; index < 50; index++)
	{
		inFile >> invList[index].productNumber;
		inFile >> invList[index].description;
		inFile >> invList[index].price;
		inFile >> invList[index].tax;
	}
	inFile.close();
}

void Store::setProductNumber(int n)
{
	productNumber = n;
}

void Store::setDescription(std::string d)
{
	description = d;
}

void Store::setPrice(double p)
{
	price = p;
}

void Store::setTax(char t)
{
	tax = t;
}

int Store::getProductNumber() const
{
	return productNumber;
}

std::string Store::getDescription() const
{
	return description;
}

double Store::getPrice() const
{
	return price;
}

char Store::getTax() const
{
	return tax;
}


This compiled and ran with no problem.

When the program ran the output file opened and was written to along with asking for input of product numbers. Product numbers are limited by entering zero to quit, the quantity asked for all 50. I would revise this to enter product number followed by a check for zero followed by entering the quantity before it loops back for the next product number. This way you enter one product number and quantity at a time.

Without information in the variable "product" to work with the output to the file is not much right now.

Hope that helps,

Andy
Thanks Andy.

Unfortunately, I seem to still be having the same issue. It compiles fine but it still isn't recognizing the inventory item numbers. It should asking for the inventory item number. If it is found it should prompt for the quantity. If it isn't found it should skip the quantity questions and output a message to the file and ask for the inventory item number. When I put in say 11111 as the inventory item it should be prompting me for the quantity as that is a valid inventory item number. Instead it is skipping and writing a message to the output file.
I added a cout statement to see what it was actually pulling for the product number to compare against and it looks like it is returning the location rather than the value.....
Last edited on
Hello shawnap,

I added a cout statement to see what it was actually pulling for the product number to compare against and it looks like it is returning the location rather than the value.....

In your code it is returning a garbage value because "productNumber" was never given a value to start with. In my version it would return zero.

The problem is that by the time you reach the if statement you have created the array of classes, but have never given any of these classes in the array any values to use.

You might want to call the function that reads the file before you enter the do/while loops.

Hope that helps,

Andy
Thanks Andy.
Topic archived. No new replies allowed.