You are here:

C/C programming - User Input issue

Advertisement


Question
Hello 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;

}


Answer
Hi, 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.

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joseph Moore

Expertise

I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.

Experience

I've been writing C code for 12 years, both on my own in my spare time and professionally.

Organizations
IGDA

Education/Credentials
Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2012 About.com, a part of The New York Times Company. All rights reserved.