Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,114 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,763 people online right now. Registration is fast and FREE... Join Now!




Simple question

 
Reply to this topicStart new topic

Simple question, testing string values

Oats
7 Jun, 2007 - 12:55 PM
Post #1

New D.I.C Head
*

Joined: 3 Jun, 2007
Posts: 2


My Contributions
Just a simple question that I've been looking for the answer to for a few days, but I figured after awhile that it'd be easier to do it here.
I know that you can use scanf to store numbers and whatnot, as well as using it for if/then statements, but how do you do it with words and stuff? (The question didn't really make sense because I can't think of a good way to word it, so sorry.) Here's what I mean, sort of:
CODE
char something[20];
    
    printf("stuff? ");
    scanf("%s", something);

That's how I was doing it, but if you were to do if (something==toast), it doesn't work correctly. (This isn't from an actual program, so I just put in randomness since I'm kind of lazy.) If there is an easy way to do this, I'd appreciate some help.
User is offlineProfile CardPM
+Quote Post

ZlxA
RE: Simple Question
7 Jun, 2007 - 01:21 PM
Post #2

New D.I.C Head
*

Joined: 6 Nov, 2006
Posts: 10


My Contributions
QUOTE(Oats @ 7 Jun, 2007 - 01:55 PM) *

Just a simple question that I've been looking for the answer to for a few days, but I figured after awhile that it'd be easier to do it here.
I know that you can use scanf to store numbers and whatnot, as well as using it for if/then statements, but how do you do it with words and stuff? (The question didn't really make sense because I can't think of a good way to word it, so sorry.) Here's what I mean, sort of:
CODE
char something[20];
    
    printf("stuff? ");
    scanf("%s", something);

That's how I was doing it, but if you were to do if (something==toast), it doesn't work correctly. (This isn't from an actual program, so I just put in randomness since I'm kind of lazy.) If there is an easy way to do this, I'd appreciate some help.


if(something == "toast"), you need the quotes around the string you want to test it to, otherwise it takes it as toast being a variable name.

User is offlineProfile CardPM
+Quote Post

salindor
RE: Simple Question
7 Jun, 2007 - 02:44 PM
Post #3

D.I.C Head
Group Icon

Joined: 10 Nov, 2006
Posts: 156



Thanked: 4 times
Dream Kudos: 50
My Contributions
QUOTE(ZlxA @ 7 Jun, 2007 - 02:21 PM) *

QUOTE(Oats @ 7 Jun, 2007 - 01:55 PM) *

Just a simple question that I've been looking for the answer to for a few days, but I figured after awhile that it'd be easier to do it here.
I know that you can use scanf to store numbers and whatnot, as well as using it for if/then statements, but how do you do it with words and stuff? (The question didn't really make sense because I can't think of a good way to word it, so sorry.) Here's what I mean, sort of:
CODE
char something[20];
    
    printf("stuff? ");
    scanf("%s", something);

That's how I was doing it, but if you were to do if (something==toast), it doesn't work correctly. (This isn't from an actual program, so I just put in randomness since I'm kind of lazy.) If there is an easy way to do this, I'd appreciate some help.


if(something == "toast"), you need the quotes around the string you want to test it to, otherwise it takes it as toast being a variable name.


Just becareful with the above snippet. I know personally hackers look for code that looks like that, and will perform a buffer overrun by placing entering say a 40 character string instead of a 20 character string.

The above is really only good for test projects that will never see the light of day, but if your doing anything that could possibly be seen by anyone else, you really want to avoid using scanf.

Salindor
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Simple Question
7 Jun, 2007 - 03:20 PM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
In any case, you cannot use the equality operator with a character array. To compare c style strings, you'll need to use the strcmp() function:

http://www.cplusplus.com/reference/clibrar...ing/strcmp.html

The equality operator can be used with the C++ string object.
User is offlineProfile CardPM
+Quote Post

Oats
RE: Simple Question
8 Jun, 2007 - 06:58 PM
Post #5

New D.I.C Head
*

Joined: 3 Jun, 2007
Posts: 2


My Contributions
Hmm, I've tried plenty of times but I can't find a way to make it so that if szInput is equal to szKey it does one thing, but if not it says something different. I'm not sure there's a way to do it with the method in the link, but I've been trying and can't seem to get anything dry.gif
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Simple Question
8 Jun, 2007 - 08:23 PM
Post #6

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,441



Thanked: 64 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
CODE

#include <stdio.h>

#define MAX 20

int main(void) {
    char buff[MAX];
    
    printf("Please enter some words:");
    scanf("%s", &buff);
    
    printf("%s",buff);
    return 0;
}

But like it was pointed out, you want to run a check while gathering the text that it does not go over the pre-defined amount. When I perform this, I will read in one char at a time while checking the current possition in the array.

Good luck!
User is online!Profile CardPM
+Quote Post

Amadeus
RE: Simple Question
9 Jun, 2007 - 10:53 AM
Post #7

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
QUOTE(Oats @ 8 Jun, 2007 - 10:58 PM) *

Hmm, I've tried plenty of times but I can't find a way to make it so that if szInput is equal to szKey it does one thing, but if not it says something different. I'm not sure there's a way to do it with the method in the link, but I've been trying and can't seem to get anything dry.gif

Can you post the code your having problems with?

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:47PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month