You are here:

C++/C question

Advertisement


Question

I am trying to write a simple program for inputting 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
Dear mike:

Here is some information on scanf:
http://www.cplusplus.com/reference/clibrary/cstdio/scanf/

I do not know if this will fix your problem, but I can give you some corrections on your code:

printf("First Name: ");
         scanf("%s", first_name);
         printf("n");

printf("Last Name: ");
         scanf("%s", last_name);
         printf("n");

printf("Address Number: ");
         scanf("%s", address_number);
         printf("n");

printf("Street Name: ");
         scanf("%s", street_name);
         printf("n");

printf("City: ");
         scanf("%s", city);
         printf("n");

printf("State: ");
         scanf("%s", state);
         printf("n");

printf("Zip Code: ");
         scanf("%s", zip_code);
         printf("n");  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Titus B. Ledbetter, Jr.

Expertise

C , MFC, Object Oriented, Artificial Intelligence

Experience

I have over ten years experience in the field of Computer Science, five years experience developing C/C++. I recently wrote a chess program using Object Oriented, C++, MFC.

Education/Credentials
Master's Degree Computer Science from Johns Hopkins

©2016 About.com. All rights reserved.