• 0

C++ Help FAST!


Question

I have the following code, that deals with a grocery store and items, when I run it after compling it(which it finds no errors) and then go to debug it it starts with the MS-Dos Screen and then just sits there. It is suppose to say in the Ms-Dos Screen, press any key to continue. But it doesn't can someone please look at it for me and maybe lend a helping hand. I am totally lost in this class and do not know why it is doing it!

Thank you :yes:

here is the code since I don't know how to upload it

// Lindsay Schiller
//
// Grocery
// 10/28/04

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

const int maxs = 100;


void init(int id[maxs], int unitssold[maxs], double unitprice[maxs],
    double unitsales[maxs])
{
	int i;
	for (i = 0; i < maxs; i++)
	{
  id[i] = -1;
  unitssold[i] = 0;
  unitprice[i] = 0;
  unitsales[i] = 0;
	}
}

void reedem (int id[maxs], int unitssold[maxs], double unitprice[maxs],
    int &nums, string names[maxs])
{
	int i;
	ifstream inf;
	inf.open("grocery.txt");
  i = 0;
	while (!inf.eof())
	{
  inf >> id[i] >> names[i] >> unitssold[i] >> unitprice[i];

  i++;
	}
	nums = i;

}

void calctot(int id[], int unitssold[], double unitprice[],
    double unitsales[], int &unitquant, double &totsales,
    int nums)
{
	int i;
	for (i = 0; i < nums; i++)
	{
  unitsales[i] = unitssold[i] * unitprice[i];
  unitquant = unitssold[i] + unitquant;
  totsales = unitsales[i] + totsales;
	}
}

void swapint (int &a, int &b)
{
	int temp;
	temp = a;
	a = b;
	b = temp;
}

void swapdob (double &a, double&b)
{
	double temp;
	temp = a;
	a = b;
	b = temp;
}

void swapstr (string &a, string &b)
{
	string temp;
	temp = a;
	a = b;
	b = temp;
}

void swapchar (char &a, char &b)
{
	char temp;
	temp = a;
	a = b;
	b = temp;
}

void sortem (int id[], int unitssold[], double unitprice[],
 	 double unitsales[], int nums, string names[])
{
	int i,j;
	for (j = 0; j < nums; j++)
	for (i = 0; i < nums-1; i++)
	if (id[i] > id[i+1])
	{
  swapint(id[i], id [i+1]);
  swapint(unitssold[i], unitssold[i+1]);

  swapdob(unitprice[i], unitprice[i+1]);
  swapdob(unitsales[i], unitsales[i+1]);

  swapstr(names[i],names [i + 1]);
	}
}

void rsales( double unitsales[], int &a, int &b, int &c,
 	 int &d, int nums)
{
	int i;
	for (i = 0; i < nums; i++)
  if(unitsales[i] >= 500.00)
 	 a = a + 1;
  else if (unitsales[i] >= 250.00)
 	 b = b + 1;
  else if (unitsales[i] >= 100.00)
 	 c = c + 1;
  else
 	 d = d + 1;
}

void printt (int unitquat, double totsales, ofstream &outf)
{
	outf.setf(ios::fixed);
	outf.precision(2);
	outf << setw(35) << "----" << setw(30) << "------" << endl;
	outf << setw(35) << unitquat << setw(30) << totsales << endl;
}


void print (int id[], string names[], int unitssold[], double unitprice[],
 	 int nums,  ofstream &outf)
{       
	int i;
	outf.setf(ios::fixed);
	outf.precision(2);
	outf << setw(3) << "ID's" << setw(8) << "Product" << setw(25) << "Units Sold" 
   << setw(15) << "Unit Price" << setw(15) << endl;
	outf << setw(1) << "====================================================" 
   <<endl;

	for (i = 0; i < nums; i++)
	{	
  outf << setw (3) << id[i] << "  ";
  outf.setf(ios::left);
  outf << setw(15) << names[i];
  outf.unsetf(ios::left);
  outf << setw(15) << unitssold [i] 
       << setw(15)  << unitprice[i] << endl;
    
	}
    outf <<endl << endl << endl;
}

void print2 (int id[], string names[], int unitssold[], double unitprice[], double unitsales[],
    int nums,  ofstream &outf)
{       
	int i;
	outf.setf(ios::fixed);
	outf.precision(2);
	outf << setw(3) << "ID's" << setw(8) << "Product" << setw(25) << "Units Sold" 
   << setw(15)<< "Unit Price" << setw(15) << "Total Sales" << endl;
	outf << setw(1) 
   << "===================================================================" 
   <<endl;

	for (i = 0; i < nums; i++)
	{	
  outf << setw (3) << id[i] << "  ";
  outf.setf(ios::left);
  outf << setw(15) << names[i];
  outf.unsetf(ios::left);
  outf << setw(15) << unitssold [i] 
       << setw(15)  << unitprice[i] << setw(15) << unitsales[i] 
    << endl;
	}
}


void main()
{  
    int id[maxs], nums = maxs;
	int unitssold[maxs], unitquat = 0;	
	double unitprice[maxs];
	double unitsales[maxs], totsales = 0;

    string names[maxs];
	ofstream outf;
	outf.open("grocery.out");

	init(id, unitssold, unitprice, unitsales);
	reedem(id, unitssold, unitprice, nums, names);
	calctot (id, unitssold, unitprice, unitsales, 
 	 unitquat, totsales, nums);
    
    print (id,names, unitssold, unitprice, nums,  outf);
	sortem(id, unitssold, unitprice, unitsales, nums, names);
	print2 (id,names, unitssold, unitprice, unitsales, nums, outf);
	printt (unitquat, totsales, outf);


}

Then here is the input file

208  Mints      205    .28

137  Bread      252    1.95

485  Soup      125    .49

217  Yogurt    111    1.11

945  Pencil    250    .09

607  Pen    335    .29

642  Chips    200    2.66

735  Coffee      36  10.02

300  Pickles    163    .87

299  Mushroom  255    .35

435  Peppers    51    .64

116  Chili      75    6.03

189  Lobster      5  29.95

218  Mayonnaise  61    3.03

830  Eraser    217    .17

695  Paper      325    .76

708  Salsa  216    .79

325  Ketchup    99    1.19

339  Mustard  49    .85

418  Book  237    1.49

225  Lettuce 311    .52

180  Hamburger  128    2.22

925  Buns      299    1.49

455  Onions      40    .44

347  Steak      77    8.00

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0
never mind...

584818995[/snapback]

any ideas? WHen the dos screen appears you are suppose to press any key and it suppose to do all the calculations and then end, it just hangs on the dos screen and says nothing for about 3 - 5 mins

Link to comment
Share on other sites

  • 0

is "grocery.txt" in the same directory as the source, project, or executable? if not, this is your problem.

however, if nothing appears on the screen, but the "press any key..." thing comes up, then go to you project folder and open "grocery.out" with a text editor to view the contents.

here is the output:

ID's Product              Units Sold    Unit Price

====================================================

208  Mints                      205          0.28

137  Bread                      252          1.95

485  Soup                      125          0.49

217  Yogurt                    111          1.11

945  Pencil                    250          0.09

607  Pen                        335          0.29

642  Chips                      200          2.66

735  Coffee                      36          10.02

300  Pickles                    163          0.87

299  Mushroom                  255          0.35

435  Peppers                    51          0.64

116  Chili                      75          6.03

189  Lobster                      5          29.95

218  Mayonnaise                  61          3.03

830  Eraser                    217          0.17

695  Paper                      325          0.76

708  Salsa                      216          0.79

325  Ketchup                    99          1.19

339  Mustard                    49          0.85

418  Book                      237          1.49

225  Lettuce                    311          0.52

180  Hamburger                  128          2.22

925  Buns                      299          1.49

455  Onions                      40          0.44

347  Steak                      77          8.00

-1                              0          0.00

ID's Product              Units Sold    Unit Price    Total Sales

===================================================================

-1                              0          0.00          0.00

116  Chili                      75          6.03        452.25

137  Bread                      252          1.95        491.40

180  Hamburger                  128          2.22        284.16

189  Lobster                      5          29.95        149.75

208  Mints                      205          0.28          57.40

217  Yogurt                    111          1.11        123.21

218  Mayonnaise                  61          3.03        184.83

225  Lettuce                    311          0.52        161.72

299  Mushroom                  255          0.35          89.25

300  Pickles                    163          0.87        141.81

325  Ketchup                    99          1.19        117.81

339  Mustard                    49          0.85          41.65

347  Steak                      77          8.00        616.00

418  Book                      237          1.49        353.13

435  Peppers                    51          0.64          32.64

455  Onions                      40          0.44          17.60

485  Soup                      125          0.49          61.25

607  Pen                        335          0.29          97.15

642  Chips                      200          2.66        532.00

695  Paper                      325          0.76        247.00

708  Salsa                      216          0.79        170.64

735  Coffee                      36          10.02        360.72

830  Eraser                    217          0.17          36.89

925  Buns                      299          1.49        445.51

945  Pencil                    250          0.09          22.50

                              ----                        ------

                              4122                      5288.27

STV

Link to comment
Share on other sites

  • 0

Yea i got it to work but here is my next problem

I need to call a function to print the id's of the items, ten per line using a heading that states" Product sold this week."

I do not know how to do that.

I have this example

[inf >>numperrow;
for(i=0; i < maxs; i = i+ numperrow)
outf<<setw(5)<<a[i]<<setw(5)<<a[i+1]<<setw(5)<<setw(5)<<a[i+2]
<<setw(5)<<a[i+3]<<setw(5)<<a[i+4]<<endl;

but when I plug it in i get an error

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.