Hey guys so I looked through the basic input/output section of the tutorials and I came across a problem I need help with. My code below shows the basic input output module I made I am just wondering how I would get a users name to print out but include numbers and letters not just letters or just numbers?
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int InputNumber;
count<<"enter number";
cin>>InputNumber;
cout<<InputNumber;
return 0;
}
For example I want the output from the console to ask you to input the user name and that you can enter benny123 or tom872 rather than just an integer or string variable. Basically a mixed variable.
#include <iostream>
#include <string>
int main( void )
{
std::string screen_name;
std::cout << "Please enter your name: ";
std::cin >> screen_name; // user enters **7myname_123
std::cout << "hello " << screen_name << std::endl; // hello **7myname_123
return 0;
}
The reason this works is that numbers are characters too. Think of the fact that we can type numbers onto this textarea, the numbers aren't thought of as numbers.
As a twist, everything is actually a number:
1 2
char ch = '1';
std::cout << (int)ch << std::endl;
Maybe I confused you more. change line 8 to be string InputNumber; and your program will do what you want.
Thanks dude. So let's say I state that the user input has to be a number such as d.o.b or age. How would I relay an error message such as "must be a number" if they input a letter.