473,805 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3486
In message <Sk************ ******@bignews6 .bellsouth.net> , Sonoman
<fc*****@cse.fa u.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.fa u.edu> wrote in message
news:Sk******** **********@bign ews6.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.fa u.edu> wrote in message
news:Sk******** **********@bign ews6.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.merke rk(at)dse.nl

Jul 19 '05 #5
Bryce <Br**********@s ZoEhRoOcSoPdAeM .com> writes:
In message <Sk************ ******@bignews6 .bellsouth.net> , Sonoman
<fc*****@cse.fa u.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.fa u.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*******@pres by.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.fa u.edu> wrote in message
news:Sk******** **********@bign ews6.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
4293
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 must be many know the answer here. thanks
8
2012
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 msg that im getting: "Error executing cl.exe" this is the code that I have, but I know whats causing it, ill just show you the whole thing first though //file: Quadratic
5
11410
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 string like this: char szString = "Hello";
1
2628
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 experimenting a bit with what I need to do this and have the following code snippet. But first if I pass the assembly name and type to Activator.CreateInstance() it always fails. However if I walk my assembly and get a type value, the call to...
14
2295
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 this string "1,2,3,4,..." Into "...4,3,2,1" This one seems hard enough let alone trying to turn a string of space-seperated words around(is that even possible? a trick question
5
4792
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
4473
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
2026
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 functions that offer conversions instead of conversion operators. In page 87, example 2: Errors that work. class String { // ...
2
201
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 that contain certain words / messages ( i know that is probably more than a beginner can do right off the bat ) but i have other programmers available that can help, but rather try myself before i get them involved.. example of what i mean
3
2047
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 might have gotten the syntax wrong. the variable I want to write is a line from a file I am reading: "... f = open('receptor.mol2', 'r') line = f.readline()
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10607
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10359
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10104
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9182
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4317
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3843
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.