Hi everyone,

I'm new to the forums and having a problem that I can't seem to fix, so it would be great if someone could help me out!

Everything in the following code works except for the while loop. When it gets to the "Input another product? (y/n) :" line, no matter what letter I enter it will not loop back to the beginning. It just displays the next part with the grand totals. Please help! Thanks!

/* ================================================================== */
/* >  Program:      intmenu2.c                                      < */
/* >                                                                < */
/* >  Description:  printf, scanf, loop                             < */
/* >                                                                < */
/* >     Name           Date      Comment                           < */
/* >     -------------  --------  ------------------------------    < */
/* >                       2/07/08   Author                           < */
/* ================================================================== */

#include <stdio.h>
#include <stdlib.h>

int main()

{

int prodnum, prodtype, prodquan, count = 0;
double cost = 0, price = 0, total = 0;
double prod_cost = 0, prod_price = 0, prod_profit = 0;
double total_price = 0, total_cost = 0, total_profit = 0;
char choice;

system("cls");
do
{
printf("Sierra Sporting Goods\n\n");

printf("Enter the product number: ");
scanf("%d%*c", &prodnum);
printf("Enter the product type: ");
scanf("%d%*c", &prodtype);
printf("Enter the quantity: ");
scanf("%d%*c", &prodquan);

count++;

printf("Enter the unit cost: ");
scanf("%lf", &cost);
printf("Enter the unit price: ");
scanf("%lf", &price);

	prod_price = price * prodquan;
	prod_cost = cost * prodquan;
	prod_profit = (price - cost) * prodquan;

	total_price += prod_price;
	total_cost += prod_cost;
	total_profit += prod_profit;

printf("\n\n\n");
printf("Sierra Sporting Goods\n\n");

printf("The product number is ---->  %04d\n", prodnum);
printf("The product type is ------>  %d\n", prodtype);
printf("The quantity is ---------->  %d\n", prodquan);
printf("The cost is -------------->  %.2lf\n", cost);
printf("The price is ------------->  %.2lf\n\n", price);

printf("Total product price ------> %.2lf\n", prod_price);
printf("Total product cost -------> %.2lf\n", prod_cost);
printf("Total product profit -----> %.2lf\n\n", prod_profit);

printf("Input another product? (Y/N): ");
	scanf("%c%*c", &choice);
	}
while (choice == 'y' || choice == 'Y');

printf("\n\n");
printf("Total count of products --> %d\n", count);
printf("Total all prices ---------> %.2lf\n", total_price);
printf("Total all costs ----------> %.2lf\n", total_cost);
printf("Total profit -------------> %.2lf\n", total_profit);
printf("\n\n");


return 0;
}

Recommended Answers

All 10 Replies

The same newline issue is left after the input of the double. It then immediately satisfies the subsequent call to scanf in which you type y .

That %*c is nonstandard. I suspect that you are trying to get rid of the newline.

Instead, get rid of the newline with: scanf( "%d\n", &my_int ); Hope this helps.


BTW, this is the C++ forum.

Huh? Sorry I'm just learning all of this and I have no idea what that means!

I know this is the C++ forum... That's the class I'm doing this assignment for...

That %*c is nonstandard. I suspect that you are trying to get rid of the newline.

Nonstandard?!?

Replace every occurrence of %*c with

\n
[inlinecode]
, and down at lines 39 and 41 change it to

[inlinecode]
scanf( "%lf\n", &cost );

and

scanf( "%lf\n", &price );/inlinecode]

Every time you ask the user for something you can expect him to press ENTER. You need to get rid of that enter key from the input each time you read something. That's what the "\n" does.

Hope this helps.

[EDIT] > Nonstandard!?!?!?

Yoinks! You're right! It is standard.

[EDIT #2] If you know this is the C++ forum, why are you posting C code here?[inlinecode]
scanf( "%lf\n", &price );/inlinecode]

Every time you ask the user for something you can expect him to press ENTER. You need to get rid of that enter key from the input each time you read something. That's what the "\n" does.

Hope this helps.

[EDIT]
> Nonstandard!?!?!?


Yoinks! You're right! It is standard.


[EDIT #2]
If you know this is the C++ forum, why are you posting C code here?

I did that and it still doesn't work.... Now when it asks for product cost it just sits there after I enter a number, until I enter another one, and then it asks for the price. AGH!

Wow, I learned something.

I've always understood literals in scanf() to be read, matched, and discarded. Apparently that doesn't count for whitespace characters...

Ok I got it! You had the right idea, it just needed to be: scanf( "%lf%*c", &cost ); scanf( "%lf%*c", &price ); Thanks a lot for the quick help, you guys! I'll be on here with more questions later on today!

So here is the final working code... Woo Hoo!

/* ================================================================== */
/* >  Program:      intmenu2.c                                      < */
/* >                                                                < */
/* >  Description:  printf, scanf, loop                             < */
/* >                                                                < */
/* >     Name           Date      Comment                           < */
/* >     -------------  --------  ------------------------------    < */
/* >     Janelle Wood    2/07/08   Author                           < */
/* ================================================================== */

#include <stdio.h>
#include <stdlib.h>

int main()

{

int prodnum = 0, prodtype = 0, prodquan = 0, count = 0;
double cost = 0, price = 0, total = 0;
double prod_cost = 0, prod_price = 0, prod_profit = 0;
double total_price = 0, total_cost = 0, total_profit = 0;
char choice;

system("cls");
do
{
printf("Sierra Sporting Goods\n\n");

printf("Enter the product number: ");
scanf("%d%*c", &prodnum);
printf("Enter the product type: ");
scanf("%d%*c", &prodtype);
printf("Enter the quantity: ");
scanf("%d%*c", &prodquan);

count++;

printf("Enter the unit cost: ");
scanf("%lf%*c", &cost);
printf("Enter the unit price: ");
scanf("%lf%*c", &price);

	prod_price = price * prodquan;
	prod_cost = cost * prodquan;
	prod_profit = (price - cost) * prodquan;

	total_price += prod_price;
	total_cost += prod_cost;
	total_profit += prod_profit;

printf("\n\n\nSierra Sporting Goods\n\n");

printf("The product number is ---->  %04d\n", prodnum);
printf("The product type is ------>  %d\n", prodtype);
printf("The quantity is ---------->  %d\n", prodquan);
printf("The cost is -------------->  %.2lf\n", cost);
printf("The price is ------------->  %.2lf\n\n", price);

printf("Total product price ------> %.2lf\n", prod_price);
printf("Total product cost -------> %.2lf\n", prod_cost);
printf("Total product profit -----> %.2lf\n\n", prod_profit);

printf("Input another product? (Y/N): ");
	scanf("%c%*c", &choice);
	}
while (choice == 'y' || choice == 'Y');

printf("\n\nTotal count of products --> %d\n", count);
printf("Total all prices ---------> %.2lf\n", total_price);
printf("Total all costs ----------> %.2lf\n", total_cost);
printf("Total profit -------------> %.2lf\n\n\n", total_profit);


return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.