473,320 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.
Jul 19 '05 #1
7 3465
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
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
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
> 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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there...
8
by: Bshealey786 | last post by:
Okay im doing my final project for my first computer science class(its my major, so it will be my first of many), but anyway im a beginner so im not to great with C++ yet. Anyway this is the error...
5
by: Tarjei Romtveit | last post by:
I'm still a newbie into C++ programming, so I got a quite foolish string related question. Using: Dev-cpp 4.9.9.2 (I think Dev-Cpp uses a gcc compiler of some sort) If i declare a char...
1
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am...
14
by: z_learning_tester | last post by:
But I can't seem to find the answer. The question is how do you reverse the words in a string? Or how do you reverse the numbers listed in a string? The example is usually something like: Turn...
5
by: Jacky Luk | last post by:
import java.awt.Graphics; public class printtest extends java.applet.Applet { public void init() { } public void paint (Graphics g) {
10
by: Roman Zeilinger | last post by:
Hi I have a beginner question concerning fscanf. First I had a text file which just contained some hex numbers: 0C100012 0C100012 ....
4
by: subramanian100in | last post by:
In the book, C++ Coding Standards book by Hereb Sutter and Andrei Alexandrescu, in Item 40 on pages 86-87 viz, "Avoid providing implicit conversions", the authors have advised the use of named...
2
by: Daniel | last post by:
Looking for a good resource for beginners? books, online anything that will get me started.. what im trying to focus on is building a small program in c# to look thru a few files in a folder...
3
by: Ben Keshet | last post by:
I have a probably simple beginner's question - I have a script that I am currently able to print its output. instead, i want to write it into a file - I tried different versions of write() but...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.