whats wrong with this piece of code?

This is a discussion on whats wrong with this piece of code? within the C Programming forums, part of the General Programming Boards category; hi, really annoying me now, it compiles, runs, and the add or mult work with the response part, but when ...

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

    whats wrong with this piece of code?

    hi, really annoying me now, it compiles, runs, and the add or mult work with the response part, but when it says enter the 2 numbers to either add or mult, when i hit enter it does nothing, just goes down, it just wont do the caculation.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int mult ( int a, int b );
    int add ( int c, int d );
    
    int main()
    {
        int a;
        int b;
        int c;
        int d;
        char e[5];
        
    
        printf( "Add or Mult: " );
        scanf( "%s", &e );
        if (strcmp ( e, "add" ) == 0 ) {
                   printf( "enter two numbers to be added together: " );
                   scanf( "%d", &c );
                   scanf( "%d", &d );
        printf( "the product of your two numbers is %d\n", add( c , d);)}
        else if (strcmp ( e, "mult" ) == 0 ) {
             printf( "enter the two numbers you want to Multiply: " );
             scanf( "%d", &a );
             scanf( "%d", &b );
             printf( "the product of your numbers is %d\n", mult( a, b ) ) ;
             getchar();
             }
             }
             int mult (int a, int b)
             {
                 return a * b;
                 }
                 int add (int c, int d)
                 {
                     return c + d;
                     }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Well this program dosen't compile because of this line
    Code:
     printf( "the product of your two numbers is %d\n", add( c , d);)}
    The semi colon needs to follow the printf statment not the add statment like so
    Code:
     printf( "the product of your two numbers is %d\n", add( c , d));}
    And you will get a warning about main not returning a value otherwise the program works fine.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    well, it does the calculation but u are not seeing it as the screen goes a way without asking for a to enter a key. thats because the 'Enter key has been lft in the input buffer'. u can over come this probem by just including i single statment in your code. and as Quantum1024 says there are few error which dont even compile your code. i have just changed few things in your code which could probably work fine now
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int mult ( int a, int b );
    int add ( int c, int d );
    void clear_buffer(void);
    
    int main()
    {
        int a;
        int b;
        int c;
        int d;
        char e[5];
        
    
        printf( "Add or Mult: " );
        scanf( "%s", &e );
        if (strcmp ( e, "add" ) == 0 ) {
                   printf( "enter two numbers to be added together: " );
                   fflush(stdin);
    			   scanf( "%d ", &c);
                   scanf( "%d", &d );
        printf( "the product of your two numbers is %d\n", add( c , d));}
        else if (strcmp ( e, "mult" ) == 0 ) {
             printf( "enter the two numbers you want to Multiply: " );
             scanf( "%d", &a );
             scanf( "%d", &b );
             printf( "the product of your numbers is %d\n", mult( a, b ) ) ;
             }
        	 clear_buffer();
    		 getchar();
    		 return 0;
             }
             int mult (int a, int b)
             {
                 return a * b;
                 }
                 int add (int c, int d)
                 {
                     return c + d;
                     }	
    void clear_buffer()
    {
     	 int ch;
     	 
     	 while((ch=getchar())!='\n' && ch!=EOF);
    }
    
    /* my ouput
    Add or Mult: mult
    enter the two numbers you want to Multiply: 2
    2
    the product of your numbers is 4
    */
    ssharish2005

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    This is bad
    Code:
    scanf( "%s", &e );
    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-20-2007, 01:07 PM
  2. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 01:03 PM
  3. Replies: 7
    Last Post: 08-06-2004, 09:14 AM
  4. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 09:11 AM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM

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