| | |
Question about comparing a string with a character
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jan 2005
Posts: 31
Reputation:
Solved Threads: 0
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.
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.
>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:
Of course, this assumes that you fix the first problem and FirstCharacter actually is a null terminated string. You can do it like this:
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:
Or even better:
That avoids the need to even use a temporary variable.
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:
C++ Syntax (Toggle Plain Text)
if ( strcmp ( FirstCharacter, "*" ) == 0 )
C++ Syntax (Toggle Plain Text)
char FirstCharacter[2] = {0}; // ... FirstCharacter[0] = OutputFile.peek(); if ( strcmp ( FirstCharacter, "*" ) == 0 ) cout<<"Dummy record"<<endl;
C++ Syntax (Toggle Plain Text)
char FirstCharacter; // ... FirstCharacter = OutputFile.peek(); if ( FirstCharacter == '*' ) cout<<"Dummy record"<<endl;
C++ Syntax (Toggle Plain Text)
// ... if ( OutputFile.peek() == '*' ) cout<<"Dummy record"<<endl;
I'm here to prove you wrong.
![]() |
Similar Threads
- Append a Character(backslash) to a string at any position (C)
- C++ pointer/Struct help please (C++)
- Compare one character from a string with a string. (C++)
- Splitting words from string into character array (C++)
- question about php string function (PHP)
- Comparing String with a 2D Array (C++)
Other Threads in the C++ Forum
- Previous Thread: Inheritance & Derived Classes
- Next Thread: Basic Error Checking
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






