Help setting up 3 Parallel Arrays from input file to be searchable by program

I am struggling to figure this one out. I have looked in the textbook and scoured for hours online looking at example after example and still not figuring it out.

input file: Stock.txt

1234
Stockings
12.39
2865
Pajamas
17.99
4020
Dress
23.00
3640
Sweater
20.50
5109
Shorts
56.99
4930
TV
88.20
6600
ToothBrush
94.55
5020
AluminumPan
16.79
2336
Pencils
4.55



This is the program that I must alter:
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

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

int main ()
{
   int itemNumber;          
   int itemNum;             
   string description;
   float price;
   int amt;
   float total = 0;
   float subtotal;

   ofstream outputFile( "Sales.txt" );
   outputFile << "FUNNY STUFF RETAIL, INC." << endl;
   outputFile << endl;
   
   while( true )                                                    
   {
      cout << "Please give me an item number that you would like me to look up ? (0 when done)" << endl;
      cin>> itemNum;

      if ( itemNum == 0 ) break;                                    

      ifstream inputFile( "stock.txt" );                             
      if ( !inputFile )
      {
         cout << "The file (stock.txt) Failed to open." << endl;
         return 0;
      }

      bool found = false;                                            
      while ( inputFile >> itemNumber >> description >> price )      
      {
         if ( itemNum == itemNumber )
         {
            found = true;
            break;
         }
      }

      if ( found )
      {
      	 cout << fixed << showpoint << setprecision (2);
      	 cout << "Item number : " << itemNum << " is pulling up : " << description << ", at $" << price << " per Item." << endl;
         cout << "How many " << description << " would you like to purchase? " << endl;
         cin >> amt;
         total = total + ( amt * price );
         found = false;
         outputFile << "Item Number :  " << itemNum << endl;
		 outputFile << "Item Description :  " << description << endl;
		 outputFile << "Price per Item :   $" <<price << endl;
		 outputFile << "Amount Purchased :  " << amt << endl;
		 outputFile << "Item Subtotal :   $" << (amt * price) << endl;
		 outputFile << endl;
         cout << "OK, I've added " << amt << " " << description << " to your order." << endl;
      }

      else
      {
         cout << "I'm sorry, but we do not appear to carry that item in stock." << endl;
      }

      inputFile.close();
   }

   	outputFile << fixed << setprecision( 2 );                           
  	cout << fixed << setprecision( 2 );
	cout << fixed << showpoint << setprecision (2);
	outputFile << "Combined Subtotal : $" << total <<endl;
	subtotal = total;
	outputFile << fixed << showpoint << setprecision (2);
	outputFile << "Tax : $" << (total * .08)<<endl;
 	total = total + (total * .08);
	outputFile << fixed << showpoint << setprecision (2);
	outputFile << "Total : $" << total <<endl;
	cout << fixed << showpoint << setprecision (2);
	cout << "Your total comes to : $" << total << endl;
	cout << "How much would you like to pay? " << endl;

   float money, moreMoney, change;
   cin >> money;
   
   while ( money + moreMoney <= total )
   {
      cout << "I'm sorry, but that is not enough money to cover the total. How much more are you going to pay : ";
      cin >> moreMoney;
   }
   change = money  - total;
   outputFile << fixed << showpoint << setprecision (2);
   outputFile << "Amount Tendered :  $" << money + moreMoney <<endl;
   change = money + moreMoney - total;
   cout << "Your change is : $" << change << endl;
   outputFile << "Your change is : $" << change << endl;
   cout << fixed << showpoint << setprecision (2);
   cout << "Your change is : $" << change << endl;
   outputFile << endl;
   outputFile << "Thank You for Shopping with Us" <<endl;
   outputFile << "Have a Great Day !" <<endl;

   outputFile.close();
   
   return 0;


Instead of opening and closing the input file for each item number entered, I need to open the file and read it into an array. Then instead of searching the input file for a match the program needs to search the parallel arrays instead. I have figured it out using strings, but the char array is throughing me through a loop. This is what I have so far, and it isn't working.
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

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

void readData(int [], char[], float[]);

int main()
{
	int array_size = 9;
	int name_size = 20;
	int itemNumber[array_size];
	char description[array_size][name_size];
	float price[array_size];
	int i;
	
	
	
	readData(itemNumber, description, price);
	for (int j = 0; j < 9; j++)
	{
		cout << itemNumber[j] << endl;
		cout << description[j] << endl;
		cout << price[j] << endl;
	}

	
	return 0;
}


void readData(int itemNumber[], char description[], float price[])
{
	int i;
	int num_characters = 0;
	
	ifstream inputFile;
	inputFile.open("Stock.txt");
        int k=0;
        while(!inputFile.eof())
        {
        	inputFile.get(description[i]);
        	i++;
        	num_characters ++;
    	}
    
    	while (inputFile)
                if(k==9)
                {
                      break;
                }
		inputFile >> itemNumber[k] >> description[k] >> price[k];
                k++;
                
                inputFile.close();
return 0;
} 


I am not looking for anyone to do the work for me, just need help to understand and getting the program working. I appreciate any and all help.
Thank You in advance.
Last edited on
Hello Cplusstudent01,

I do not know why I missed this on, but happened to find it if yo still need help,

Line 9 I would put above main as const unsigned int ARRAYSIZE{9};. Because on lines 13 - 15 what is between the [] needs to be a constant like 9 or a variable defined as a constant. This way if you need to make a change it is only in one place.

Line 14. Defines a 2D array yet the function expects a 1D array. The other thing I would do is make line 14 a "std::string" and not a char array. You will find the string is much easier to use.

On line 41 for the condition it is best not to loop on "eof".

while(!inFile.eof())

I do not know why people teach this. This is a bad idea as it does not work the way you think it will. Generally by the time the while condition determines that "eof" has been reached you will have processed the last read twice and wondering why it shows up twice.

What happens is the while condition is checked and “eof” has not been set yet. After entering the while loop you read the last line or bit of information from the file process the information reach the bottom of the while loop. At this point “eof” has not been set yet and you return to the while condition which is still good. After entering the loop you try to read from the file, but now there is nothing left to read and “eof” is set, but you do nothing at this point to deal with the “eof” condition. So you process what information is in the variables from the last read before returning to the while condition where it finally detects the “eof”, but you have processed the last good read twice unless you have cleared the variables at the end of the while loop in which case the last process will be blank.
A more acceptable way of using the while loop is:

1
2
3
4
5
while (infile >> someVariable)
{
    infile >> additionalVariables;
    //  Additional code.
}


In the above example you can set up the while condition to read one variable, two or more variables or use “std::getline(?, str)” where ? could be from “std::cin” or from a file stream and “str” represents any “std::string” variable.
inside the while loop you will need to read into all three variables and I would change the input for description to a "std::getline(...)"

The second while loop has more potential except that you are missing the {}s for the while block.

If you put the two while loops together you will have something.

Hope that helps,

Andy

P.S. If this gets archived you may have to start a new message in April.
Topic archived. No new replies allowed.