User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 401,669 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,532 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Views: 2393 | Replies: 2 | Solved
Reply
Join Date: Jan 2005
Posts: 31
Reputation: tat2dlady is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
tat2dlady tat2dlady is offline Offline
Light Poster

Help Question about comparing a string with a character

  #1  
Mar 12th, 2005
Does anyone know how to compare a old style character string with a character in qoutes? Example:
I want to compare the first character in a record that is stored in an old style character string with a "*". Here's what I have in my code:

char FirstCharacter[1]; // Character string to hold to value found by
the peek() function

OutputFile.seekp(Total * 60);
//Seek to the correct position

FirstCharacter[0] = OutputFile.peek();
//FirstCharacter set equal to peek()

if (FirstCharacter[0] == "*")
{
cout << "Dummy Record";
}
//Try to compare value stored in FirstCharacter with a "*". If it is true, I know that I can write my record there.

It will not compare the string like it is. I get the error "Cannot convert char to char*". I tried strcmp() also, but to no avail. I get the error message "Cannot find a match for strcmp(char, char*). Anyone have any suggestions? Thanks a lot.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,059
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 419
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Question about comparing a string with a character

  #2  
Mar 12th, 2005
>char FirstCharacter[1]; // Character string to hold to value found by
This must be an empty string or it cannot be a string at all. C-style strings require a null character at the end as a terminator. You would really be better off just using a single char.

>if (FirstCharacter[0] == "*")
You don't compare strings with the == operator. Include <cstring> and do this:
if ( strcmp ( FirstCharacter, "*" ) == 0 )
Of course, this assumes that you fix the first problem and FirstCharacter actually is a null terminated string. You can do it like this:
char FirstCharacter[2] = {0};

// ...

FirstCharacter[0] = OutputFile.peek();

if ( strcmp ( FirstCharacter, "*" ) == 0 )
  cout<<"Dummy record"<<endl;
The "= {0}" part guarantees that a null character is placed in FirstCharacter[1]. But, as I said before, it's better to just use a character to begin with:
char FirstCharacter;

// ...

FirstCharacter = OutputFile.peek();

if ( FirstCharacter == '*' )
  cout<<"Dummy record"<<endl;
Or even better:
// ...

if ( OutputFile.peek() == '*' )
  cout<<"Dummy record"<<endl;
That avoids the need to even use a temporary variable.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Jan 2005
Posts: 31
Reputation: tat2dlady is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
tat2dlady tat2dlady is offline Offline
Light Poster

Re: Question about comparing a string with a character

  #3  
Mar 12th, 2005
Thank you. The "OutputFile.peek() == '*' " worked great. Thank you, thank you, thank you. I thought I had to store the value found with the peek() function.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 7:14 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC