I'm having a problem understanding how to read in a string to a structure member array. I have a structure called 'customer' and a member called 'char last_name[20]'. I prompt the user to enter in his last name and that last name is to be stored in the 'last_name[20]' variable. The condition is that I have to use a do...while loop.
Here's the code:
void get_customer_info(struct customer *p_customer_start, int customer_num)
{
struct customer *p_customer;
for (p_customer = p_customer_start; (p_customer - p_customer_start) <
customer_num; p_customer++)
{
printf("\nCustomer number %d: ", (p_customer - p_customer_start) + 1);
while (getchar() != NEW_LINE);
printf("\n Enter the customer's last name: ");
// *THIS PART IS THE PROBLEM*
do
{
p_customer->last_name = getchar();
p_customer->last_name++;
} while (*p_customer->last_name != NEW_LINE);
}
return;
}
Problem is, with that algorithm last_name[0] does not get checked, it moves to 'last_name[1]' before it gets checked for a new line. And yes, a do...while construct must be used (this is for a class).
I appreciate anyone's thoughts.