By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 821 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

beginner string question

P: n/a
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.
Jul 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
In message <Sk******************@bignews6.bellsouth.net>, Sonoman
<fc*****@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
Jul 19 '05 #2

P: n/a
Hi,

see below
"Sonoman" <fc*****@cse.fau.edu> wrote in message
news:Sk******************@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

Jul 19 '05 #3

P: n/a
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" <fc*****@cse.fau.edu> wrote in message
news:Sk******************@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.

Jul 19 '05 #4

P: n/a
> 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

Jul 19 '05 #5

P: n/a
Bryce <Br**********@sZoEhRoOcSoPdAeM.com> writes:
In message <Sk******************@bignews6.bellsouth.net>, Sonoman
<fc*****@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
Jul 19 '05 #6

P: n/a
In article <gd*****************@bignews5.bellsouth.net>,
Sonoman <fc*****@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 <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 19 '05 #7

P: n/a
Thank you everyone for the help, you are all a great bunch of people.
"Sonoman" <fc*****@cse.fau.edu> wrote in message
news:Sk******************@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.

Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.