| | |
comparing strings
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
I am trying to make a program that needs to compare a string. here is a basic program of what i mean, i want it to say "it worked" but it seems to only read as not true. anyone know how to do this? thanx for any help.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <conio> #pragma hdrstop #include <condefs.h> //--------------------------------------------------------------------------- #pragma argsused int main(int argc, char **argv) { char *name = new char [100]; cout << "the word is silver (all lower case)." << endl; cout << "enter name:"; cin >> name; if (name == "silver") { cout << "it worked." << endl; } else if (name != "silver"){ cout << "it didn't work." << endl; } cout << name; getch(); return 0; }
•
•
•
•
Originally Posted by winbatch
You can't compare strings with == (just like you can't assign strings with = ), use:
if ( strcmp( first_string, second_string) == 0 )
C++ Syntax (Toggle Plain Text)
char *test = new char [100]; test = "this works";
p.s. i tried what you said and it work.
>yes you can asign them with =, try this
Brilliant Holmes, nice memory leak you've got there. First you assign memory to a pointer, then you reseat the pointer to a string literal, thus losing a reference to the memory you just allocated.
You need to figure out the difference between an array, a pointer, and a string before trying stuff like that.
Brilliant Holmes, nice memory leak you've got there. First you assign memory to a pointer, then you reseat the pointer to a string literal, thus losing a reference to the memory you just allocated.
You need to figure out the difference between an array, a pointer, and a string before trying stuff like that.
New members chased away this month: 3
•
•
•
•
Originally Posted by evilsilver
yes you can asign them with =, try thisC++ Syntax (Toggle Plain Text)
char *test = new char [100]; test = "this works";
Last edited by Dave Sinkula; Feb 12th, 2005 at 11:34 pm. Reason: D'oh!
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Dec 2004
Posts: 22
Reputation:
Solved Threads: 1
When I run your programme using Dev C++ at the line
I add-watch to the comparison
I get
name occurs like C string and is delimited by '\0'. So name is "silver\0" but not "silver".
C++ Syntax (Toggle Plain Text)
if (name == "silver") {
I add-watch to the comparison
C++ Syntax (Toggle Plain Text)
name == "silver"
I get
C++ Syntax (Toggle Plain Text)
name == {115's', 105'i', 108'l', 118'v', 101'e', 114'r', 0'\0'}
name occurs like C string and is delimited by '\0'. So name is "silver\0" but not "silver".
![]() |
Similar Threads
- Comparing strings in Java? (Java)
- Help for comparing strings and copying strings please. (C++)
- problem comparing strings (PHP)
- Comparing Strings (Assembly)
- Error comparing strings from arrays (PHP)
- Comparing Strings in C# (C#)
- comparing two strings with linear search.. (Java)
Other Threads in the C++ Forum
- Previous Thread: FlushConsoleInputBuffer() not doing it's job
- Next Thread: deitel C++ 4th edition question??
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph homeworkhelp iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






