bipolar variable

I need help figuring out how to use one variable as both an int and a char. The program I'm creating takes a number for an input and then outputs a bunch of data about the number like the decimal and hex values and if it overloads certain data types.

It keeps looping throught the process until the user enters "q".

I need to find out how to make the program recognize "q" as a letter and not a number or whatever it needs to do to make the program quit. I've read up on cin.fail() and I think it could help me but I still don't understand how to use it.
Forget about cin.fail.
Just read a string and then convert it to a number:
1
2
3
4
string input;
cin >> input;
if (input=="q")return 0;
int num=lexical_cast<int>(input);


lexical_cast is part of boost.
How about a solution that doesn't require a newbie to use a library?
boost is quasi-standard.
It would be possible to create a simple lexical_cast implementation using standard stringstreams, but the practicality of that is questionable, considering boost already offers a superior alternative.
Yes, but it has to be manually linked to the program (in some cases), and that's assuming it's even installed. All this just to convert a single string to a number is a bit silly, and would only confuse someone who isn't very experienced in the language or the build system.
This isn't as nice as Boost, but it's a lot easier for a newbie to use:
1
2
3
#include <cstdlib>
//...
int num=atoi(input.c_str());
The majority of boost libraries does not require linking and of course that includes lexical_cast.
Installation of the boost library is also far easier than normal libraries, as you just have to extract the boost folder to your include folder (unless you want to use libraries that aren't header-only).
Since the standard library is rather lacking, boost makes many things easier, especially for newbies who might have trouble implementing a particular functionality on their own.
Thanks for getting back so quickly. First off, to Athar, what does lexical_cast even do? Wouldn't line one solve my problem by itself? And to helios, on your line 3, does it have to be a c-string, and can I switch the atoi to itoa?
does it have to be a c-string
The code is partly based on Athar's, so input is an std::string. The call to c_str() gives back a pointer that can be safely passed to atoi().
More completely:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
#include <cstdlib>
//...
std::string input;
std::getline(std::cin,input);
if (input=="q"){
    //do something
}
int num=atoi(input.c_str());


can I switch the atoi to itoa?
Well, you probably can, but it won't do what you're trying to do, since itoa() converts an integer to a string.

Now that I take a better look at it, Athar: what's the point of using boost::lexical_cast if you're just going to use the operator>>() overload anyway? Why not just take input into an integer directly? It would have made more sense if you were using std::getline().
Last edited on
Ishvite wrote:
First off, to Athar, what does lexical_cast even do?

Here, it converts the string to an integer (it also works the other way round).

Ishvite wrote:
Wouldn't line one solve my problem by itself?

Not if you want to actually do something with the number (like performing calculations on it).
You can't do that while it's still a string.

helios wrote:
Now that I take a better look at it, Athar: what's the point of using boost::lexical_cast if you're just going to use the operator>>() overload anyway? Why not just take input into an integer directly?

Because the input is supposed to be either a control word such as "q" or a number.

helios wrote:
It would have made more sense if you were using std::getline().

There's not much of a difference, except that operator>> stops at the first whitespace.

helios wrote:
Well, you probably can, but it won't do what you're trying to do, since itoa() converts an integer to a string.

Besides, unlike atoi, itoa isn't portable.
Thanks for all the help, I figured it out.
Topic archived. No new replies allowed.