frustrated-c calcuator 
Author Message
 frustrated-c calcuator

I made this program a simple c calculator but it doesn't work properly
it displayes the enter operation adn enter sencond number at the same
time what did I do wrong. thanx,
                             yoyo2904

#include <stdio.h>

int main () {
double num1, num2, total;
char operation;
printf("Welcome to Yonatan's calculator\n");
printf("Enter the first number\n");
scanf("%e", &num1);
printf("enter operation\n");
scanf("%c", &operation);
printf("enter second number\n");
scanf("%e", &num2);
switch(operation) {
case '+':
total = num1 + num2;
printf("%e\n",total);
getch();
break;
case '-':
total = num1 - num2;
printf("%e\n",total);
getch();
break;
case '*':
total = num1*num2;
printf("%e\n",total);
getch();
break;
case '/':
if (num2 == 0);
        printf("ERROR: DIVIDE BY ZERO!!\n");
getch();
if(num2 != 0);
        total = num1 / num2;
        printf("%e\n",total);
getch();
break;
default:
printf("ERROR\n");
getch();
break;

Quote:
}
}

--



Tue, 24 Jun 2003 14:22:57 GMT  
 frustrated-c calcuator

Quote:

> I made this program a simple c calculator but it doesn't work properly
> it displayes the enter operation adn enter sencond number at the same
> time what did I do wrong. thanx,
>                              yoyo2904

The problem is that when you are reading the first number, you does not
remove the newline character, therefore, the 'operation' scanf reads a
newline character and proceeds to the second number

Try the following program instead:

#include <stdio.h>

int main () {
  float num1, num2, total;
  char operation;
  printf("Welcome to Yonatan's calculator\n");
  printf("Enter the first number\n");
  scanf("%f", &num1);
  getchar();
  printf("enter operation\n");
  scanf("%c", &operation);
  printf("enter second number\n");
  scanf("%f", &num2);
  printf("%f %c %f = ",num1,operation,num2);
  switch(operation) {
  case '+':
    total = num1 + num2;
    printf("%f\n",total);
    break;
  case '-':
    total = num1 - num2;
    printf("%f\n",total);
    break;
  case '*':
    total = num1*num2;
    printf("%f\n",total);
    break;
  case '/':
    if (num2 == 0)
      printf("ERROR: DIVIDE BY ZERO!!\n");
    else{
      total = num1 / num2;
      printf("%f\n",total);
    }
    break;
  default:
    printf("ERROR\n");
    break;
  }

Quote:
}

--
MVH

Mikkel Bjerg
--



Wed, 25 Jun 2003 05:31:12 GMT  
 frustrated-c calcuator

Quote:

> I made this program a simple c calculator but it doesn't work properly
> it displayes the enter operation adn enter sencond number at the same
> time what did I do wrong. thanx,
>                              yoyo2904

Hi,
First of all, your indentation makes it hard to read your code, tra
to improve that

Quote:
> #include <stdio.h>

> int main () {

I'd prefer
int main(void)

Quote:
> double num1, num2, total;
> char operation;
> printf("Welcome to Yonatan's calculator\n");
> printf("Enter the first number\n");
> scanf("%e", &num1);

Avoid scanf(). It is very difficult to use it in a way which is safe
against bogus input from the user. And here is your problem: scanf()
leaves the \n in the buffer, so the next scanf finds the \n and
returns without converting/reading anything. In your simple (and not
safe) input handling you can use a getchar() to get rid of the \n

Quote:
> printf("enter operation\n");
> scanf("%c", &operation);

same as above

Quote:
> printf("enter second number\n");
> scanf("%e", &num2);

same as above
besides that you force the user to enter the numbers in scientific
notation ( the %e format specifier ) and the format specifier is for
float, not for double. If you insist in using scanf(), use %le or
better %lf and check the return value of scanf(). It returns the
number of converted items

Quote:
> switch(operation) {
> case '+':
> total = num1 + num2;
> printf("%e\n",total);
> getch();

getchar();
;-)

Quote:
> break;
> case '-':
> total = num1 - num2;
> printf("%e\n",total);
> getch();
> break;
> case '*':
> total = num1*num2;
> printf("%e\n",total);
> getch();
> break;
> case '/':
> if (num2 == 0);
>         printf("ERROR: DIVIDE BY ZERO!!\n");
> getch();
> if(num2 != 0);
>         total = num1 / num2;
>         printf("%e\n",total);
> getch();
> break;
> default:
> printf("ERROR\n");
> getch();
> break;
> }

Now just think about one more thing:
In the above switch() ladder you have a printf("%e\n",total); and a
getchar(); in every case. You can simplify the code, writing for
example:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    double num1, num2, total;
    char operation;

    printf("Welcome to Yonatan's calculator\n");
    printf("Enter the first number\n");
    scanf("%lf", &num1);
/*check the return value of scanf() and act sensible */
    getchar();
    printf("enter operation\n");
    scanf("%c", &operation);
/*check the return value of scanf() and act sensible */
    getchar();
    printf("enter second number\n");
    scanf("%lf", &num2);
/*check the return value of scanf() and act sensible */
    getchar();

    if(!('/' == operation && 0.0 == num2))
    {
        switch(operation)
        {
            case '+':
                total = num1 + num2;
                break;
            case '-':
                total = num1 - num2;
                break;
            case '*':
                total = num1 * num2;
                break;
            case '/':
                total = num1 / num2;
                break;
            default:
                printf("ERROR\n");
                getchar();
                return EXIT_FAILURE;
        }
        printf("%e\n",total);
        getchar();
    }
    else
    {
        printf("ERROR: DIVIDE BY ZERO ATTEMPTED!!\n");
        getchar();
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;

Quote:
}

As others will probably point out, this is stiil not the best
possible way to do it, especially the scanf()s should better be
replaced with fgets() and parsing of the input string. I recommend
you to read the description of scanf(), fgets() and the string
conversion functions carefully and improve the input sequence of
your code using fgets(), strtod(), strtok() and friends. I would
also let the user type the whole expression in one line ( 123 + 456
<enter> ), which can be easily done with fgets()

HTH

Robert
--
Robert Stankowic

"The regular early morning yell of horror" - Douglas Adams
--



Wed, 25 Jun 2003 05:31:40 GMT  
 frustrated-c calcuator

writes

Quote:
>I made this program a simple c calculator but it doesn't work properly
>it displayes the enter operation adn enter sencond number at the same
>time what did I do wrong. thanx,

As you do no input validation and are using scanf, my best guess is that
you have some invalid input from the keyboard.

If you use scanf it is very important to check the return value, and
also to clear the input buffer.  Consider using fgets() to read the
input and then go and read up about the conversion functions (atoi, atod
etc.)

Francis Glassborow      Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Wed, 25 Jun 2003 12:29:32 GMT  
 frustrated-c calcuator


Quote:
>> printf("Welcome to Yonatan's calculator\n");
>> printf("Enter the first number\n");
>> scanf("%e", &num1);

>Avoid scanf(). It is very difficult to use it in a way which is safe
>against bogus input from the user. And here is your problem: scanf()
>leaves the \n in the buffer, so the next scanf finds the \n and
>returns without converting/reading anything. In your simple (and not
>safe) input handling you can use a getchar() to get rid of the \n

>> printf("enter operation\n");
>> scanf("%c", &operation);

Well, to be precise, the problem is exactly because you used %c almost
every other format specifier would skip whitespace (a newline character
is white space) getchar is not enough, if you want char input you must
read the input until a non-whitespace character is found.

Francis Glassborow      Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Wed, 25 Jun 2003 12:43:49 GMT  
 frustrated-c calcuator
Thank you very much! :-)
yoyo2904
--



Wed, 25 Jun 2003 12:44:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Frustrated with VC++.NET 2003

2. Frustrating save behaviour

3. Frustrating

4. Frustrated with the ICON editor in Visual C#.

5. frustrated-c calculator

6. frustrated (here's the code :-) )

7. frustrated (here's the code :-) )

8. frustrated...

9. A VERY frustrated Programmer...

10. MSVC++ v.5 is frustrating.....

11. Frustrated!!! ExitWinEx()/Borland C 4.0

12. frustrating linking problem

 

 
Powered by phpBB® Forum Software