Hi all. I have a problem with my calculator.

#include <stdio.h>

int a, b; /* value entered by user */
char c, choice;

int main()
{
    printf("\nThis program is a calculator.\n");
    printf("Enter your calculation: ");
    scanf("%d%c%d", &a,&c,&b);

    if  (c=='+')
        printf("The answer is %d", a+b);
    if (c=='-')
        printf("The answer is %d", a-b);
    if (c=='*')
        printf("The answer is %d", a*b);
    if (c=='/')
        printf("The answer is %d", a/b);

    printf("\n\nDo you want to continue? [Y/N]\n\n");
    scanf("%s", &choice);
    if(choice=='y'||choice=='Y');
        main();
        return 0;
}

I am having a problem with ending the program. When I enter a key other than Y, it loops back to the start of the program. How do I exit it?

Thanks in advanced.

Edited 3 Years Ago by pyTony: fixed formatting

Please use code tags [code] /*code goes here*/ [/code]

You shouldn't call main() like that, but the real problem is the semicolon at the end of your if statement if (choice=='y' etc) . If the if statement is true, the then "statement" is ; and then the program continues with the next line.

Read a little ahead in your book to the do/while statement section and replace your call to main() with that.

One final thing, when dividing 2 integers you're going to lose the decimal information. Also, try putting in a fraction like 5/7 and see what happens.

Edited 5 Years Ago by jonsca: n/a

as jonsca

your if condition is not effected because of semicolon at the end of the line
your code is

if(choice=='y'||choice=='Y');

remove the semicolon

if(choice=='y'||choice=='Y')

your code will work as you wish

Edited 5 Years Ago by prvnkmr194: n/a

Hello everyone. Thanks for the replies. I've made a couple of adjustments to my coding and it is working as intended now.

#include <stdio.h>

float a, b; /* value entered by user */
char c, choice; /* operator and continue decision */

int main()
{
	printf("\nThis program is a calculator.\n");
	printf("Enter your calculation: ");
	scanf("%f%c%f", &a,&c,&b);

	if  (c=='+')
		printf("The answer is %f", a+b);
	if (c=='-')
		printf("The answer is %f", a-b);
	if (c=='*')
		printf("The answer is %f", a*b);
	if (c=='/')
		printf("The answer is %f", a/b);

	printf("\n\nDo you want to continue? [Y/N]\n\n");
	scanf("%s", &choice);
	if(choice=='y'||choice=='Y')
		main();
	else
	printf("\nGood bye!");
	return 0;
}

I don't really understand what you're saying though jonsca regarding the do/while loop. How is it suppose to work in this case and what is the disadvantage of me calling the main(); like that? I'm sorry but my book has very limited information on do/while as it states that it is not frequently used in C.

It wouldn't change much of your code at all to put in the do/while loop

do {
/*all of your code from 8 to 22 */
} while(choice == 'y' || choice == 'Y');

then eliminate 23-25.

Calling main like you did is technically recursion. It's not forbidden, but be aware that each time main() would be called from itself, the "state" of each call to main would be conserved on the stack as another instance of it was called. Those representations (address/instruction to return to, and I think local variables, but I'm not sure) could pile up ad infinitum or potentially until it crashed your program.

Looking something like (but don't quote me on the specifics -- take a look at http://en.wikipedia.org/wiki/Call_stack for a good overall explanation):

[n-th call to main()]
   .... 
[3rd call to main()]
[2nd call to main()]
[1st call to main()]

I read up a bit on it, and it seems how you had it set up was a "tail" recursion, which the compiler can optimize away if possible. Doubtless someone knows whether or not this type of thing is allowable under the different standards (C89. C99, etc.), but I wasn't able to find any clear cut statement on that. I do know it's non-standard in C++, but that has no bearing on C per se.

The bottom line is that a simple do/while avoids all of this potential headache. I am not sure about your book's statement about its prevalence in C.

Edited 5 Years Ago by jonsca: dug myself a hole here

Ah I get it now. Thanks for all the help and explanation guys especially jonsca. Take care =)

This is how my coding looks like now.

#include <stdio.h>

float a, b; /* value entered by user */
char c, choice; /* operator and continue decision */

int main()
{
	do {
	printf("\nThis program is a calculator.\n");
	printf("Enter your calculation: ");
	scanf("%f%c%f", &a,&c,&b);

	if  (c=='+')
		printf("The answer is %5.2f", a+b);
	if (c=='-')
		printf("The answer is %5.2f", a-b);
	if (c=='*')
		printf("The answer is %5.2f", a*b);
	if (c=='/')
		printf("The answer is %5.2f", a/b);

	printf("\n\nDo you want to continue? [Y/N]\n\n");
	scanf("%s", &choice);
	}while(choice=='y'||choice=='Y');

	printf("\nGood bye!");
	return 0;
}

Sorry for the double post but I have no idea how to edit my post earlier. I found another problem with my program.

The program ends itself if I enter perhaps a character rather than a float into the statement below.

printf("Enter your calculation: ");
	scanf("%f%c%f", &a,&c,&b);

How do I make it to display "Invalid input" and loop it back to the start?

scanf has a return value which will tell you how many items were successfully converted (it returns EOF if no input is given). Use that in the condition of a small loop around lines 9-11.

Edited 5 Years Ago by jonsca: n/a

Hmm, I don't really understand what you're saying there. Use what/which condition? Sorry again, I am pretty new at this stuff.

Nevermind, I got an idea on how to fix it already. Thanks again.

This question has already been answered. Start a new discussion instead.