Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strcmp help

Status
Not open for further replies.

Rellicea

Technical User
Joined
Apr 5, 2006
Messages
4
Location
GB
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
 
1. Please use [code][/code] tags when posting code.

2. 'error C2664: 'strcmp' : cannot convert parameter 1 from 'char [10][7]' to 'const char *'
Without some kind of indication in the listing, like
// This is the line which errors
Few people will even bother to look at your code.

FYI, I think you need to do
[tt]if (strcmp (code[pos], code_user) != 0)[/tt]
But that would imply some kind of loop to check each code in the array.

> #include <stdlib.h> <- this is a C header
> #include <conio.h> <- this is a DOS header
> #include <iomanip.h> <- this is an OLD C++ header.
Something this simple should not need such a wide variety of headers.

--
 
Thanks for your reply, sorry about not using tags or indicating where the error lies.

I'll looking into incorpating your help into a for loop and see how I go.

As for the headers, I was instructed by someone to include those headers and have been taught to use them when performing some of the tasks including in the program.

 
I have implemented changes and the program now compiles, however no matter what code I search for the program accepts it, so even if I input an invalid code the program accepts it as correct.

Here is the function:
Code:
void reserve (int stands[])  //function header paramater list with variables
{
char code_user[7];
int user_reserve, i;   //declaration for user inputted number
for (i = 0; i <9; i ++)
{
  cout<<"Please insert event code"<<endl;  //user instructions
  cin>>code_user;    //read in fair code
    if (strcmp (code[i], 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
	   break;
	}
    else 
	{
	   cout<<"Incorrect code"<<endl;
	}
}
}

Any ideas anyone? It's certainly progress on the way it was before but I'm stumped as to why the strcmp isn't working.
 
Shouldn't your strcmp() be comparing against == 0 instead of != 0

0 means the strings are identical.
Anything other than 0 means they are different.
 
Thanks cpjust :)

I've fixed that oversight and it now works, however not perfectly. The function works fine if a correct code is input however if an incorrect code is input then the error message is displayed 8 times then the loop is left :(

Code:
void reserve (int stands[])  //function header paramater list with variables
{
char code_user[7];
int user_reserve, i;   //declaration for user inputted number
  cout<<"Please insert event code"<<endl;  //user instructions
  cin>>code_user;    //read in fair code
for (i = 0; i <9; i ++)
{
    if (strcmp (code[i], 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[i] = stands[i] - user_reserve; //calculation to add removed stands to the number of available stands
	   cout<<stands[i]<<endl;
	   break;
	}
	else
	{
     if (i <8)
	 {
       cout<<"Incorect code"<<endl;
	 }
	}
}

}

Any ideas how I can fix this?
 
Code:
bool found = false;
for (i = 0; i <9; i ++)
{
    if (strcmp (code[i], 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[i] = stands[i] - user_reserve; //calculation to add removed stands to the number of available stands
       cout<<stands[i]<<endl;
       found = true;
       break;
    }
}
if ( ! found ) {
  // message
}

--
 
Tastes differs but in that program case I prefer this pattern:
Code:
int i;
for (i = 0; i < 9 && strcmp(code[i],code_user; ++i)
   ;
if (i >= 9)
{
... Not found reaction ...
}
Let the loop makes its job (array scan). The reaction is not a logical part of this loop body...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top