beginner string question

S

Sonoman

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.
 
B

Bryce

Sonoman said:
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)
{
}
 
C

Catalin Pitis

Hi,

see below


Sonoman said:
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
 
S

Sonoman

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.
 
P

Peter van Merkerk

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.
 
F

Frank Schmitt

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

#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
 
J

Jon Bell

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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top