123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|208|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> C/C++/C#/Other -> complex if statements.

Mon, 11 Jul 2011, 14:37
jedimastersterli
need a little help.

the program i'm writing needs to take in user input differently depending on previous input. so say for example if user inputs characters a, e, i, o, or u the computer will ask user for a second input, but for characters b, d, p, or t it starts calculating immediately. something like this

string first;
string second;

cin >> first

if (first == a,e,i,o,u)
{
cin >> second
//code
cout << "something";
}
else if (first == d,t,b,p)
{
//code without second input
cout << "something else"
}

I have no idea what the correct syntax would be for these kinds of if statements, everything i try comes out as arbitrarily true or false, or a big error message. It would be really annoying to have to do a dozen if/else if statements especial if there are really only two options.
Mon, 11 Jul 2011, 14:40
jedimastersterli
Found one solution:

if ((first == "a")||(first == "e")||(first == "i")||(fist == "o")||(first == "u"))

it works, but still not as elegant as i'd like, shorter than five seperate if/else if statements though.
Mon, 11 Jul 2011, 14:48
shroom_monk
The solution you've got there is probably the best way of doing it. It's probably about as elegant as you'll get given what you're trying to do (although given that I was shown up the last time I said something, there's the off chance I could be wrong ).

-=-=-
A mushroom a day keeps the doctor away...

Keep It Simple, Shroom!
Mon, 11 Jul 2011, 15:09
JL235
My C is quite rusty, so I'd expect a few bugs in this code. But this is how I would approach the problem:

By using an array it allows you to have a single if, with a single check. You then just run through the possibilities within each for loop.

Not the most efficient method (although I also see now reason why my added bloat couldn't be optimized out), but it helps to separate out your concerns (which I see as a big win).
Mon, 11 Jul 2011, 17:25
CodersRule
I'm not a pro with C, but it seems you can use a switch statement, which looks a bit more elegant, but does not scale:


JL's code is probably better to use, because you can use pretty much an infinite amount of elements, and it will always be the same length. It would make things a lot easier to code.
Mon, 11 Jul 2011, 18:00
jedimastersterli
Thanks, I think I'll stick with the or clause thing for now.

another question though, can i use one entry with cin to write both an int and a string, so that i get an int 10 and a string "10". Or is there a way to turn a symbol into a number and back again. Both if possible.

I tried casting but it either gives a compiler error or turns the number into some weird symbol like (8706).
Mon, 11 Jul 2011, 18:27
HoboBen
@coders, again, single quotes for chars (e.g. case 'a'), otherwise you have to use strcmp.

@jedi

If you have a string, look up the "atoi" function to convert it to an integer -- the prototype is:

int atoi(const char *str);

It's called "atoi" because it converts an ascii string into an integer. "atof" is the equivalent for floating point numbers.

The easiest way to turn an integer or floating point number into a string is probably to use sprintf.

-=-=-
blog | work | code | more code
Mon, 11 Jul 2011, 21:36
jedimastersterli
hey check this out.

#include <sstream>

std::stringstream ss;

// Cast to float...
ss << some_string;
ss >> some_float;


// Cast to string...
ss >> some_float;
ss << some_string;

From:
https://www.velocityreviews.com/forums/t288976-convert-a-string-to-float-and-vice-versa.html

it works, and i didn't need to change the string to a char* either. Anyone know what it's doing though?
Tue, 12 Jul 2011, 12:24
HoboBen
Ah, C++ magic that I don't know too much about.

Normally, the << and >> operators perform a left or right bitshift on two integers.

C++ has a string type, and the << and >> bitwise operators have been overloaded in this case to convert between this string type and other types.

I think it introduces a lot of ambiguity when reading code, so I avoid it in favour of C. If you want to write C++ though, as opposed to C++ that looks like C, you'll want to use the C++ standard libraries such as sstream.


-=-=-
blog | work | code | more code
Tue, 12 Jul 2011, 15:40
Evil Roy Ferguso
jedimastersterling: that is using a stringstream, and it is the correct way of doing it in "plain" C++. Strings don't ordinarily allow the stream operators; a stringstream lets you write to / read from a string using the same operators you'd use with any other stream (like cout). Here you're writing the string to the stream, so you can read it back just like you would with cin >> myInt does.

It's a little convoluted, admittedly -- if you're using Boost (which you absolutely should, once you get more familiar with C++), you can just use lexical_cast like so:

This is what I would personally go with.

atoi is technically deprecated, even in C (in favor of strtol), so it should probably be avoided.