Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Project problem

I have this problem with my project. I want to see if

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;
}
[11039 byte] By [SweetTrombonist] at [2007-11-11 10:01:04]
# 1 Re: Project problem
Hi,

have you compiled it? Are there any error messages?

D
drkybelk at 2007-11-11 20:59:36 >
# 2 Re: Project problem
you need a project file under most IDEs -- this means adding all the .cpp files to the project via some menu, and adding one or more include folders where your .h files live. Non - Ide compilers will just take an arguement list from the commandline -- this gets large fast -- such as g++ -options file1.cpp file2.cpp etc.cpp -include folder -yes compile it - other stuff. You can put this into a text file and make it a script for your project or you can try to unravel the cryptic make syntax to create a "makefile". The makefile version is much more powerful and is the way to go on such systems in the long run.
jonnin at 2007-11-11 21:00:36 >
# 3 Re: Project problem
b) it's better to have multifiles, specially if u have too many lines in the code like here which make the debuging and updating more complicated .. anyway to make them in one file u must put all headers, definitions, declarations in the top of the programme , remove dublications , and bind the two main functions , for example put statments in the second one after all the first one ..

a) explain more and tell us your specific errors .
Amahdy at 2007-11-11 21:01:40 >
# 4 Re: Project problem
Well im suppost to do a project for my C++ class. I need help with a file. I need to sort the file so that it can be put into a table. Then it should have a report telling how many searches, how many were successful and how many new part numbers I added. And, it should write the file out again, so the additions are saved. I need to copy the file to the directory where your solution will be. I will most likely need parallel vectors to hold the data and functions to read in the data, search for the part and then print out the report.

the only thing i know i can do is that although a part number is a string, it will contain no spaces or other white space and therefore can be read with fin or whatever I call the input file. I have no need to use getline, just read each field into a variable and then use push_back to put it on the appropriate vector. now i wrote out the programs and i believe they are right, but i am not totally sure. I might have gone extra about the program, but at least they are there. However, i looked at the website of my professor, and found the requirements that he forgot to put on there.

// fills vectors
void get_data (vector <string>& part_number, vector <char>& part_class, vector <int>& ohb, vector <double>& cost);

// Does a binary search
int bin_search(string key, const vector<string>& part_number);

// Asks user for a part number to search for
string get_target ();

// gets remaining info to add a part number
void get_more_data(char& class_in, int& ohb_in, double& cost_in);

// Inserts part number data into Vectors
void insert_data (vector <string>& part_number, vector <char>& part_class, vector <int>& ohb, vector <double>& cost, string part_in, char class_in,
int ohb_in, double cost_in);

// Displays info on part number
void display (const vector <string>& part_number, const vector <char>& part_class, const vector <int>& ohb, const vector <double>& cost, int finder);

// sorts vectors (Calls swapper)
void sort (vector <string>& part_number, vector <char>& part_class, vector <int>& ohb, vector <double>& cost);

// prints search stats
void print_stats(int searches, int good, int bad);

// writes out file
void put_data (const Vector <string>& part_number, const vector <char>& part_class, const Vector <int>& ohb, const Vector <double>& cost);

// templated swap function
template <class CType>
void swapper (CType& a, CType & b);

so i really dont know if i did it right.
SweetTrombonist at 2007-11-11 21:02:40 >