Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I enter the code below and try to enter my name then rest of the codes executive themselves without allowing me to enter any data but when I enter a single character for name then I can input the data one by one. Why is that ?

C++
#include <stdio.h>

int main() {
	
	char name;
	int age;
        char gen;
	
	printf("What is your name ? \n");
	scanf(" %c", &name);
	
	printf("What is your age ? \n");
	scanf(" %i", &age);
	
	printf("What is your gender ? \n");
	scanf(" %c", &gen);
	
	printf("Thank you for the information! :)");
	return 0;
}


What I have tried:

I have tried using "char name[10];" thinking it might be the storage problem, I have also changed "10" to "1000" but it doesn't seem to work. Whenever I input more than 1 character, the program executes the rest of the code itself.
Posted
Updated 9-Jan-18 21:15pm
v2

%c is the format specifier for a single character, which is what you used. %s is the format specifier for a text string and it is the one you should be using for the name.
 
Share this answer
 
v2
Comments
CPallini 10-Jan-18 3:09am    
5.
Quote:
I have tried using "char name[10];" thinking it might be the storage problem,
There is also a storage problem. In C programming language, char data type represents a single character. On the other hand, strings (e.g. names) are (0 terminated) array of characters. See, for instance, C Programming Strings[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900