Thread: Text Calculator, first program, not working correctly

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    Unhappy Text Calculator, first program, not working correctly

    i've been learning c for about, 2 days, and i tried to make a text-based calculator, and everything was going fine until i realized it is only doing addition... this is the code i made

    Code:
    #include <stdio.h>
    
    int mult( int x, int y );
    
    
    int add( int t, int u );
    
    
    int sub( int z, int w );
    
    
    int div( int a, int b );
    
    
    int main()
    {
        int first;
        int x;
        int y;
        int t;
        int u;
        int z;
        int w;
        int a;
        int b;
     int mult( int x, int y );
    
    
    int add( int t, int u );
    
    
    int sub( int z, int w );
    
    
    int div( int a, int b );
    
        printf( "press 1 for addition, 2 for subtraction, 3 for multipulcation, or 4 for division" );
        scanf( "%d", &first );
        if( first = 1 ) {
         printf( "Please enter your first number: " );
         scanf( "%d", &t );
         printf ( "Now your second number please: " );
         scanf ( "%d", &u );
         printf ( "Your answer is: %d\n", add ( t, u ) );
         getchar();
        }
        else {
            if ( first = 2 ) {
             printf ( "Please enter your first number: " );
             scanf ( "%d", &z );
             printf ( "Now your second number please: " );
             scanf ( "%d", &w );
             printf ( "Your answer is: %d\n", sub( z, w ) );
             getchar();
            }
            else {
                if ( first = 3 ) {
                 printf ( "Please enter your first number: " );
             scanf ( "%d", &x );
             printf ( "Now your second number please: " );
             scanf ( "%d", &y );
             printf ( "Your answer is: %d\n", mult( x, y ) );
             getchar();
                }
                else {
                    if ( first = 4 ) {
                    printf ( "Please enter your first number: " );
             scanf ( "%d", &a );
             printf ( "Now your second number please: " );
             scanf ( "%d", &b );
             printf ( "Your answer is: %d\n", div( a, b ) );
             getchar();
                }
            }
        }
    }
    
    
    
    
    }
    int add (int t, int u)
     {
         return t + u;
     }
    
    int sub (int z, int w)
     {
         return z - w;
     }
    
    int mult (int x, int y)
     {
         return x * y;
     }
    
    int div (int a, int b)
     {
         return a / b;
     }

    i've gotten rid of any errors, but when i try it only does addition no matter what i choose, i think it may have something to do with it not recognizing what i put in for "first", any help would be nice

    p.s i know i suck, but i've only got 1 or 2 days under my belt

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    if( first = 1 )
    You are doing assignment here, not a comparison. What you want is:
    Code:
    if( first == 1 )
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    2
    thank you so much, i should have remembered that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Dart Score Program Not Working Correctly, Help Please.
    By matthayzon89 in forum C Programming
    Replies: 4
    Last Post: 09-25-2010, 11:57 PM
  2. Max/min function not working correctly
    By En-Motion in forum C++ Programming
    Replies: 6
    Last Post: 03-19-2009, 12:28 AM
  3. Pointers are not working correctly
    By adrian_fpd in forum C Programming
    Replies: 8
    Last Post: 11-17-2008, 07:55 PM
  4. File I/O not working correctly
    By gL_nEwB in forum C++ Programming
    Replies: 4
    Last Post: 05-27-2006, 10:29 PM
  5. A Simple Calculator Program Not Working
    By oobootsy1 in forum C++ Programming
    Replies: 9
    Last Post: 01-09-2004, 09:34 PM

Tags for this Thread