5 Replies - 1492 Views - Last Post: 07 October 2009 - 01:30 PM Rate Topic: -----

#1 Worr   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-October 09

While loop doesn't terminate

Post icon  Posted 07 October 2009 - 10:41 AM

I am writing a simple printing calculator that is suppose to accept input from a user in the following form: number operator
The calculation will be based on the letter the user inputs (e or E sets the number as the accumulator, t or T is suppose to terminate, a or A is addition, m or M is multiplication, etc.). The problem I am having is that it doesn't matter what number i put in it just prints out 0.00 and the loop never ends. The program should go through once do the calculation based off the operator and then wait for the users next input.

#include <stdio.h>
#include <math.h>

void main (){

	double accum;
	double number;
	char operator;

	scanf ("%lf%c", &number, &operator);
	while (operator != 't' && operator != 'T'){
		if (operator == 'e' || operator == 'E')
			printf ("= %0.2lf\n", accum = number);
		else if (operator == 'a' || operator == 'A')
			accum += number;
		else if (operator == 's' || operator == 'S')
			accum -= number;
		else if (operator == 'm' || operator == 'M')
			accum *= number;
		else if (operator == 'd' || operator == 'D')
			accum /= number;
		else if (operator == 'p' || operator == 'P'){
			accum = pow(accum, number);
		printf ("= %0.2lf\n", accum);}				
		else{		
			printf ("invalid command\n");
			scanf ("%lf%c", &number, &operator);}
		
   }
}


This post has been edited by Worr: 07 October 2009 - 01:31 PM


Is This A Good Question/Topic? 0
  • +

Replies To: While loop doesn't terminate

#2 Autocrat   User is offline

  • D.I.C Regular
  • member icon

Reputation: 4
  • View blog
  • Posts: 307
  • Joined: 27-September 09

Re: While loop doesn't terminate

Posted 07 October 2009 - 10:44 AM

Quote

else
printf ("= %0.2lf\n", accum);
scanf ("%lf%c", &number, &operator);
}


When you have more than one statements, you use braces, so this should be:

	else {
		printf ("= %0.2lf\n", accum);
		scanf ("%lf%c", &number, &operator);
	}
   }


This post has been edited by Autocrat: 07 October 2009 - 10:45 AM

Was This Post Helpful? 0
  • +
  • -

#3 taylorc8   User is offline

  • B&

Reputation: 150
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: While loop doesn't terminate

Posted 07 October 2009 - 10:51 AM

is operator a keyword ??

i would imagine you would get an error
Was This Post Helpful? 0
  • +
  • -

#4 Worr   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-October 09

Re: While loop doesn't terminate

Posted 07 October 2009 - 11:24 AM

View Posttaylorc8, on 7 Oct, 2009 - 09:51 AM, said:

is operator a keyword ??

i would imagine you would get an error

It compiles fine. Operator is a character the user inputs and each character represents the type of calculation the program should do. Sorry if this is not what you meant and also thanks for the quick responses guys. I fixed that problem Autocrat posted, but I'm still having the same issue which is the loop doesn't stop.
Was This Post Helpful? 0
  • +
  • -

#5 aks29921   User is offline

  • D.I.C Regular

Reputation: 116
  • View blog
  • Posts: 345
  • Joined: 24-August 09

Re: While loop doesn't terminate

Posted 07 October 2009 - 12:02 PM

use or instead of and in while statement
while (operator != 't' || operator != 'T')
Was This Post Helpful? 1
  • +
  • -

#6 Worr   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-October 09

Re: While loop doesn't terminate

Posted 07 October 2009 - 01:30 PM

Got it thanks.

This post has been edited by Worr: 07 October 2009 - 05:40 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1