Thread: character string problem

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    10

    character string problem

    Hi,
    I need to write a program that has the user input a command such as ADD (adds two numbers), SUB(subtracts two numbers), DIV( divides), MUL(multiplies) and then input the two numbers. The user is asked to input all three things at one time. Here is the code.


    Code:
    #include<stdio.h>
    #include<math.h>
    
    int performance(char[], int, int);
    
    int main()
    {
      int one;
      int two;
      char com[3];
      int FI;
    
      printf("Enter Instruction:");
      scanf(" %3s %d %d", &com, &one, &two);
    
      FI =  performance( com, one, two);
      printf("the answer is %d\n", FI);
      return(0);
    
    }
    
    int performance(char A[], int B, int C)
    {
      int final;
      char ADD;
      char SUB;
      char DIV;
      char MUL;
    
      if(A[3] == ADD)
      {
        printf("%d", final);
      }
      if(A[3] == SUB)
      {
        final = B - C;
      }
      if(A[3] == DIV)
      {
        final = B / C;
      }
      if(A[3] ==  MUL)
      {
        final = B*C;
      }
      return(final);
    }
    I am not getting the right answers. I think it has something to do with my if statements.

    please help.
    THANKS

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    1. you haven't given enough room to store a three-character string in array comm -- you forget about the null-terminator. So it should be a minimum of 4, not 3 characters.

    2. use strcmp() to check if the string contains specific characters. For example
    Code:
     if( strcmp(A, "ADD") == 0)

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Code:
    int final;
      char ADD;
      char SUB;
      char DIV;
      char MUL;
    Problems 'o plenty...

    What values are stored in each of the above variables? Do you know? The thing is that you did not initialise them to any value at all, and as such they are holding some garbage value. Then you use them as values to test against, when they are holding no meaningful value. So in that sense, yes, it does has something to do with your if statements..

    edit

    But yes, Ancient Dragon has given you an example which ought to do what you intended. Just remember to #include <string.h> and you may as well ditch those char variables in your function 'performance'.
    Last edited by kermit; 11-28-2005 at 04:33 PM.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    Well,

    char ADD; isnt actually making a string a containing the word ADD, it simply creates a variable called ADD.

    Also A[3] is the 4th element in A, i.e if A was "hello", A[3] would be l (the 2nd one).

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM