Can someone help to fix my simple calculator program?

This is a discussion on Can someone help to fix my simple calculator program? within the C Programming forums, part of the General Programming Boards category; I am new to programming, so correct me if I am in the wrong. Can someone please me to solve ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    8

    Angry Can someone help to fix my simple calculator program?

    I am new to programming, so correct me if I am in the wrong. Can someone please me to solve the problem below. I am creating a simple calculator.



    #include <stdio.h>

    void main()
    {
    char opr;
    float ans;
    float num;
    int counter=0;


    do{
    counter++;

    do{

    printf("Please insert integer:\n");
    scanf("%f", &num);

    if(counter > 0)
    {
    if(opr=='+')
    ans += num;
    if(opr=='-')
    ans -= num;
    if(opr=='*')
    ans *=num;
    if(opr=='/')
    ans /= num;
    }

    else

    scanf("%[+-*/=]", &opr);
    if(counter==1)
    {
    ans=num;
    }
    counter++;
    }while(opr!='=');

    if(opr=='=')
    printf("Ans is %f", ans);

    }while(getchar()!='Q');
    if (getchar()=='Q')
    {
    printf("Bye\n");
    }

    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: Can someone help to fix my simple calculator program?

    Originally posted by Aven
    I am new to programming, so correct me if I am in the wrong. Can someone please me to solve the problem below. I am creating a simple calculator.
    What problem? You need to tell us whats wrong....

    Please use code tags when posting code (see my sig).

    >void main()
    is wrong, use
    >int main(void)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    8
    The program keeps on asking for input value non stop

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >printf("Please insert integer:\n");
    >scanf("%f", &num);
    You ask for an int, but scanf() for a float. Make up your mind which one!

    >if(opr=='+')
    at this point, opr has not been assigned a value, so you cannot test it and guarantee it the correct response.

    When you layout your code correctly, you can see a logic problem here:
    Code:
    if(counter > 0)
    {
    	if(opr=='+')
    	ans += num;
    	if(opr=='-')
    	ans -= num;
    	if(opr=='*')
    	ans *=num;
    	if(opr=='/')
    	ans /= num;
    }
    else scanf("%[+-*/=]", &opr);
    counter is always greater than 0, therefore you won't be scanf()'ing for opr.

    There maybe some more, but it'd be best if you sort the logic out first....
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    8
    The meaning of this program is first the compiler goes in for the first time, it checks for the condition of the if(counter>0), but instead the conuter starts from counter=0, so it will skip this part since it is false.

    After that it will go to the else statement instead.Here it will ask the user to input an operator. Nest, assign the value from num to ans and the counter will be incremented.

    Now, the compiler will go in the second time, it will ask the user to input the second number. This time counter is set to 1, so if(counter>0) which is 1>0, so the +,-,*,/ operators which the user input previously will be used to perform the calculation. If the users decide to insert the = operators, then the ans will display the real answer.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OK, I understand your theory, but your code says otherwise.

    The counter variable is initialised to 0 here
    >int counter = 0;
    but the next thing you do is this
    >counter++;
    So counter is now 1. This is (one of) your problems.

    Also, you don't reset counter to 0, so the user will not get the chance to enter another opr. I don't know if this is what you intended or not? (I presume not, as it must at some point be set to the equals sign)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    8
    Now, if I get ride of counter++ after do{, I wil still continue to have the which is my pogram keeps asking to insert value.

    Code
    -----------------------------------------------------------------------------------
    do{


    do{

    printf("Please insert value:\n");
    scanf("%f", &num);

    if(counter > 0)
    {
    if(opr=='+')
    ans += num;

    if(opr=='-')
    ans -= num;

    if(opr=='*')
    ans *=num;

    if(opr=='/')
    ans /= num;
    }

    else

    scanf("%[+-*/=]", &opr);

    if(counter==0)
    {
    ans=num;
    }
    counter++;
    }while(opr!='=');

    __________________________________________________ __

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I think you should print a prompt for the user to enter the operator:

    Code:
    {
    	printf ("Enter operator: ");
    	scanf("%[+-*/=]", &opr);
    }
    Personally, I wouldn't use scanf() in this manner though. I'd probably use getchar() or fgets(), and validate the input myself.

    Anyway, adding this prompt will show you that the program does ask you for the opr, but appears to jump straight over it. This is because of the characters left in the input stream from the previous call the scanf(). You can try flushing the input buffer with
    >while (getchar() != '\n');
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ code need help to fix
    By McReal in forum C++ Programming
    Replies: 9
    Last Post: 05-12-2007, 03:48 PM
  2. ...deceptively simple...
    By Sebastiani in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 07-29-2002, 01:51 PM
  3. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM
  4. The following code has one simple error.....
    By bluehead in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 11-08-2001, 07:45 PM
  5. One simple error(IN DOS)
    By bluehead in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2001, 08:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21