Hej, Jeg har lavet et program ved brug af parallel arrays, da det er et krav fra min laerer. Jeg har umiddelbart nogle problemer med det og haaber at der er en der kan kompile det og hjaelpe med med at loese problemet.
Paa forhaand tak!
Jeg fylder mine arrays up med data fra en tekstfil:
Brake Pads 1132 26.50
Radiator hose 8740 5.00
Fan belt 2381 12.25
Wiper blades 9028 8.00
Her er programmet
#include <iostream.h> // for input and output statements #include <fstream.h> // for reading from and writing to a file
const int MAX_NAME = 20; //maximum number of characters const int MAX_LIST = 100; // maximum number of items const char BLANK = ' '; // used for a dummy character const int DONT_CARE = 0; // used for a dummy integer variable const int SO_MANY_CHARS = 100; // to be used with ignor() to skip blanks
void print_message(message_type sttm, char ch, int val, int pos); void fill_list(ifstream &infile, int item[], int item_number[], int item_price[], int& len); void print_list (const int item[], const int item_number[], const int item_price[], int len); void get_menu_item (char& ans); void get_part_number (int& val); int find_position(const int list[],int len, int val); void insertion_sort (ifstream& infile, int item_number[], int& len ); int where_in_the_array(int item_number [], int len, int buffer); void shift_right (int item_number[], int len, int pos); int bin_search (int item_number[], int len, int value);
typedef char NameType[MAX_NAME +1]; //An extra space for \0 character NameType item [MAX_LIST]; //Array containing item names long item_number [MAX_LIST]; //Array containing item numbers float item_price [MAX_LIST]; //Array containing prices of each item int list[MAX_LIST]; // the list for holding up to MAX_LEN numbers int length; // number of elements of the array int value; // value to be deleted from the list entered by the user int index; // the index to the value to be deleted in the list char answer; // answer to the query enterd by the user ifstream infile; //input file stream
infile.open(DATAFILE,ios::in|ios::nocreate); //open datafile for reading if (infile.fail()) cout<<"*** Can't open the file**"<<endl; else { print_message(OPENING,BLANK,DONT_CARE,DONT_CARE);
switch (answer) { case 'd': get_part_number (value); bin_search (item_number, length, value); index= find_position (item_number, length, value); if (index<0) // value not found print_message(NOT_FOUND,BLANK, value,DONT_CARE ); else { print_message(POSITION_IN_LIST,BLANK, value,index+1); //add one to index to be user friendly //delete_value (list, length, index); print_list(item, item_number, item_price, length); } break;
case 'q': print_message(CLOSING,BLANK,DONT_CARE,DONT_CARE); break;
default: print_message (UNKNOWN_CHAR,answer,DONT_CARE,DONT_CARE); break; //not necessary but a good habit }//switch
} while (answer != 'q');
infile.close(); }
} //****************************************************************************************** // print_message() // This function writes messages on the screen. //****************************************************************************************** void print_message(/*in*/ message_type sttm, //type of message /*in*/ char ch, //used with UNKNOWN_CHAR message /*in*/ int val, //used with NOT_FOUND and POSITION_INLIST message /*in*/ int pos) //used with POSITION_INLIST message { switch (sttm) { case UNKNOWN_CHAR: cout<<"=======> Sorry I don't understand what "<<'"'<<ch<<'"'<< " means"; break;
case NOT_FOUND: cout<<"=======> Sorry "<<val<<" is not in the list"; break;
case OPENING: cout<<"****************************************************"<<endl; cout<<"***** Welcome to the CSCI2912 Automotive Parts *****"<<endl; cout<<"****************************************************"<<endl; cout<<" ****** "<<endl; cout<<" ****** "<<endl; cout<<" ****** "<<endl; cout<<" ****** "<<endl; cout<<" ************ "<<endl; cout<<" ******* "<<endl; cout<<" * "<<endl; break;
case CLOSING: cout<<"*****************************************************"<<endl; cout<<"*****************************************************"<<endl; cout<<"* Thanks for shopping at CSCI2912 Automotive Parts*"<<endl; cout<<"* Have a nice day and come back again *"<<endl; cout<<"*****************************************************"<<endl; break;
case POSITION_IN_LIST: cout<<"The first occurance of the value "<<"("<<val<<")"<< " is at position "; cout<<pos<<endl; break;
case WARNING: cout<<"----Warning---> The file contains more than " << MAX_LIST<<" elements. "; cout<<"Only the first "<< MAX_LIST<<" elements will be processed.n"; break;//UNNECESSARY BUT A GOOD HABIT
}
} //****************************************************************************************** // fill_list() // This function fills up the array with numbers in the file. If the file contains // more than MAX_LEN numbers only the first MAX_LEN numbers will be processed. //****************************************************************************************** void fill_list(/* in*/ ifstream &infile,//contains the numbers /*out*/ int item[], //for holding the numbers /*out*/ int item_number[], /*out*/ int item_price[], /*out*/ int& len) //length of the list { int buffer; len =0; infile >> buffer; while (infile && len< MAX_LIST) { item[len]=buffer; item_number[len]=buffer; item_price[len]=buffer; len++; infile >>buffer; } if (infile) print_message(WARNING, NOT_FOUND, BLANK, DONT_CARE); } //****************************************************************************************** // print_list() // This function prints the elements of the array. //****************************************************************************************** void print_list (/* in*/ const int item[], // the list /* in*/ const int item_number[], /* in*/ const int item_price[], /*in*/ int len) // number of elements stored in the array
//****************************************************************************************** // get_menu_item() // This function gets a menu item from the user. //****************************************************************************************** void get_menu_item (/*out*/ char& ans) // p for price and q for quit { cout<< "Would you like to check the [p]rice or "; cout<< "[q]uit the program? "; cin >> ans; cout << endl; } //****************************************************************************************** // get_value() // This function gets an integer number from the user. //****************************************************************************************** void get_part_number (/*out*/ int& val) // an integer number to be deleted from the list { cout<< "Please enter the part number: "; cin >> val; cout<<endl; } //****************************************************************************************** // find_position() // This function finds the postion of the value in the list, if found. if the value is not // in the list it returns -1. //******************************************************************************************
int find_position(/*in*/ const int list[],// the list to be searched int len, // the lenght of the list int val) // the vlaue to be looked up { bool found=false; // Initialize found to false int curr_pos =0; // Initialize curr_pos to zero while ((curr_pos<len) && (!found)) // As long as the variable // is not found and // len < MAX_LEN { if( list[curr_pos] == val ) found= true; else curr_pos++; } if (found) return curr_pos; else return -1; }
//****************************************************************************************** // insertion_sort() // This function finds the correct position in the buffer if there is // room in the array //****************************************************************************************** void insertion_sort (ifstream& infile, int item_number[], int& len ) { int buffer; //temporary storage for numbers int position; len=0; infile >> buffer; // priming read in the buffer while(infile && len < MAX_LIST)// as long as the file is not empty and { // there are enough locations in the array position= where_in_the_array(item_number, len, buffer); shift_right (item_number, len, position); item_number[position]=buffer; len++; infile>>buffer; // priming read in the buffer } if (infile) //More than MAX_LEN elements print_message(WARNING,NOT_FOUND,BLANK,DONT_CARE); } //****************************************************************************************** // where_in_the_array() // This function makes room for the position you want to enter // //****************************************************************************************** int where_in_the_array(int item_number [], int len, int buffer) { int curr_pos = 0; bool found = false; while ( curr_pos <= len-1 && !found) { if(buffer < item_number [curr_pos]) curr_pos ++; else found= true; } //****************************************************************************************** // shift_right() // This function makes room for the position you want to enter // //****************************************************************************************** void shift_right (int item_number[], int len, int pos) { for( int i = len; i> pos; i-- ) { item_number[i]=item_number[i-1]; }
} //****************************************************************************************** // bin_search() // This function continues narrowing the part of the sequence being // searched until only one term of the sequence remains. //****************************************************************************************** int bin_search(/*in*/ int item_number[],// the list to be searched int len, // the lenght of the list int value) // the vlaue to be looked up { int middle; // initialize the middle int lower=0; // initialize lower int upper = len-1; // initialize upper to be // the index of the last // element in the list bool found = false; while ((upper >= lower) && (!found))// as long as upper is greater // than or equal to lower and { // found is false middle = (lower + upper)/2; // middle of interval if (value == item_number[middle])// value equal to middle value // in the list found = true; else if (value < item_number[middle])// if value is smaller than // middle value in the list upper= middle-1; else lower = middle + 1; } if (found = true) return middle; else return -1; }
Jeg har nu rettet mine prototyper/funktioner, men har nu 25 fejl istedet for 9 som jeg havde foer. Er det muligt at du kan se andre fejl. Jeg er ikke ret erfaren i C++ saa jeg har lidt svaert ved at se det. Mange tak!
Dette er koden i rettet tilstand og med 25 errors:
#include <iostream.h> // for input and output statements #include <fstream.h> // for reading from and writing to a file
const int MAX_NAME = 20; //maximum number of characters const int MAX_LIST = 100; // maximum number of items const char BLANK = ' '; // used for a dummy character const int DONT_CARE = 0; // used for a dummy integer variable const int SO_MANY_CHARS = 100; // to be used with ignor() to skip blanks
void print_message(message_type sttm, char ch, int val, int pos); void fill_list(ifstream &infile, NameType item[], long item_number[], float item_price[], int& len); void print_list (const NameType item[], const long item_number[], const float item_price[], int len); void get_menu_item (char& ans); void get_part_number (int& val); int find_position(const long item_number[],int len, int val); void insertion_sort (ifstream& infile, long item_number[], int& len ); int where_in_the_array(long item_number [], int len, int buffer); void shift_right (long item_number[], int len, int pos); int bin_search (long item_number[], int len, int value);
typedef char NameType[MAX_NAME +1]; //An extra space for \0 character NameType item [MAX_LIST]; //Array containing item names long item_number [MAX_LIST]; //Array containing item numbers float item_price [MAX_LIST]; //Array containing prices of each item int list[MAX_LIST]; // the list for holding up to MAX_LEN numbers int length; // number of elements of the array int value; // value to be deleted from the list entered by the user int index; // the index to the value to be deleted in the list char answer; // answer to the query enterd by the user ifstream infile; //input file stream
infile.open(DATAFILE,ios::in|ios::nocreate); //open datafile for reading if (infile.fail()) cout<<"*** Can't open the file**"<<endl; else { print_message(OPENING,BLANK,DONT_CARE,DONT_CARE);
switch (answer) { case 'd': get_part_number (value); bin_search (item_number, length, value); index= find_position (item_number, length, value); if (index<0) // value not found print_message(NOT_FOUND,BLANK, value,DONT_CARE ); else { print_message(POSITION_IN_LIST,BLANK, value,index+1); //add one to index to be user friendly //delete_value (list, length, index); print_list(item, item_number, item_price, length); } break;
case 'q': print_message(CLOSING,BLANK,DONT_CARE,DONT_CARE); break;
default: print_message (UNKNOWN_CHAR,answer,DONT_CARE,DONT_CARE); break; //not necessary but a good habit }//switch
} while (answer != 'q');
infile.close(); }
} //****************************************************************************************** // print_message() // This function writes messages on the screen. //****************************************************************************************** void print_message(/*in*/ message_type sttm, //type of message /*in*/ char ch, //used with UNKNOWN_CHAR message /*in*/ int val, //used with NOT_FOUND and POSITION_INLIST message /*in*/ int pos) //used with POSITION_INLIST message { switch (sttm) { case UNKNOWN_CHAR: cout<<"=======> Sorry I don't understand what "<<'"'<<ch<<'"'<< " means"; break;
case NOT_FOUND: cout<<"=======> Sorry "<<val<<" is not in the list"; break;
case OPENING: cout<<"****************************************************"<<endl; cout<<"***** Welcome to the CSCI2912 Automotive Parts *****"<<endl; cout<<"****************************************************"<<endl; cout<<" ****** "<<endl; cout<<" ****** "<<endl; cout<<" ****** "<<endl; cout<<" ****** "<<endl; cout<<" ************ "<<endl; cout<<" ******* "<<endl; cout<<" * "<<endl; break;
case CLOSING: cout<<"*****************************************************"<<endl; cout<<"*****************************************************"<<endl; cout<<"* Thanks for shopping at CSCI2912 Automotive Parts*"<<endl; cout<<"* Have a nice day and come back again *"<<endl; cout<<"*****************************************************"<<endl; break;
case POSITION_IN_LIST: cout<<"The first occurance of the value "<<"("<<val<<")"<< " is at position "; cout<<pos<<endl; break;
case WARNING: cout<<"----Warning---> The file contains more than " << MAX_LIST<<" elements. "; cout<<"Only the first "<< MAX_LIST<<" elements will be processed.n"; break;//UNNECESSARY BUT A GOOD HABIT
}
} //****************************************************************************************** // fill_list() // This function fills up the array with numbers in the file. If the file contains // more than MAX_LEN numbers only the first MAX_LEN numbers will be processed. //****************************************************************************************** void fill_list(/* in*/ ifstream &infile,//contains the numbers /*out*/ NameType item[], //for holding the numbers /*out*/ long item_number[], /*out*/ float item_price[], /*out*/ int& len) //length of the list { int buffer; len =0; infile >> buffer; while (infile && len< MAX_LIST) { item[len]=buffer; item_number[len]=buffer; item_price[len]=buffer; len++; infile >>buffer; } if (infile) print_message(WARNING, NOT_FOUND, BLANK, DONT_CARE); } //****************************************************************************************** // print_list() // This function prints the elements of the array. //****************************************************************************************** void print_list (/* in*/ const NameType item[], // the list /* in*/ const long item_number[], /* in*/ const float item_price[], /*in*/ int len) // number of elements stored in the array
//****************************************************************************************** // get_menu_item() // This function gets a menu item from the user. //****************************************************************************************** void get_menu_item (/*out*/ char& ans) // p for price and q for quit { cout<< "Would you like to check the [p]rice or "; cout<< "[q]uit the program? "; cin >> ans; cout << endl; } //****************************************************************************************** // get_value() // This function gets an integer number from the user. //****************************************************************************************** void get_part_number (/*out*/ int& val) // an integer number to be deleted from the list { cout<< "Please enter the part number: "; cin >> val; cout<<endl; } //****************************************************************************************** // find_position() // This function finds the postion of the value in the list, if found. if the value is not // in the list it returns -1. //******************************************************************************************
int find_position(/*in*/ const long item_number[],// the list to be searched int len, // the lenght of the list int val) // the vlaue to be looked up { bool found=false; // Initialize found to false int curr_pos =0; // Initialize curr_pos to zero while ((curr_pos<len) && (!found)) // As long as the variable // is not found and // len < MAX_LEN { if( item_number[curr_pos] == val ) found= true; else curr_pos++; } if (found) return curr_pos; else return -1; }
//****************************************************************************************** // insertion_sort() // This function finds the correct position in the buffer if there is // room in the array //****************************************************************************************** void insertion_sort (ifstream& infile, long item_number[], int& len ) { int buffer; //temporary storage for numbers int position; len=0; infile >> buffer; // priming read in the buffer while(infile && len < MAX_LIST)// as long as the file is not empty and { // there are enough locations in the array position= where_in_the_array(item_number, len, buffer); shift_right (item_number, len, position); item_number[position]=buffer; len++; infile>>buffer; // priming read in the buffer } if (infile) //More than MAX_LEN elements print_message(WARNING,NOT_FOUND,BLANK,DONT_CARE); } //****************************************************************************************** // where_in_the_array() // This function makes room for the position you want to enter // //****************************************************************************************** int where_in_the_array(long item_number [], int len, int buffer) { int curr_pos = 0; bool found = false; while ( curr_pos <= len-1 && !found) { if(buffer < item_number [curr_pos]) curr_pos ++; else found= true; } //****************************************************************************************** // shift_right() // This function makes room for the position you want to enter // //****************************************************************************************** void shift_right (long item_number[], int len, int pos) { for( int i = len; i> pos; i-- ) { item_number[i]=item_number[i-1]; }
} //****************************************************************************************** // bin_search() // This function continues narrowing the part of the sequence being // searched until only one term of the sequence remains. //****************************************************************************************** int bin_search(/*in*/ long item_number[],// the list to be searched int len, // the lenght of the list int value) // the vlaue to be looked up { int middle; // initialize the middle int lower=0; // initialize lower int upper = len-1; // initialize upper to be // the index of the last // element in the list bool found = false; while ((upper >= lower) && (!found))// as long as upper is greater // than or equal to lower and { // found is false middle = (lower + upper)/2; // middle of interval if (value == item_number[middle])// value equal to middle value // in the list found = true; else if (value < item_number[middle])// if value is smaller than // middle value in the list upper= middle-1; else lower = middle + 1; } if (found = true) return middle; else return -1; }
Hvis den fx siger at NameType er udefineret ved linien: void fill_list(ifstream &infile, NameType item[], long item_number[], float item_price[], int& len);
er det fordi du jo først definerer den senere! Fly linien: typedef char NameType[MAX_NAME +1]; //An extra space for \0 character op FØR prototypen for fill_list!
Takker. Jo, det hjalp mig bestemt. Jeg var overbevist om at jeg skulle definere dem i main, men det gaar jo ikke. Der nu er kun 4 fejl og en advarsel. Disse fejl er det som jeg oprindeligt havde problemer med og som jeg har proevet at loese uden held. Kan ikke forstaa hvad der er galt med min shift_right og bin_search funktioner Har kopieret fejlene:
Compiling... Assignment 5.cpp C:\NinaDocuments\Assignment 5.cpp(176) : error C2440: '=' : cannot convert from 'int' to 'char [21]' There are no conversions to array types, although there are conversions to references or pointers to arrays C:\NinaDocuments\Assignment 5.cpp(178) : warning C4244: '=' : conversion from 'int' to 'float', possible loss of data C:\NinaDocuments\Assignment 5.cpp(295) : error C2601: 'shift_right' : local function definitions are illegal C:\NinaDocuments\Assignment 5.cpp(310) : error C2601: 'bin_search' : local function definitions are illegal C:\NinaDocuments\Assignment 5.cpp(339) : fatal error C1004: unexpected end of file found Error executing cl.exe.
Det kunne kompileren godt lide. Jeg kan godt se logikken i at goere som du goer. Nu er fejlmeddelsen: Assignment 5.cpp C:\MyDocuments\Assignment 5.cpp(293) : error C2601: 'shift_right' : local function definitions are illegal C:\MyDocuments\Assignment 5.cpp(308) : error C2601: 'bin_search' : local function definitions are illegal C:\MyDocuments\Assignment 5.cpp(337) : fatal error C1004: unexpected end of file found Error executing cl.exe.
Jeg skal desvaerre i skole nu. Takker mange gange for din hjaelp, jeg er imponeret over hvor hurtigt du svarer. Det ville vaere rigtig rart hvis du kan se hvad der er galt med shift_right og bin_search.
Tak jpk, Det fik bestemt ryddet ud i alle de dumme fejl jeg havde lavet. Men mine funktioner er vist fejlagtige, saa jeg skal til at se paa det.
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.