C/C programming - User Input issue
Expert: Joseph Moore - 7/2/2009
QuestionHello Joseph,
I am trying to write a simple program for inputtig a mailing address. I am new to the C programming language.
I have written the following code, but when it executes, it will only allow for me to input my first name, but not the rest. After I input my first name, all the other print commands are executed, and then the program terminates.
What I'd like is after I input my first name, for the program to then ask me to in put my last name, then adderss , and so on.
Here is my code:
/* User Input of mailing, using scanf, ANSI C, p.44*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
main()
{
double address_number,
zip_code;
char first_name,
last_name,
street_name,
city,
state;
/* ask user to input their mailing address, line by line */
printf("First Name: ");
scanf("%", &first_name);
printf("\n");
printf("Last Name: ");
scanf("", &last_name);
printf("\n");
printf("Address Number: ");
scanf("%", &address_number);
printf("\n");
printf("Street Name: ");
scanf("%", &street_name);
printf("\n");
printf("City: ");
scanf("%", &city);
printf("\n");
printf("State: ");
scanf("%", &state);
printf("\n");
printf("Zip Code: ");
scanf("%", &zip_code);
printf("\n");
return 0;
}
AnswerHi, Mike.
OK, so there are a few problems here with your code. First, you're using doubles as storage for street number and zip code. I suppose I could see a double being used for street number, since some people live at half addresses. Zip codes, though, are always integer values. That said, it still may be better to store them alphabetically, because you're never quite sure what a user is going to input.
Beyond that, you have single characters for the first name, last name, street name, city, and state. A char is literally just one character, so if someone input "John" then you would only store the J. You must make each of these character arrays so that you can store the input properly. You have to define the size of the array, too, which means you are limiting your input to a maximum size. Often times, you will see situations such as this us a single, large, temporary storage array which is then copied into the real location, after size is determined, something like this:
char tmpStorage[512];
printf("First Name: ");
scanf("%511s", &tmpStorage);
printf("\n");
first_name = malloc(sizeof(char) * (strlen(tmpStorage) + 1));
strcpy(first_name, tmpStorage, strlen(tmpStorage));
Also, when using scanf, you need to specify the storage type of the data being read. Notice in the above code the %511s. This specifies that it is a string (character array) with enough storage space for 512 characters (including the NULL terminator). Additionally, when reading a string, the first white space character encountered is considered to be the end of the string. That means something like, "de Soto" as a last name would actually just turn into "de" and the next requested input would automatically be filled with the rest of the input (so Address Number would try to get its value from Soto, and if that failed, it would fall to Street Name).
The scanf function can be a bit tricky to work with, and it's best to add a *lot* of error handling to it when you use it. You ought to verify that you've received the correct number of input parameters, perhaps loop until you've handled all of the input provided (or otherwise errored out).
Hopefully I have answered all of your questions regarding this issue. If I have not, feel free to send a followup question and I'll see what I can do to help you out further.