arithmetic problem

This is a discussion on arithmetic problem within the C Programming forums, part of the General Programming Boards category; here's the instructions: write a c program that prompts the user for two integers and one of the letter codes ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Location
    Behind you
    Posts
    2

    arithmetic problem

    here's the instructions:

    write a c program that prompts the user for two integers and one of the letter codes a, s, m, or d.
    a - addition
    s - subtraction
    m - multiplication
    d - division

    and here's the code i've written so far:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    main()
    {
          int x1,      /*first integer value*/
              x2;      /*second integer value*/
          char x;      /*a, s, d, or m*/
    
          printf("\aEnter the first integer: ");
          scanf("%d",&x1);
          printf("\aEnter the second integer: ");
          scanf("%d",&x2);
          printf("\n\a\aYou inputted %d and %d",x1, x2);
          printf("\n\nEnter one letter code:"
                 "\n a - addition"
                 "\n s - subtraction"
                 "\n d - division"
                 "\n m - multiplication\n\n");
          getchar(x);
          getchar(x);
    
                while(scanf("%c",&x)!=EOF)
                   switch(x)
                   {
                         case 'a':
                         case 'A':
                               (float)(x1+x2);
                               break;
                         case 's':
                         case 'S':
                               (float)(x1-x2);
                               break;
                         case 'd':
                         case 'D':
                               (float)(x1/x2);
                               break;
                         case 'm':
                         case 'M':
                               (float)(x1*x2);
                               break;
                   }
          printf("\n\n%d", x);
    the problem i'm having is using the switch statement i think.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    How does that even compile?

    1. getchar() doesn't accept a char. It returns an int, though, so use an int.
    2. Think your check on scanf() isn't really the best. You should be checking, in this case, if the result of scanf() is equal to 1...
    3. Your math inside the cases is stupid since you're not saving the result anywhere. Any decent, modern compiler should be able to tell you this.


    Try working on these points for now.... I'd consider turning up the warning level on your compiler.

    If you're using GCC, or any varient of it like MinGW, you should be able to turn up the warning level to something really decent.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Problem with some arithmetic!
    By bobthebullet990 in forum C Programming
    Replies: 4
    Last Post: 01-05-2007, 09:04 AM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

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