Basic input output help?

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>
  using namespace std;

  int main()
{

  int InputNumber;
  count<<"enter number";
  cin>>InputNumber;
  cout<<InputNumber;

  return 0;
}
Not exactly sure what you are asking. Tell us what you want to type and what you want the result to be.
I want the input name to be a user name for example benny123 as it includes both numbers and letters.
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.
A string will be able to handle that:
1
2
3
4
5
6
7
8
9
10
11
#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.
I understand everything you say thank you very much. Just one thing why use

int main(void)

Why a void here is it a no return value?
The void states that the function takes no arguments, I put it there, but one does not have to.

Programs can take command line arguments, so you might see something like this:
1
2
3
4
5
6
7
#include <iostream>

int main( int argc, char* argv[] )
{
  for ( int i = 0; i < argc; ++i ) std::cout << argv[i] << '\n';
  return 0;
}


From the command line:
1
2
3
4
5
6
$ g++ main.cpp -o my_program
$ my_program these are command line arguments
my_program
these
are
command
line
arguments


Adding the void makes a difference in C. The following does not produce a compile error:
1
2
3
4
5
6
7
8
9
int func()
{
  return 1;
}

int main( void )
{
  func( "hello" );
}


But this would:
1
2
3
4
5
6
7
8
9
int func( void )
{
  return 1;
}

int main( void )
{
  func( "hello" );
}
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.
Topic archived. No new replies allowed.