Please help, this is for an assignment I need to turn in and I'm STUCK!
I have no idea how to do this I've done programs but none like this calling differnt functions. this is what is in the data file.
100 Hammer 10 20 10
200 Saw 24 00 12
300 Screwdriver 12 36 11
400 Pliers 40 00 25
500 Drill 24 12 13
600 Wrench 16 05 10
000 End_of_File 00 00 00
EOJ

Here is my code. I got it to ouput reading the file as char even though the numbers are suppose to be int, but every time i try to do the variables as int it outputs a never ending number. and I'm supposed to acummulate the totals on hand and total on order.
Also I must calculate the potential quantity wish is the total number on hand + on order - sold
I'm really just lost as to why it wont read my data file as int, that would help if i could figure that out, and hopefully i'll be able to figure out the rest.

//Preprocessive directives
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

//Prototypes
void initialization();
void process ();
void eoj ();
void openIt();
void readIt(); 
void writeIt();

ifstream inputFile;
char itemNum[4];
char itemDescription[15];
char qtyOnHand[2]; 
char qtyOnOrder[2];
char qtySold[2];


void main(void)
{
	cout << "\n\t\tGWINNET TOOLS INC.\n\n";
	cout << "Ref#  Description    OnHand  OnOrder Sold\n";
	cout << "--------------------------------------------\n";
	initialization();
	
	while (!inputFile.eof())    
	{
		process();
	}

	eoj();

	cout << "\n-----<E N D  O F  R U N>-----\n";
	
	return;
}


void initialization()
{
	openIt();
	readIt();
	return;
}
void process ()
{
	writeIt();
	readIt();
	return;
}
void eoj ()
{
 inputFile.close();
 return;
}
void openIt()
{
	inputFile.open("c:\\invent.dat");
	if (inputFile.fail())
		cout << "Names file open failed" << endl;
	return;

}
void readIt()
{
	inputFile >> itemNum >> itemDescription >> qtyOnHand >>
	qtyOnOrder >> qtySold;
	return;
}
void calculate()
{

}
void accumulate()
{

}
void writeIt()
{
	cout << setw(6) << left<< itemNum << setw(15)  << itemDescription << setw(8) << qtyOnHand 
		 << setw(8) << qtyOnOrder << setw(5) << qtySold << setw(5) << endl; 
	
	
		
		return;
}

As long as you are guaranteed that the item names will be only one word, this is not a hard problem.

Your numeric data items should be type int, with the item name being a char array, as you have. Your reading line stays as you have it.

From the standpoint of program organization, you've got several functions which might result in a failed state, but they don't transmit that information back to the caller. For instance, openIt( ) will display a message on the console if the file fails to open, but the process will attempt to go on. Such a function would be better as:

bool openIt()
{
	inputFile.open("c:\\invent.dat");
	if (inputFile.fail())
        {
		cout << "Names file open failed" << endl;
                return false;
        }
	return true;
}

Or even better, don't put the error display in this function, let the caller handle the failure in any way needed.

Similarly, your readIt( ) should perhaps test the item description - when it encounters the End_of_File name, it could return false, otherwise true.

Well there are somethings you might not be understanding, or not looking from the right side
Here is a code that might give you a hand, didnt tested it but is just for you to get an idea of how to read the different kind of data

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>

using namespace std;

int main()
{
	int ref;
	char nameofproduct[20];
	int onhand;
	int onorder;
	int sold;
	char temp[50];
	char temp1[20];
	int i;
	char *name="test.txt";
	char *name1="test1.txt";
    fstream file, file2;
	file.open(name,ios::in);//opens file for reading
	file2.open(name1,ios::out);//opens file where data will be stored
	while(!file.eof())//keeps going until the end of file
	{
		i=0;
		file.getline(temp,sizeof(file));//gets line by line of the file
		while(temp[i]!=' ')//goes untill it finds a space
		{
			temp1[i]=temp[i];//copies what is on each space of the array 
			i++;
		}
		temp1[i]='\0';//finishes the array
		i++;//jumps the space and goes to next 
		ref=atoi(temp1);//temp1 has the ref number right now so we make it an "int" 
		int k=0;//new counter
		while(temp[i]!=' ')//same thing as before but with the next word
		{
			temp1[k]=temp[i];
			i++;
			k++;
		}
		i++;
		temp1[k]='\0';
		strcpy(nameofproduct,temp1);//here we copy what is on temp1 that is now the name
		k=0;
	while(temp[i]!=' ')
		{
			temp1[k]=temp[i];
			i++;
			k++;
		}
		i++;
		temp1[k]='\0';
		onhand=atoi(temp1);
		k=0;
		while(temp[i]!=' ')
		{
			temp1[k]=temp[i];
			i++;
			k++;
		}
		i++;
		temp1[k]='\0';
		onorder=atoi(temp1);
		k=0;
		while(temp[i]!='\0')
		{
			temp1[k]=temp[i];
			i++;
			k++;
		}
		i++;
		temp[k]='\0';
		sold=atoi(temp1);
		file2<<ref<<" "<<nameofproduct<<" "<<onhand<<" "<<onorder<<" "<<sold<<endl;
	}

	return 0;
}

Hope it helps

This article has been dead for over six months. Start a new discussion instead.