input.open("a.dat");
  if (input.fail())
 {
  cout<<"Unable to open a file \n";
  exit(1);
  }//end if

  while(input>>alp>>index)
  {//write to Usecase usecase
      Alpha[a].setkeyword(keyword);
      Alpha[a].setindex2(index);
      a++;
      }//end while
    input.close();

      cout<<"Please Insert The keyword:";
      int position,py;
      char keyPrompt[100];
      int i=0;
      char c;
      cin>>keyPrompt;
        
       
   for(py=0; py<a; py++)
     { 

      if(strcmp(keyPrompt[0],Alpha[py].getalphabet())==0)
      {
        position=py; 
         break;
       } //end if
        if(py==(a-1))
       {
        position=0;
       }//end if
       }//end for

i want to compare the input string array number 0 (the first letter) with the array data that got from that .dat file, but it got error.....

cannot convert "int" to 'const *char';

how can i solve it?

Recommended Answers

All 16 Replies

strcmp only works with C style strings and not single characters and strings. You are comparing a single character with a string which is wrong.

If you want to compare two characters, you can do something like:

int comparision_result = strncmp ( str1, str2, 1 ) ;

though it can be simply done with a comparision operator.

if (str1 [0] == str2 [0])
{
    // do something
}

thanks ~S.O.S~ but... im not understand with that.... can u explain more? the flow of this coding is as below: first, it will fetch and get the data from alphabet.dat file and store in array Alpha[a]; then user input a keyword (known as keyPrompt),, i would like to compare the first character with the data inside the Alpha[a],

But the information you provided is very minimal. What exactly are the data types of the variables defined and declared in the snippet you pasted.

Would you like to compare or search for a single character in Alpha [a] which I have no idea what it holds ?

And btw, you can't compare a single character with a whole string, if thats what you are planning to do...

i cant paste all the coding here cause it is too long and involve many different CPP file,

inside Array A[a] it is a character , cause inside alpha.dat is is only have 2 column, alphabet and index.

so, what i plan is compare the first letter that user input with that particular alphabet that get from alphabet.dat, if it is same, the program will set the position and the position is use by other code.

thanks, and sorry cause i also duno how to explain it clearly.....

I need to know what kind of problem you are facing with the code. Is it a compiler error, a runtime error or logic error ?

If the file contains only character and index, you can pull them in a character variable and an integer variable respectively. Accept the input from the user in the form of string, check the first char of the string. If it is the same as any one char present in the file, then you have found a match.

can u gv an example of the way u said juz now? thanks....

my error is codding error i think, cause
it display

----cannot convert 'char' to "*char"-----
and also the error juz now i state at the 1st post... thanks

i cant paste all the coding here cause it is too long and involve many different CPP file,

So post just the code section you are asking about. With details about what is going on and how the variables are defined, we can usually read code segments easier than 100s of lines of code anyway...

char *Generate::Gettestcase()
{
  int a=0;
  int x=0;
  int y;
  const int size=50;
 char keyword[100];
 char alphabet[1];
 char index[4];
  Database UC[size];
  Database Alpha[size];
  ifstream input;
  
  input.open("alphabet.dat");
  if (input.fail())
 {
  cout<<"Unable to open a file \n";
  exit(1);
  }//end if

  while(input>>alphabet>>index)
  {//write to Usecase usecase
      Alpha[a].setkeyword(keyword);
      Alpha[a].setindex(index);
      a++;
      }//end while
    input.close();

      cout<<"Please Insert The keyword:";
      int position,py;
      char keyPrompt[100];
      int i=0;
      char c;
      cin>>keyPrompt;
     


    /********************************AlphaCompare****************************/
   for(py=0; py<a; py++)
     { //compare the first alphabet with the alphabet in file

               cout<<Alpha[py].getalphabet();
               //cout<<alphabet;

      if(Alpha[py].getalphabet()==keyPrompt[0])
      {//alphabet match
        position=py; //position in alpha array
         break;
       } //end if
        if(py==(a-1))
       {
        position=0;
       }//end if
       }//end for

this is the code.....

Here is a minimalistic working program.

#include <iostream>
#include <fstream>
#include <string>
using namespace std ;

int main (void)
{
    ifstream in ( "a.txt" ) ;
    string input ;
    char ch ;
    int value ;

    cout << "Enter the keyword: " ;
    getline (cin, input) ;

    while ( in >> ch >> value )
    {
        if (input [0] == ch)
            cout << ch << " is present in the file and its value is " << value ;
    }

    getchar () ;
    return 0;
}

/* a.txt contents

a 1
b 2
c 3
d 4
*/

If you can't figure this one out, maybe you should read some tutorials on the internet and strengthen your concepts.

ok.... 10s.. i will try to read it out 1st...

hm... look like the way u write is different from wat i write........ so i need more time to understand it..........

did u understand the 2nd code that i post up juz now?

can u gv an example of the way u said juz now? thanks....
and also the error juz now i state at the 1st post... thanks

ok.... 10s.. i will try to read it out 1st...

hm... look like the way u write is different from wat i write........ so i need more time to understand it..........

did u understand the 2nd code that i post up juz now?

OK, it's now time for you to read the Rules. You should read them all, but this first paragraph is important...

commented: random rep++ from salem +6

I still can not make it, my compiler is older version borland C++ compiler, the thing that SOS teach canot run at that compiler.

any alternative way to do such as the coding below?

 for(py=0; py<a; py++)
     { //compare the first alphabet with the alphabet in file
      if(strcmp(keyPrompt[0],Alpha[py].getkeyword())==0)

          {//alphabet match
        position=py; //position in alpha array
         break;
       } //end if

because array (keyPrompt[0]) cannot be compare to the string that get from Alpha[py].getkeyword().

Why not get a new compiler which supports standard C++ and move with the world ?

And like I told you before, you need not use strcmp if all you are doing is comparing characters.

i got try to move to virtual C++ compiler, but my codding all become not match on it, because it is build up using borland compiler.

as a conclusion here, the program code that build up by different compiler canot be run although based on theory, the codding is C++ coding.

i got try to move to virtual C++ compiler, but my codding all become not match on it, because it is build up using borland compiler.

as a conclusion here, the program code that build up by different compiler canot be run although based on theory, the codding is C++ coding.

What are you using that's only available on Borland? I see nothing in what you posted that's Borland specific, so it should compile on any compiler.

If something doesn't work, you have to tell us why and how it failed and the code you used.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.