Topic: if" problems?
Author: rad6938@gemini.tntech.edu (Rad)
Date: 8 Jan 95 11:58:26 -0600 Raw View
In article <3ecfjk$3o7@mudraker.mtholyoke.edu>, lodonnel@mtholyoke.edu (Laurel) writes:
>Hiya, I'm pretty much new to C++, and I'm, having a little trouble with
>the following:
>
>char x[80];
>printf("Command: ");
>gets(x);
>if (x == "r")
> printf("You typed r.");
>
>Problem being, the if statement wont work even if x does equal "r." BUt
>if I use if (x != "r") it works. I'm lead to beleive that it might be a
>problem with gets(x) ? Any help here would be greatly appreciated.
The problem here is that you are comparing addresses and not strings.
When you use the name of an array by its self in an expression the address of
the first element of that array is used. The same thing with a string
constant; you are using the address of the first character in that string.
What I assume you want can be done using the library function strcmp() as
follows:
if (strcmp (x,"r") == 0)
printf ("You typed r.\n");
----------------------------------------------------------------------------
Richard Deken Graduate student in electrical engineering
PGP public key available Tennessee Technological University
Internet: rad6938@gemini.tntech.edu Cookeville, TN, USA
Author: lodonnel@mtholyoke.edu (Laurel)
Date: 3 Jan 1995 21:29:24 GMT Raw View
Hiya, I'm pretty much new to C++, and I'm, having a little trouble with
the following:
char x[80];
printf("Command: ");
gets(x);
if (x == "r")
printf("You typed r.");
Problem being, the if statement wont work even if x does equal "r." BUt
if I use if (x != "r") it works. I'm lead to beleive that it might be a
problem with gets(x) ? Any help here would be greatly appreciated.
Thanks much!
Ryan O'Donnell
Author: scotc@meaddata.com (Scot Cunningham)
Date: 4 Jan 1995 15:12:24 GMT Raw View
Laurel (lodonnel@mtholyoke.edu) wrote:
: Hiya, I'm pretty much new to C++, and I'm, having a little trouble with
: the following:
Pretty much new to C as well, eh? Always glad to help a newbie.
1: char x[80];
2: printf("Command: ");
3: gets(x);
4: if (x == "r")
5: printf("You typed r.");
: Problem being, the if statement wont work even if x does equal "r." BUt
: if I use if (x != "r") it works. I'm lead to beleive that it might be a
: problem with gets(x) ? Any help here would be greatly appreciated.
Your variable x is the address of an area of memory that you declared
in line 1. The string "r" compiles out to be another area of memory
containing the character 'r' followed by a null. In the expression
x == "r" in line 4, you are comparing these two addresses, not the data
in them. They are not the same area of memory and, therefore, not equal.
Make sense?
What you probably want to do is this:
#include <stdio.h>
#include <string.h>
char x[80];
printf("Command: ");
gets(x);
if (strcmp(x,"r") == 0)
printf("You typed r.");
You might want to familiarize yourself a little more with C before
delving into the pleasures of C++. Just a suggestion.
Pick up a copy of the K&R C book.
: Thanks much!
: Ryan O'Donnell
:
Author: nick@avenger.fcc.cpf.navy.mil (Nick Nichols)
Date: Wed, 4 Jan 1995 14:44:41 GMT Raw View
In article <3ecfjk$3o7@mudraker.mtholyoke.edu>, lodonnel@mtholyoke.edu
says...
>
>Hiya, I'm pretty much new to C++, and I'm, having a little trouble with
>the following:
>
>char x[80];
>printf("Command: ");
>gets(x);
>if (x == "r")
> printf("You typed r.");
>
>Problem being, the if statement wont work even if x does equal "r." BUt
>if I use if (x != "r") it works. I'm lead to beleive that it might be a
>problem with gets(x) ? Any help here would be greatly appreciated.
>
>Thanks much!
>Ryan O'Donnell
>
You can't compare a string in that way. You need to use something like
"strcmp". If your using Borlands package there are more powerfull string
comparasions but for now that one will work for ya.
syntax goes like this :
< int strcmp(const char *s1, const char*s2); >
for the const char*s2 you should be able to insert the "r" and that
should give you the results your looking for.
nick@avenger.fcc.cpf.navy.mil