Project problem
a) the codes are right
b) a way to put the codes together into one code.
the file is right here:
part number:
#include <string>
#include <fstream>
#include <iostream>
#include <ctype.h>
using namespace std;
struct item
{
string name;
string partNumber;
short quantity;
double price;
};
//******************************* FUNCTION PROTOTYPES **********************************
int countItemsInFile ( ifstream & );
void readFileIntoVector ( ifstream & , item * , int );
void menuInterface ( item * , int );
//**************************************************************************************
int main ()
{
ifstream input ( "c:\\programs\\data.txt" );
if ( input.fail() )
{
cout << "Input file did not open!!!" << endl;
return 1;
}
int itemsInFile = countItemsInFile ( input );
input.close();
item * items = new item [ itemsInFile ];
ifstream input2 ( "c:\\programs\\data.txt" );
if ( input2.fail() )
{
cout << "Input file did not open!!!" << endl;
return 1;
}
readFileIntoVector ( input2 , items , itemsInFile );
menuInterface ( items , itemsInFile );
return 0;
}
int countItemsInFile ( ifstream & i )
{
string name , partNumber;
int quantity;
double price;
int count = 0;
i >> name;
while ( ! i.eof() )
{
i >> partNumber;
i >> quantity;
i >> price;
i >> name;
count ++;
}
return count;
}
void readFileIntoVector ( ifstream & i , item * is , int numberItems )
{
int j = 0;
while ( j < numberItems )
{
i >> is [ j ] .name;
i >> is [ j ] .partNumber;
i >> is [ j ] .quantity;
i >> is [ j ] .price;
j ++;
}
}
void menuInterface ( item * i , int ni )
{
char choice;
int j = 0;
string partNum;
do
{
cout << endl;
cout << endl << "F) Find item by part number";
cout << endl << "P) Print items";
cout << endl << "Q) Quit";
cout << endl << "Choice >";
cin >> choice;
choice = toupper ( choice );
while ( choice != 'F' && choice != 'P' && choice != 'Q' )
{
cout << endl <<"Enter a F or P or Q > ";
cin >> choice;
choice = toupper ( choice );
}
if ( choice == 'P' )
{
cout << endl << endl;
j= 0;
while ( j < ni )
{
cout << i [ j ].name << " " << i [ j ].partNumber << " " << i [ j ].quantity
<< i [ j ].price << endl;
j++;
}
}
if ( choice == 'F' )
{
cout << endl << "Enter a part number: ";
cin >> partNum;
j = 0;
while ( j < ni )
{
if ( i [ j ].partNumber == partNum )
{
cout << endl << "Part Number Found!" << endl;
cout << i [ j ].name << " " << i [ j ].partNumber << " " << i [ j ].quantity
<< i [ j ].price << endl;
break;
}
j ++;
}
if ( j == ni )
cout << endl << "Part Number Not Found" ;
}
if ( choice == 'Q' )
cout << endl << "Goodbye";
}while ( choice != 'Q' );
}
sorted file :
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <ctype.h>
#define MAX_WORD_LENGTH 50
#define TRUE 1
#define FALSE 0
struct wordListNode
{
char *word ;
int count ;
wordListNode *next ;
void initialize ( char * w )
{
word = w;
count = 1;
next = 0;
};
};
//****************************************************************
wordListNode * buildSortedList ( char * fileName );
char * processWord ( char * );
void printList ( wordListNode * );
void destroyList ( wordListNode *& );
bool findList ( wordListNode * , char * );
//****************************************************************
void main ()
{
wordListNode * head;
head = buildSortedList ( "c:\\programs\\data.txt" );
printList ( head );
if ( findList ( head , "the" ) )
cout << endl << "\"the\" is in the list" << endl << endl;
destroyList ( head );
}
wordListNode * buildSortedList ( char * f /*file name */ )
{
ifstream inputFile ( f ); // i == input file
if ( inputFile.fail() )
{
cout << "File did not open!! " << endl;
return 0;
}
char inputBuffer [ MAX_WORD_LENGTH ]; //input buffer
char * tempWord; //temp word
wordListNode * tempListNode; //temp list node
wordListNode * head = 0; //head list
wordListNode * next = 0; //follow
wordListNode * first = 0;//first
wordListNode * temp = 0; //temporary node pointer
inputFile >> inputBuffer; //get first word
while ( ! inputFile.eof () ) //while not at the end of the file
{
tempWord = processWord ( inputBuffer ); //take out punc , upper case
tempListNode = new wordListNode; //make a new list node
tempListNode -> initialize ( tempWord ); //make the new node point to new word
//*************** enter the word in the list
if ( head == 0 ) //empty list
{
head = tempListNode;
}
else //at least one item in list
{
//the word is the new first word in the list @@@@@@@@@@@@@@@@@@@@@@@@@@@@
if ( strcmp ( tempListNode -> word , head -> word ) < 0 )
{
temp = head;
head = tempListNode;
head -> next = temp;
}
else if ( strcmp ( tempListNode -> word , head -> word ) == 0 )
{
head -> count ++;
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
else //else the new word belongs in the middle somewhere
{
//word is in middle or back of the list *********
next = head -> next; //pointer to second word in list
first = head; //pointer to first word in list
while ( next ) //while not at the last word in the list
{
if ( strcmp ( tempListNode -> word , next -> word ) < 0 ) //found place in list
{
//insert the word before the word pointed to by next
temp = next; //save address of the new word
first -> next = tempListNode; //make the word before point to new word
tempListNode -> next = temp; //make the new word point to next
break;//stop searching
}
//found the word in the list
else if ( strcmp ( tempListNode -> word , next -> word ) == 0 )
{
next -> count ++; //increase count
break; //stop search
}
first = first -> next; //make the next word the one before word to test
next = next -> next; //test next word in the list
}
//new word must be last in the list
if ( next == 0 )
{
//if is same increase count
if ( strcmp ( tempListNode -> word , first -> word ) == 0 )
tempListNode -> count ++;
else
first -> next = tempListNode;
}
}
//***********************************************
}
inputFile >> inputBuffer;
//********************************************
}
inputFile.close();
return head;
}
char * processWord ( char * word /* word */ )
{
int i = 0;
int j = 0;
int wordLength = strlen ( word ); //get length
char * newWord = new char [ wordLength + 1 ]; //set up space for word
while ( i < wordLength ) //while not at end of word
{
if ( isalpha ( word [ i ] ) ) //wastes some space here ( maybe 2 * sizeof ( chars ) !
{ newWord [ j ] = tolower ( word [ i ] );
j ++;
}
i ++;
}
newWord [ j ] = 0;
return newWord;
}
//outputs ( word , word count )
void printList ( wordListNode * l )
{
int i = 1;
while ( l )
{
cout << " ( " << l -> word << " , " << l -> count << " ) " ;
if ( i % 3 == 0 )
cout << endl;
l = l -> next;
i++;
}
cout << endl << endl;
}
void destroyList ( wordListNode *& h )
{
wordListNode * t;
while ( h )
{
t = h;
h = h -> next;
delete t -> word;
delete t;
}
}
bool findList ( wordListNode * h , char * w )
{
while ( h )
{
if ( strcmp ( h -> word , w ) == 0 )
return TRUE; //found
h = h -> next;
}
return FALSE;
}

