1 Replies - 171 Views - Last Post: 01 March 2018 - 10:22 PM Rate Topic: -----

#1 JeepLife   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-March 18

STUCK! Parallel Arrays read from Input File

Posted 01 March 2018 - 09:52 PM

I am having issues resolving this. I have scoured looking for what I am missing. I really do not understand char arrays. Our professor doesn't want us using strings. She will only allow Cstrings or char arrays in this assignment. Can someone help me to clear up my frustration? Any and all help is greatly appreciated.

These are my instructions:
Instead of opening and closing the input file for each requested item, open the stock.txt file, read all the values for the 9 stock items into three parallel arrays, then close the input file. When the user gives you a stock number you should search the arrays for the match and the corresponding description and price.

I am providing you a file called Stock.txt that contains information regarding the 9 different stock items carried by FUNNY STUFF RETAIL INC. The file is a repeating sequence of three records such that the first record represents an item code number, the second record represents the description of each item and the third record contains the current selling price. Then the sequence repeats. Your program is to simulate the checkout register allowing the check out clerk to input item codes to obtain the current selling prices and descriptions. If an incorrect code is entered, the clerk should be notified by an appropriate message on the screen. If the item is found, the clerk will enter the number of items with that description and price and continue to the next item. When all items have been entered the clerk terminates the transaction by entering an item code of 0 (zero). The program will build a sales slip called Sales.txt enumerating the item code, description, unit price, quantity purchased, and total extended price. The bill will be subtotaled and an 8% sales tax added for a final total. Amount tendered by the customer and change given will be finally added to the invoice. This invoice should be written out to an output file called Sales.txt. The final total and change should appear on the screen as well.
Your output file should line up all columns and output formatted for dollars and cents on money amounts, using setprecision, showpoint, and fixed. Make sure you format your output to the screen as well.

This is the 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 what I have so far:
#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;
    
 	ifstream inputFile;
    
    inputFile.open("Stock.txt");
	if ( !inputFile )
      {
         cout << "The file (stock.txt) Failed to open." << endl;
         return 0;
      }

	int itemNum_Array[9];
	string description_Array[9];
	double price_Array[9];

 

	for (int x = 0; x < 9; x++)
	{
   	inputFile >> itemNum_Array[x] >> description_Array[x] >> price_Array[x];
    }
   
   inputFile.close();
   
   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;                                    

      bool found = false;                                          
      	
      	for (int i = 0; i < 9; i++){
  			 if (itemNum == itemNum_Array[i])
       		  {
        		    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;
      }

      
   }

   	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;
}



I can not figure it out to save my life. I had to use a string because that is the only thing I understood how to use. I do not understand how to set it up into a char array or a Cstring. Either way I am not getting the desired output.

Is This A Good Question/Topic? 0
  • +

Replies To: STUCK! Parallel Arrays read from Input File

#2 jimblumberg   User is offline

  • member icon

Reputation: 5487
  • View blog
  • Posts: 17,063
  • Joined: 25-December 09

Re: STUCK! Parallel Arrays read from Input File

Posted 01 March 2018 - 10:21 PM

What don't you understand about C-strings?

Do you understand that C-strings are arrays of char terminated by the end of string character ('\0')?

Your instructions say you need an array of C-strings, this means that you need a multidimensional array. The first dimension will be the number of elements in the array the second dimension is the maximum length of the C-string. Something like: char description[NUM_ITEMS][MAX_STRING_SIZE];. Where NUM_ITEMS is a const that holds the maximum number of your inventory, in this case 9: const int NUM_ITEMS = 9, and it looks like the MAX_STRING_SIZE can be something like 30 (since all of your strings appear to be smaller than this number): const int MAX_STRING_SIZE = 30;. These constants must be declared before any of your other variables, possibly even in the global scope prior to any of your functions.

And note you should never try to retrieve information into a C-string with a function that doesn't limit the number of characters it will try to retrieve to avoid possible buffer overrun problems. I recommend using getline() to retrieve these C-strings, but be sure you use the proper version of getline() that works with C-strings.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1