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. 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
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
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.
> 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
Bryce <Br**********@s ZoEhRoOcSoPdAeM .com> writes: In message <Sk************ ******@bignews6 .bellsouth.net> , Sonoman <fc*****@cse.fa u.edu> writesI 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 mea 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
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
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.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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
|
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";
|
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...
|
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
| |
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)
{
|
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
....
|
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
{
// ...
|
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
|
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()
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |