so I'm a super noob at the moment and am trying to better my knowledge with simple practice programs. I want to be able to ask the user for an input which mostly will be an int. but I also want to say something like, if the user's input is equal to "END" then return 0; to terminate the program. But I cant get it to work because it just will end the program because the input was a string and not an int... Long story short is there a way to tell the computer that: X can be a string or an Int.
I have been searching the website for a while now looking for a solution, any help is appreciated.
sort of.
you can read a string and convert it to an integer if it did not == "END" for this purpose.
conversion of strings to numbers is expensive, but its the accepted way to control user input for error handling purposes, so you would pay this anyway.
looks sort of like this (you may need to check the stoi parameters, I think it needs a little more than this, but you can see the idea here).
string s;
cin >> s;
if (s== "END")
return 0;
int i = stoi(s);
cout << i << endl;
there are other ways to hijack a variable and store other types of data into them, most of this is bad practice, unions can do it, and pointer magic can do it.
for example (this may be a little advanced, and its also usually a BAD idea).
char imanint[100];
int *ip = (int*)imanint;
ip[0] = 12345;
basically we repurpoted the raw bytes of the char array and stuffed the raw bytes of an int into them. This is sometimes handy for things like sending tcpip type messages, but there are cleaner ways to do it.
in general c++ is strongly typed, meaning that no, you can't really put 2 types in one variable. Union and variant (old and modern versions of the same idea) are an interface around the issue but you should not over-use these.
Thank you both! It looks to me like there has to be an easier way to get my desired outcome? lets say I want a program that repeats my input:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
usingnamespace std;
void NUM();
int main ()
{
NUM();
return 0;
}
void NUM()
{
int x;
cin >> x;
cout << x << " is what you wrote" << endl;
NUM();
}
Now obviously this will go on forever unless I close the program. what I want is to be able to tell it to end (with user input) rather than just closing it. How would you recommend I do this?
recursion requires a terminal case.
for example, you could say
if(x != -1) //this is called a sentinel value, you pick a number you don't normally want as an exit condition.
NUM();
or, more of the same..
get a string instead of x,
check its value against a quit word
if its not a quit word, convert it.
here, because its cout, you don't NEED to convert it back to an integer, the user can't tell a text number from a numeric value, but if you needed to cout x*3 or something, you would need convert it. If that makes sense?
do you want it as a string, or is a sentinel sufficient?
I suppose a sentinel could work just fine. I did however just realize that I can just define x as a string and then it would work the way I want it to. As long math isn't involved in the program then I should be fine.
By coincidence, your last snippet of code avoided this issue, but please note that if you have multiple statements of code within an if-block, they need to be wrapped in { }. Whitespace (tabs) don't matter (syntax-wise!).
1 2 3 4
if (a)
hello;
nap_time();
are_you_still_there();
is the same as
1 2 3 4
if (a)
hello();
nap_time();
are_you_still_there();
The fix is to do:
1 2 3 4 5
if (a) {
hello();
nap_time();
}
are_you_still_there();