Strcmp help - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Strcmp help

RelliceaRellicea Posts: 5Member
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
#include
#include
#include
#include
#include
//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 <<"

Unable 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: "

**Program Terminates**
"; //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[i]; //read in fair code
inFile>>price[i]; //read in price
inFile>>stands[i]; //read in available amount of stands
inFile.get(); //use .get function for the description as it contains spaces
inFile.get(des[i], 39); //read in description
inFile.get ();
cout<<setw(6)<<code[i]<<setw(5)<<price[i]<<setw(5)<<stands[i]<<setw(40)<<des[i]<<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[i]; //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

Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2006-4-5 9:33:30[/red][/b][hr]
    >>strcmp (code, code_user)

    your compiler is correct -- variable code (the first parameter) is a 2d array of strings. strcmp() wants only a 1d array.
    Something like this. But you will have to figure out which of the 10 rows in code you want to compare, or create a loop to compare them all.

    [code]strcmp (code[0], code_user) [/code]

    [code]
    for (i = 1; i <11; i ++) //do 10 times
    {
    outFile<<code[i]; //read array into file
    if (i <10)
    [/code]
    arrays indices are numbered 0 to 9, not 1 to 10 as you posted it. The loop should be like this:
    [code]
    for (i = 0 i <10; i ++) //do 10 times
    [/code]


  • RelliceaRellicea Posts: 5Member
    [b][red]This message was edited by Rellicea at 2006-4-5 10:37:45[/red][/b][hr]
    Thanks for your help, it has made things a lot clearer :)

    I'll make some changes and see how I go.


  • RelliceaRellicea Posts: 5Member
    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;
    }
    }
    }[/code]

    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.
  • stoberstober Posts: 9,765Member ✭✭✭
    >> if (strcmp (code[i], code_user) != 0)

    Remember, strcmp() returns 0 if both strings are the same, and non-0 if they are different. So probably you should be usine == operator, not != operator.
  • RelliceaRellicea Posts: 5Member
    Thanks sober :)

    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;
    }
    }
    }

    }[/code]

    Any ideas how I can fix this?
  • LundinLundin Posts: 3,711Member
    : Thanks sober :)
    :
    : 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;
    [red][b]break;[/b][/red]
    : }
    : }
    : }
    :
    : }[/code]
    :
    : Any ideas how I can fix this?
    :

  • RelliceaRellicea Posts: 5Member
    : : [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;
    : [red][b]break;[/b][/red]
    : : }
    : : }
    : : }
    : :
    : : }[/code]

    I have added that break, however now the program states every input is incorrect even when it is not.
  • tsagldtsagld Posts: 621Member
    : : : [code]void reserve (int stands[]) //function header paramater list with variables
    : : : {
    : : : char code_user[red][10][/red][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 <[red]10[/red]; 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;
    : : break;
    : : : }
    : : : }
    : : : }
    : : :
    : : : }[/code]
    :
    : I have added that break, however now the program states every input is incorrect even when it is not.
    :

    Looks like you've lost something along the way. See code in red above.

    Greets,
    Eric Goldstein
    www.gvh-maatwerk.nl


Sign In or Register to comment.