5 Replies - 256 Views - Last Post: 08 February 2012 - 09:33 AM Rate Topic: -----

#1 mr_mohamed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-February 12

problem while loop function

Posted 08 February 2012 - 09:08 AM

Hi all,
I am trying to run a program using this while loop function. It's just a simple code with the loop function.But the problem is, the first part is running correctly the second part is not responding correctly. It's just calculating a purchase by entering price & the quantity and it should give us the total of those purchase by multiplying price with the quantity after that it supposed to as for another purchase, and the problem starting with this part. it's not really asking for the second purchase it just jumped to the last part.
Since I'm new to this type of community your help will be a great thing for me. (I'm learning C myself)


#include <stdio.h>
int main(void)
{
	float price;
	short quantity;
	char answer;

	printf("So you wish to enter a purchase?(Y/N)? ");
	scanf("%c", &answer);
	while(answer== 'Y' || answer== 'y')
	{
		printf("Enter 'price & quantity': ");
		scanf("%f %hi", &price, &quantity);
		printf("The total for this item is $%6.2f.\n", price * quantity);
		while(answer == 'Y' || answer == 'y')
		printf("Another purchase(Y/N)? "); //problem with this part
		scanf("%c", &answer);
	}

	printf("\nThank you for your patronage.\n");
	getchar();
	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: problem while loop function

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: problem while loop function

Posted 08 February 2012 - 09:12 AM

You don't want a second while statement inside that while loop. Although it is OK to have loops embedded inside other loops you don't need that here. It looks like it should work if you just delete line 15.

edit: oops, sorry. It needs another change.

You need to add an additional line:
getchar();
immediately after each scanf statement and then it will work. When you use scanf for input from the keyboard, a newline character is produced when the user presses enter. scanf leaves that newline character in the input stream, so you need getchar() to extract (and discard) that newline. Otherwise, the next time around, scanf will see that same newline again and treat it as if the user just pressed enter by itself, and therefore there's no 'y' or 'Y' and it drops out of the loop.

This post has been edited by r.stiltskin: 08 February 2012 - 09:21 AM

Was This Post Helpful? 1
  • +
  • -

#3 mr_mohamed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-February 12

Re: problem while loop function

Posted 08 February 2012 - 09:18 AM

View Postr.stiltskin, on 08 February 2012 - 09:12 AM, said:

You don't want a second while statement inside that while loop. Although it is OK to have loops embedded inside other loops you don't need that here. It looks like it should work if you just delete line 15.

I am still having the same problem after deleting the line no. 15 .
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: problem while loop function

Posted 08 February 2012 - 09:23 AM

Sorry, look at post #2 again. I added some additional information.
Was This Post Helpful? 0
  • +
  • -

#5 mr_mohamed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-February 12

Re: problem while loop function

Posted 08 February 2012 - 09:28 AM

thanks you so much @r.stiltskin . The problem is solved. :)
Was This Post Helpful? 0
  • +
  • -

#6 shurd  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 162
  • Joined: 05-February 12

Re: problem while loop function

Posted 08 February 2012 - 09:33 AM

Just need a getchar(); after line 13 and before 14 to take the '\n' from your enter key.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1