printf my name

hey guys i want writing a code which print my first name and last name separately so I wrote sample code and it wont let me enter the last name, just the first name.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 #include <iostream>
#include <stdio.h>

main()
{
char a;
char b;
printf("first:");
scanf("%c",&a);
printf("last:");
scanf("%c",&b);
}

Does your names contain a single character? Because you are reading inly two characters.

correct way:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>

int main() //Implicit return type is illegal in C++
{
    std::string first; //Use C++ facilities in C++
    std::string last;
    std::cout << "first: ";
    std::cin >> first; //Protected from buffer overflows
    std::cout << "last: ";
    std::cin >> last;
}
And if you can't use strings you are going to need to make a char array.
Topic archived. No new replies allowed.