So I am trying to learn the basics of C++ before the gameduino comes out. I want to just learn the basics. Anyways I was making a code that just asked you for a number, you typed one in, then it says You typed in (insert number)!
So I hae the code all set and it works fine. I wanted to swap it to see if I could change it to letters and theres where the problem is. I am not sure what I am doing wrong but when the code is working it will only accept numbers. This is the original code.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include <iostream>
using namespace std;
int main (void)
{
int number;
cout <<"Please enter a number" <<endl;
cin >> number;
cout <<"You typed in " << number << "!" << endl;
return 0;
}
|
That code itself runs flawlessly. I tried to change it to this though.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include <iostream>
using namespace std;
int main (void)
{
int cha;
cout <<"Please enter a letter" <<endl;
cin >> cha;
cout <<"You typed in " << cha << "!" << endl;
return 0;
}
|
So if I type in a letter it just returns with a 0. If I type in 5 it will return with a 5. I was using the C++ basics guide in part 2 and read this:
VARIABLE TYPES:
1. Boolean (bool): This keyword is generally used for variables that are equal to true or false in your program.
2. Character (char): This keyword is used for characters. It can only hold one character such as an "A".
3. Integer (int): This keyword is used for numbers like 5,455, and so on. There are some regulations (which I will explain in the next tutorial), but for now just know the definition and context of when to use it.
So I thought I needed to change the variable type. Am I changing it in the wrong place or?