hey guys, new to daniweb here.
This is my first assignment in my first C class at school.
I'm having some trouble getting the program to work, was wondering if you folks could give me some tips. I believe I have most of the code down, I think my issue is structure/placement.

The assignment is:

Write a program that can serve as a simple calculator. This calculator keeps track of a single number (of type double) that is called result and that starts out as 0.0. Each cycle allows the user to repeatedly add, subtract, multiply, or divide by a second number. The result of one of these operations becomes the new value of result. The calculation ends when the user enters the letter R for “result” (either in uppercase or lowercase). The user is allowed to do another calculation from the beginning as often as he or she wants. Use the scanf for input.

The input format is shown in the following sample dialog. If the user enters any operator symbol other than +, −, *, or /, then display message “UnknownOperatorException is thrown “ and the user is asked to reenter that line of input..

Calculator is on.
result = 0.0
+5
result + 5.0 = 5.0
result = 5.0
*2.2
result * 2.2 = 11.0
result = 11.0
% 10
% is an unknown operation
Reenter, your last line:
* 0.1
result * 0.1 = 1.1
result = 1.1
r
Final result = 1.1
Again? (y/n)
y

and here is what I have so far:

#include <stdio.h>

int main()
{
	float result, number2;
	int x;
	char operation,choice,r,R;
	result = 0.0;

	printf("the calculator has started.\n");
	do{
	printf ("result is %f\n",&result);
	scanf ("%c%f\n",&operation,&number2);
	if (operation == '+')
		printf ("%f\n", result+number2);
	if (operation == '-')
		printf ("%f\n", result-number2);
	if (operation == '*')
		printf ("%f\n", result*number2);
	if (operation == '/')
		printf ("%f\n", result/number2);
	if (operation == 'r'||'R')
		printf ("final result: %f\n",result);
		printf ("do you wish to continue y/n?\n");
		scanf ("%s", &choice);
		if (choice =='n')
		{break;}
		
	} while (1);
	
    return 0;

I know I'm supposed to declare a type double, but I can't seem to get any results at all when I use double. I know you guys don't do people's homework for each other, and this website has helped me a ton already. I appreciate any help I can get. Thank you.

Edited 5 Years Ago by Narue: Added code tags

First of all ,put your code between the code tags (see that "

" thing just above your message editor box), it'll make easier to read for all of us and even you.

[code]

#include <stdio.h>
int main(void)
{
//your code
return 0;
}

use while instead of

do while

and then see how things go.
you never ask for the "choice".

if (choice =='n')
{break;}

Where did the 'n' come from.That you just wrote is a 'infinite loop' since "choice" is never equal to 'n'.
You never ask for the numbers to perform operations on , how will the user know what the input should be?

Edited 5 Years Ago by D33wakar: n/a

oh ok I apologize. I was wondering how others were displaying code in that way.
I've been at this for about two days and can't seem to figure it out.
I forgot to mention that I had some code written for the input of lower and uppercase r but it errors would fly everywhere no matter how hard I tried to fit it into the code. Pretty much, this is as close as I've got it so far.

I'll be going to bed as it is over midnight in my time, but I will be posting my progress, if any, throughout the day tomorrow. Please help a newbie out! :)

sorry I was wrong about the infinite loop thing. It seems that you did ask for the "choice"(was hard to read without formatting tags).
This should work.

#include <stdio.h>

int main()
{
float result, number2;
int x;
char operation,choice,r,R;
result = 0.0;

printf("the calculator has started.\n");
while (1){
	printf ("result is %lf\n",result);/* the format specifier for the double type is %lf*/
        fflush(stdin);
	scanf ("%c%f",&operation,&number2);

if (operation == '+')
	printf ("%f\n", result+=number2);// result= result + number2
if (operation == '-')
	printf ("%f\n", result-=number2);
if (operation == '*')
	printf ("%f\n", result*=number2);//result = result * number2
if (operation == '/')
	printf ("%f\n", result/=number2);//result = result / number2
if (operation == 'r'||'R')
	printf ("final result: %f\n",result);

printf ("do you wish to continue y/n?\n");
scanf ("%s", &choice);
if (choice =='n')
	break;

}

return 0;
}

Edited 5 Years Ago by D33wakar: n/a

I think

fflush(stdin);

will result an undefined behavior so you better use getchar() after the last scanf()(see line 28 in the above code).

...
scanf("%s",&choice);
getchar();
...

diwarak wagle, what do you mean by while(1) in your program

It is same as writing while(true) It causes the loop to execute infinetly or unless the break; statement is encountered(as you can see in the above code) .

Edited 5 Years Ago by D33wakar: n/a

This article has been dead for over six months. Start a new discussion instead.