I dont even know how to word this but... I am trying to make
the program recognize a char and convert it to an int. I want to make the user decide whether the dog is tall or short and depending on the user input it would give cat an int value. What I had in mind for example was, if dog equals Tall or tall then cat equals 1. If dog equals Short or short then cat equals 2. How would I incorporate that in to the code below?
1 2
if (dog == 't' || dog == 'T')
if (dog == 's' || dog == 'S')
You know a char is just a single character right? e.g. 'a' or 'j' or '&'. If you really want the user to put in an entire word, you will need to use strings. It is done like this:
#include <string> //Include the string library.
using std::string;
using std::cin;
using std::cout;
int main()
{
string s = ""; //Declare a string s.
cout << "Is the dog tall or short?\n";
cin >> s; //Get word from user and store it in string s.
if(s == "Tall") //Strings go inside double quotes.
{
dogsize = 2;
}
elseif(s == "short")
{
dogsize = 1;
}
return 0;
}
However, none of the code you posted specifies what the type of anything is (with the exception of short, which is a built-in type).
It looks like you are referring to character strings? In which case they should be quoted, "tall" or maybe tall is the name of a variable? And what is dog, a variable of some type, or a string?
Thanks guys, I caught the mistake I did because I meant to enter in 't' or 'T' and 's' or 'S' for tall and short char when I typed my question out. It answered my question tho, thanks again!