| | |
Compare strings...
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2004
Posts: 4
Reputation:
Solved Threads: 0
Hi there,
Could some1 tell me whats wrong with this code...I know i shudnt b using an int variable but whats the right one for a string...
Thanx a million
Could some1 tell me whats wrong with this code...I know i shudnt b using an int variable but whats the right one for a string...
C++ Syntax (Toggle Plain Text)
#include <iostream.h> void main() { int a,b; cout <<"Enter the first name"; cin >>a; cout <<"Enter the 2nd no"; cin >>b; ((a=="Tom") && (b=="Pam")) ? cout<<"Yesss..." : cout<<"Noo"; }
•
•
Join Date: Mar 2004
Posts: 1,620
Reputation:
Solved Threads: 51
Hello,
No, int variables and character / string variables are different types. You want to do something like this:
Now, you also wanted to compare if Firstname == Tom, and Nextname == Pam.
Couple things to remember:
Tom <> tom, Tom <> tOm <> TOm
Pam <> pam
Error checking, my friend!
In your studies, you may have heard of the string processing library. You should use one function to make all the data ALL CAPS or all smalls, and use another function to compare the strings. Here is a hint... strcmp
Good Luck,
Christian
No, int variables and character / string variables are different types. You want to do something like this:
C++ Syntax (Toggle Plain Text)
// Have to declare values and [size] // Note that size must be large enough to handle the // projected size of the word/sentance *and* the return character // Overloads will overwrite other data in memory (BAD!) // Compilers might handle overloads differently. { char firstname[10], nextname[10]; cout << "Enter the firstname: "; cin >> firstname; }
Now, you also wanted to compare if Firstname == Tom, and Nextname == Pam.
Couple things to remember:
Tom <> tom, Tom <> tOm <> TOm
Pam <> pam
Error checking, my friend!
In your studies, you may have heard of the string processing library. You should use one function to make all the data ALL CAPS or all smalls, and use another function to compare the strings. Here is a hint... strcmp
Good Luck,
Christian
•
•
•
•
Originally Posted by FireNet
Use string.h
returns 0 if they are equalC++ Syntax (Toggle Plain Text)
strcmp(str1,str2);
C++ Syntax (Toggle Plain Text)
if ((int a = strcmp(stringa.c_str(), stringb.c_str())) == 0) { cout << "Match" << endl; } else { cout << "No match" << endl; }
C++ Syntax (Toggle Plain Text)
if (stringa == stringb) { cout << "Match" << endl; } else { cout << "No match" << endl; }
Ben
Application development, webhosting, and much more: www.webcentric-hosting.com
![]() |
Similar Threads
- Compare Strings and read in as Groups (C++)
- Can you compare strings with if? (C++)
- Seeks help to compare strings stored in arrays (Shell Scripting)
- Compare strings (C++)
- compare two strings or compare two files? (Visual Basic 4 / 5 / 6)
- How to compare two strings? (C)
- Please help me compare strings (Java)
Other Threads in the C++ Forum
- Previous Thread: Mode 13 graphics,Problem with io streams and new operator
- Next Thread: Function that returns void
| Thread Tools | Search this Thread |
add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory microsoft multidimensional newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct studio template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






