By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

help me!!!! how to compare array with the string?

P: 3
Hi, i am currently doing a c++ program using Borland C++.
now i am facing a problem where i want to compare the keyword input from the user with the string that read from file.

this is a part of my codding that with error,

Expand|Select|Wrap|Line Numbers
  1. int position,py;
  2. char keyPrompt[100];
  3.  
  4. input.open("a.dat");
  5.   if (input.fail())
  6.  {
  7.   cout<<"Unable to open a file \n";
  8.   exit(1);
  9.   }//end if
  10.  
  11.   while(input>>alpha>>index)
  12.   {
  13.       Alpha[a].setkeyword(keyword);
  14.       Alpha[a].setindex(index);
  15.       a++;
  16.       }//end while
  17.     input.close();
  18.  
  19.  
  20.  
  21. cout<< enter keyword:";
  22. cin>>keyPrompt;
  23.  
  24. for(py=0; py<a; py++)
  25.      { //compare the first alphabet with the alphabet in file
  26.       if(strcmp(keyPrompt[0],Alpha[py].getkeyword())==0)
  27.  
  28.           {//alphabet match
  29.         position=py; //position in alpha array
  30.          break;
  31.        } //end if
i know that it is impossible to compare between keyPrompt[0] and string , but can anyone help me to search for another alternative that can use as similar as

Expand|Select|Wrap|Line Numbers
  1. strcmp(keyPrompt[0],Alpha[py],getkeyword())   
thanks!
Mar 16 '07 #1
Share this Question
Share on Google+
5 Replies


Expert 100+
P: 1,510
what does Alpha[py].getkeyword() return, a char * or a C++ string?

as you are working in C++ why not just use string type? e.g.
Alpha[py].getkeyword()
Mar 16 '07 #2

P: 3
what does Alpha[py].getkeyword() return, a char * or a C++ string?

as you are working in C++ why not just use string type? e.g.
Alpha[py].getkeyword()


char * in my program.....

char *Database::getkeyword()
{
return Keyword;
}; //method getkeyword
Mar 16 '07 #3

Expert 100+
P: 1,510
char * in my program.....

char *Database::getkeyword()
{
return Keyword;
}; //method getkeyword
keyPrompt[0] is a char and strcpy expects its parameters to be char *
should the compare
Expand|Select|Wrap|Line Numbers
  1.       if(strcmp(keyPrompt[0],Alpha[py].getkeyword())==0)
  2.  
be
Expand|Select|Wrap|Line Numbers
  1.       if(strcmp(keyPrompt, Alpha[py].getkeyword())==0)
  2.  
Mar 16 '07 #4

P: 3
keyPrompt[0] is a char and strcpy expects its parameters to be char *
should the compare
Expand|Select|Wrap|Line Numbers
  1.       if(strcmp(keyPrompt[0],Alpha[py].getkeyword())==0)
  2.  
be
Expand|Select|Wrap|Line Numbers
  1.       if(strcmp(keyPrompt, Alpha[py].getkeyword())==0)
  2.  

yap... i know that but i juz wana compare "keyPrompt[0]" with the string that i get from .dat file.....
for example:
if user Input Login
i just wana detect if the first word (keyPrompt[0]) is same as the data that i get from .dat file where the structure of .dat file is as below

a idx1
b idx2
c idx3
d idx4
.
.
l
.
z idx24

like tat... got any way to write this kind of programing code?
Mar 17 '07 #5

Expert 100+
P: 1,510
if you wish to check character by character you could try
Expand|Select|Wrap|Line Numbers
  1.      if(strcmp(keyPrompt[0], Alpha[py].getkeyword()[0])==0)
Mar 17 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.