Hi,
The problem I am currectly having is with the string function strcmp. I am trying to compare an array of 10 codes with a user inputted code, so when the strcmp finds the user inputted code in the array it can then edit that field in the array. So basically a process to search for a code and then edit that code in the array.
I'm not quite sure how to do all that but I have attempted the strcmp unsuccesfully, I keep getting the error:
'error C2664: 'strcmp' : cannot convert parameter 1 from 'char [10][7]' to 'const char *'
Here is my code, the code array is located above main and the function in question is 'reserve':
#include "stdafx.h"
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <conio.h>
#include <iomanip.h>
#include <string.h>
//Function declarations
void getData (ifstream &inFile, float price[], int stands[]);
void menu1 ();
void reserve (int stands[]);
void remove (int stands[]);
void menu2 ();
char code[10] [7], des[10] [41];
int main(int argc, char* argv[])
{
ifstream inFile; //declaration for input file
float price[10]; //declaration for the cost
int stands[10]; //declaration for the ampunt of stands available
int menu_choice, menu_choice2;
cout << "Welcome to the Reservation Program for Trade Fairs Ltd" <<endl; //welcome message
inFile.open ("L:\\C++\\file.txt",ios::in/ios::nocreate); //opening the external file
if (!inFile) //check if file opens correctly
{
cout <<"\n\nUnable to open file!" << endl; //error message if file cant be opened
exit(2); //exit the program
}
else
{
getData (inFile, price, stands); //call function to retrieve data from file
}
menu1 (); //call 1st menu function
cin>> menu_choice; //read in users menu choice
//while ((menu_choice != '1')&&(menu_choice != '2'));
switch (menu_choice) //switch case menu
{
case 1: reserve (stands); //call function to reserve a stand
break;
case 2: remove (stands); //call function to reserve a stand
break;
default: cout<<"Invalid Option"<<endl;
return 0;
}
menu2 (); //call 2nd menu fuction
cin>> menu_choice2; //read in user choice
switch (menu_choice2) //switch case menu
{
case 1: system("CLS"); //clear screen
menu1 (); //call 1st menu function
break;
case 2: "\n\n**Program Terminates**\n"; //end program
break;
//error message for invalid character input
default: cout<<"Invalid Option"<<endl;
}
return 0;
}
//function to read data from a file and place it into an array
void getData (ifstream &inFile, float price[], int stands[]) //Function header parameter list including variables
{
int i; //decalartion for array sub
cout<<""<<endl;
cout<<setw(1)<<"Code"<<setw(8)<<"Price"<<setw(8)<<"Stands"<<setw(35)<<"Description"<<endl;
for (i = 1; i <10; i ++) //do loop 10 times, increment each time
{
inFile>>code; //read in fair code
inFile>>price; //read in price
inFile>>stands; //read in available amount of stands
inFile.get(); //use .get function for the description as it contains spaces
inFile.get(des, 39); //read in description
inFile.get ();
cout<<setw(6)<<code<<setw(5)<<price<<setw(5)<<stands<<setw(40)<<des<<endl;
}
inFile.close(); //close file
cout<<""<<endl;
}
//function to display a menu for the user to choose whether they would to reserve or remove a stand
void menu1 ()
{
cout<<"1.Make a reservation"<<endl; //menu text
cout<<"2.Remove a reservation"<<endl; //menu text
cout<<"Input your choice"<<endl; //instructions for menu
}
//function to reserve a stand, user inputs which fair to reserve for, then states how many stands to reserve
//that number is then removed from the available number of stands
void reserve (int stands[]) //function header paramater list with variables
{
char code_user[7];
int user_reserve; //declaration for user inputted number
cout<<"Please insert event code"<<endl; //user instructions
cin>>code_user; //read in fair code
if (strcmp (code, code_user) != 0)
{
cout<<"Please insert the number of stands you would like to reserve"<<endl; //user instructions
cin>>user_reserve; //read in amount to reserve
stands = stands - user_reserve; //calculation to add removed stands to the number of available stands
}
else
{
cout<<"Incorrect code"<<endl;
}
}
//function to remove a reservation, user inputs which fair to remove for, then states how many stands to remove
//that number is then added to the available number of stands
void remove (int stands[]) //function header paramater list with variables
{
int user_remove; //declaration for user inputted number
char code_user[7];
cout<<"Please insert event code"<<endl; //user instructions
cin>>code_user; //read in fair code
cout<<"Please insert the number of stands you would like to reserve"<<endl; //user instructions
cin>>user_remove; //read in amount to reserve
stands = stands + user_remove; //calculation to remove reserved stands from number of available stands
}
//function to write array to a file
void write_file_data (ofstream &outFile)
{
int i; //declartion of array sub
for (i = 1; i <11; i ++) //do 10 times
{
outFile<<code; //read array into file
if (i <10)
{
outFile<< endl; // write end of record marker
}
}
}
//function to display a menu to ask the user whether to make more changes or quit
void menu2 ()
{
cout<<"1.Make more changes"<<endl; //menu text
cout<<"2.Exit"<<endl; //menu text
cout<<"Input your choice"<<endl; //user instruction
}
Any help would be much apprecited, thanks
Regards
Chris Rice
The problem I am currectly having is with the string function strcmp. I am trying to compare an array of 10 codes with a user inputted code, so when the strcmp finds the user inputted code in the array it can then edit that field in the array. So basically a process to search for a code and then edit that code in the array.
I'm not quite sure how to do all that but I have attempted the strcmp unsuccesfully, I keep getting the error:
'error C2664: 'strcmp' : cannot convert parameter 1 from 'char [10][7]' to 'const char *'
Here is my code, the code array is located above main and the function in question is 'reserve':
#include "stdafx.h"
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <conio.h>
#include <iomanip.h>
#include <string.h>
//Function declarations
void getData (ifstream &inFile, float price[], int stands[]);
void menu1 ();
void reserve (int stands[]);
void remove (int stands[]);
void menu2 ();
char code[10] [7], des[10] [41];
int main(int argc, char* argv[])
{
ifstream inFile; //declaration for input file
float price[10]; //declaration for the cost
int stands[10]; //declaration for the ampunt of stands available
int menu_choice, menu_choice2;
cout << "Welcome to the Reservation Program for Trade Fairs Ltd" <<endl; //welcome message
inFile.open ("L:\\C++\\file.txt",ios::in/ios::nocreate); //opening the external file
if (!inFile) //check if file opens correctly
{
cout <<"\n\nUnable to open file!" << endl; //error message if file cant be opened
exit(2); //exit the program
}
else
{
getData (inFile, price, stands); //call function to retrieve data from file
}
menu1 (); //call 1st menu function
cin>> menu_choice; //read in users menu choice
//while ((menu_choice != '1')&&(menu_choice != '2'));
switch (menu_choice) //switch case menu
{
case 1: reserve (stands); //call function to reserve a stand
break;
case 2: remove (stands); //call function to reserve a stand
break;
default: cout<<"Invalid Option"<<endl;
return 0;
}
menu2 (); //call 2nd menu fuction
cin>> menu_choice2; //read in user choice
switch (menu_choice2) //switch case menu
{
case 1: system("CLS"); //clear screen
menu1 (); //call 1st menu function
break;
case 2: "\n\n**Program Terminates**\n"; //end program
break;
//error message for invalid character input
default: cout<<"Invalid Option"<<endl;
}
return 0;
}
//function to read data from a file and place it into an array
void getData (ifstream &inFile, float price[], int stands[]) //Function header parameter list including variables
{
int i; //decalartion for array sub
cout<<""<<endl;
cout<<setw(1)<<"Code"<<setw(8)<<"Price"<<setw(8)<<"Stands"<<setw(35)<<"Description"<<endl;
for (i = 1; i <10; i ++) //do loop 10 times, increment each time
{
inFile>>code; //read in fair code
inFile>>price; //read in price
inFile>>stands; //read in available amount of stands
inFile.get(); //use .get function for the description as it contains spaces
inFile.get(des, 39); //read in description
inFile.get ();
cout<<setw(6)<<code<<setw(5)<<price<<setw(5)<<stands<<setw(40)<<des<<endl;
}
inFile.close(); //close file
cout<<""<<endl;
}
//function to display a menu for the user to choose whether they would to reserve or remove a stand
void menu1 ()
{
cout<<"1.Make a reservation"<<endl; //menu text
cout<<"2.Remove a reservation"<<endl; //menu text
cout<<"Input your choice"<<endl; //instructions for menu
}
//function to reserve a stand, user inputs which fair to reserve for, then states how many stands to reserve
//that number is then removed from the available number of stands
void reserve (int stands[]) //function header paramater list with variables
{
char code_user[7];
int user_reserve; //declaration for user inputted number
cout<<"Please insert event code"<<endl; //user instructions
cin>>code_user; //read in fair code
if (strcmp (code, code_user) != 0)
{
cout<<"Please insert the number of stands you would like to reserve"<<endl; //user instructions
cin>>user_reserve; //read in amount to reserve
stands = stands - user_reserve; //calculation to add removed stands to the number of available stands
}
else
{
cout<<"Incorrect code"<<endl;
}
}
//function to remove a reservation, user inputs which fair to remove for, then states how many stands to remove
//that number is then added to the available number of stands
void remove (int stands[]) //function header paramater list with variables
{
int user_remove; //declaration for user inputted number
char code_user[7];
cout<<"Please insert event code"<<endl; //user instructions
cin>>code_user; //read in fair code
cout<<"Please insert the number of stands you would like to reserve"<<endl; //user instructions
cin>>user_remove; //read in amount to reserve
stands = stands + user_remove; //calculation to remove reserved stands from number of available stands
}
//function to write array to a file
void write_file_data (ofstream &outFile)
{
int i; //declartion of array sub
for (i = 1; i <11; i ++) //do 10 times
{
outFile<<code; //read array into file
if (i <10)
{
outFile<< endl; // write end of record marker
}
}
}
//function to display a menu to ask the user whether to make more changes or quit
void menu2 ()
{
cout<<"1.Make more changes"<<endl; //menu text
cout<<"2.Exit"<<endl; //menu text
cout<<"Input your choice"<<endl; //user instruction
}
Any help would be much apprecited, thanks
Regards
Chris Rice