| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Sonoman" |
| Date: |
12 Nov 2003 12:23:44 AM |
| Object: |
beginner string question |
I am trying to do this:
cin >> temp;
if (temp == "n"){
Then do something...
}
temp was declared as a string and the input I give at the prompt is n, but
it skips the condition for the if statement when I think it should go into
the if statement. I tried swithching to 'n' for the condition but it gave me
a compile error. The complete code is here:
http://www.cse.fau.edu/~fcarpio/
and the offending lines are in the person.cpp file line 66. I know it must
be something simple, but I am failing to see it. There is no compiler error.
Thanks in advance.
.
|
|
| User: "Catalin Pitis" |
|
| Title: Re: beginner string question |
12 Nov 2003 02:30:02 AM |
|
|
Hi,
see below
"Sonoman" <fcarpio@cse.fau.edu> wrote in message
news:Skksb.11179$jA6.6383@bignews6.bellsouth.net...
I am trying to do this:
cin >> temp;
if (temp == "n"){
Then do something...
}
temp was declared as a string and the input I give at the prompt is n, but
it skips the condition for the if statement when I think it should go into
the if statement. I tried swithching to 'n' for the condition but it gave
me
a compile error. The complete code is here:
http://www.cse.fau.edu/~fcarpio/
Couldn't find the code you mentioned.
and the offending lines are in the person.cpp file line 66. I know it must
be something simple, but I am failing to see it. There is no compiler
error.
Use std::string instead of plain C-style strings. If you declared temp
something as:
char temp[ SOME_LEN];
then temp == "n" will compare two pointers instead of two strings. The
result of comparing will be always false.
If you use std::string like:
std::string temp;
then the operator== is overloaded and it will compare the two strings.
Catalin
.
|
|
|
|
| User: "Bryce" |
|
| Title: Re: beginner string question |
12 Nov 2003 01:18:39 AM |
|
|
In message <Skksb.11179$jA6.6383@bignews6.bellsouth.net>, Sonoman
<fcarpio@cse.fau.edu> writes
I am trying to do this:
cin >> temp;
if (temp == "n"){
Then do something...
}
temp was declared as a string and the input I give at the prompt is n, but
it skips the condition for the if statement when I think it should go into
the if statement. I tried swithching to 'n' for the condition but it gave me
a compile error. The complete code is here:
http://www.cse.fau.edu/~fcarpio/
and the offending lines are in the person.cpp file line 66. I know it must
be something simple, but I am failing to see it. There is no compiler error.
#include <string.h>
and try
if (strcmp(temp,"n") ==0)
{
}
--
Bryce
.
|
|
|
| User: "Frank Schmitt" |
|
| Title: Re: beginner string question |
12 Nov 2003 05:06:14 AM |
|
|
Bryce <Bryce-Usenet@sZoEhRoOcSoPdAeM.com> writes:
In message <Skksb.11179$jA6.6383@bignews6.bellsouth.net>, Sonoman
<fcarpio@cse.fau.edu> writes
I am trying to do this:
cin >> temp;
if (temp == "n"){
Then do something...
}
temp was declared as a string and the input I give at the prompt is n, but
it skips the condition for the if statement when I think it should go into
the if statement.
Note that the term "string" in C++ is normally(?) used for std::string - what
you've got is a C-style string (char*).
I tried swithching to 'n' for the condition but it gave me
a compile error. The complete code is here:
http://www.cse.fau.edu/~fcarpio/
and the offending lines are in the person.cpp file line 66. I know it must
be something simple, but I am failing to see it. There is no compiler error.
#include <string.h>
and try
if (strcmp(temp,"n") ==0)
{
}
or, much easier, use std::string
#include <string>
....
std::string temp;
cin >> temp;
if (temp == "n") {
....
HTH & kind regards
frank
--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com
.
|
|
|
|
|
| User: "Sonoman" |
|
| Title: Re: beginner string question |
12 Nov 2003 03:41:59 AM |
|
|
Sorry but I gave the wrong info, I declared "temp" as a character array if
that makes a difference. I did use strings but they were breaking up in
between spaces, so now I am using getline() with char[] arrays.
"Sonoman" <fcarpio@cse.fau.edu> wrote in message
news:Skksb.11179$jA6.6383@bignews6.bellsouth.net...
I am trying to do this:
cin >> temp;
if (temp == "n"){
Then do something...
}
temp was declared as a string and the input I give at the prompt is n, but
it skips the condition for the if statement when I think it should go into
the if statement. I tried swithching to 'n' for the condition but it gave
me
a compile error. The complete code is here:
http://www.cse.fau.edu/~fcarpio/
and the offending lines are in the person.cpp file line 66. I know it must
be something simple, but I am failing to see it. There is no compiler
error.
Thanks in advance.
.
|
|
|
| User: "Peter van Merkerk" |
|
| Title: Re: beginner string question |
12 Nov 2003 04:48:55 AM |
|
|
Sorry but I gave the wrong info, I declared "temp" as a character
array if
that makes a difference. I did use strings but they were breaking up
in
between spaces, so now I am using getline() with char[] arrays.
Don't use char[] arrays (or char pointers for that matter) for strings
if you don't have to. The std::string class makes your life (and that of
anyone who will have to deal with your code in the future) a lot easier.
The std::string class usually results in easier to read code, doesn't
impose arbitrary length limitations (which typically leads to safer
code) and does not require manual resource management.
The strings were breaking up in between spaces because of the way stream
I/O works, it is not a limitation of the string class itself. If you use
getline() i.c.w. std::string it will work as well.
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
.
|
|
|
|
| User: "Jon Bell" |
|
| Title: Re: beginner string question |
12 Nov 2003 09:25:55 AM |
|
|
In article <gdnsb.4730$k77.3767@bignews5.bellsouth.net>,
Sonoman <fcarpio@cse.fau.edu> wrote:
Sorry but I gave the wrong info, I declared "temp" as a character array if
that makes a difference. I did use strings but they were breaking up in
between spaces, so now I am using getline() with char[] arrays.
You can use getline() with strings; but you have to use the version that
is a free-standing function, not the one that is an istream member
function.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string line;
cout << "Enter something: ";
getline (cin, line);
cout << "You entered: " << line << endl;
return 0;
}
--
Jon Bell <jtbellap8@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
.
|
|
|
|
|
| User: "Sonoman" |
|
| Title: Re: beginner string question |
12 Nov 2003 10:02:47 AM |
|
|
Thank you everyone for the help, you are all a great bunch of people.
"Sonoman" <fcarpio@cse.fau.edu> wrote in message
news:Skksb.11179$jA6.6383@bignews6.bellsouth.net...
I am trying to do this:
cin >> temp;
if (temp == "n"){
Then do something...
}
temp was declared as a string and the input I give at the prompt is n, but
it skips the condition for the if statement when I think it should go into
the if statement. I tried swithching to 'n' for the condition but it gave
me
a compile error. The complete code is here:
http://www.cse.fau.edu/~fcarpio/
and the offending lines are in the person.cpp file line 66. I know it must
be something simple, but I am failing to see it. There is no compiler
error.
Thanks in advance.
.
|
|
|
|

|
Related Articles |
|
|