Thread: I'm stuck with function, help plz

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    13

    Unhappy I'm stuck with function, help plz

    Hi. Below is my program. I had to do one question with functions in my program, however, when i typed it out, i checked for errors but there were none. But only 3 warning messages, it says

    'possible use of 'x', 'y', and 'z' before definition in function main'

    Ok, then, so i ignored that, hoping it would work anyways, the program runs, and when i type in my first integer and hit [enter] key, it gives me a critical error, something saying bout general protection processor fault (something like that). Could anyone help me out here, just by pointing out my mistakes in my program below, and give suggestions what i can do to fix it. Thx!

    #include<stdio.h>
    int add(int, int);
    int subtract(int, int);
    int multiply(int, int);
    int divide(int, int);
    float result = 0;

    main()
    {

    int x, y;
    char z;

    printf("Please enter first integer: ");
    scanf("%d", x);
    printf("Now enter another integer: ");
    scanf("%d", y);
    printf("Now which operation would you like to perform with two integers?\n");
    printf("Press 'a' for addition\nPress 's' for substraction");
    printf("Press 'm' for multiplication\nPress 'd' for division");
    scanf("%c", z);

    switch (z){
    case 'a': case 'A':
    add(x, y);
    printf("Addition: %d + %d = %f.2", x, y, add(x, y));
    break;
    case 's': case 'S':
    subtract(x , y);
    printf("Subtraction: %d + %d = %.2f", x, y, subtract(x, y));
    break;
    case 'm': case 'M':
    multiply(x, y);
    printf("Multiplication: %d + %d = %.2f", x, y, multiply(x, y));
    break;
    case 'd': case 'D':
    divide(x, y);
    printf("Division: %d + %d = %.2f", x, y, divide(x, y));
    break;
    default:
    printf("I'm sorry, the key you typed in was not in operation menu.\n Please try again!");
    }
    return 0;
    }

    int add( int a, int b){
    result = a + b;
    return result;
    }

    int subtract(int a, int b){
    result = a - b;
    return result;
    }

    int multiply(int a, int b){
    result = a * b;
    return result;
    }

    int divide(int a, int b){
    result = a / b;
    return result;
    }

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    scanf takes a pointer as its second parameter...

    like:
    scanf("%d", &x);

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    13

    Talking oops

    oops, i forgot that, damn, how can i make a silly mistake like that, hehe. Anywayz, got ur email right away, fixed that problem with scanf, now when i run the prog., i enter two integers, but unable to enter the last one, z. It immediately gives me the answer of default case. *sigh*, where am i wrong again??? am i supposed to use char for z??? i tried already with int z, i was able to input z, but gives me not found (used default case)

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    That's a common problem with scanf - it's leaving a newline inthe buffer - it's been covered a lot on the boards...

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    13

    Unhappy oh man

    *sigh*, so.............there is absolutely no way we can use scanf with characters, cant we? why is that? how come scanf does not allow this? Is there another possible way we can use besides scanf? (i only learned scanf to input, so i dont know whether there are other ways u can input, apart from declaring variables)

  6. #6
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    You don't understand - the reason it's messing up on the char input is because there's still newlines in the buffer from the integer input before. It's got nothing to do with you inputting a char.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    42
    The spaces take care of the newline buffer problem. Try fgets instead....

    [edit]due to fgets solution below[/edit]
    Last edited by Nit; 03-27-2002 at 08:24 AM.
    http://www.KBeutler.com

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    42
    Code:
    #include <stdio.h>
    #define MAXLINE 1024
     
    int add(int, int); 
    int subtract(int, int); 
    int multiply(int, int); 
    int divide(int, int);
    
    float result = 0.0;
    
    int add (int a, int b) {
    	result = a + b;
    	return result; 
    } 
    
    int subtract (int a, int b) {
    	result = a - b;
    	return result; 
    } 
    
    int multiply (int a, int b) { 
    	result = a * b;
    	return result;
    } 
    
    int divide (int a, int b) { 
    	result = a / b;
    	return result;
    }
    
    int main() { 
    
    	int x, y; 
    	char z[4];	//holds an integer up to 3 digits big. 
    
    	printf("Please enter first integer: "); 
    	fgets(z, MAXLINE, stdin);
    	x = atoi(z);	//converts string to int.
    
    	printf("Now enter another integer: "); 
    	fgets(z, MAXLINE, stdin);
    	y = atoi(z);	//converts string to int.
    
    	printf("\nNow which operation would you like to perform with two integers?\n"); 
    	printf("Press 'a' for addition\nPress 's' for substraction\n"); 
    	printf("Press 'm' for multiplication\nPress 'd' for division\n"); 
    	printf("> ");
    	fgets(z, MAXLINE, stdin);
    
    
    	switch (z[0]) { 
    		case 'a': case 'A':
    			add(x, y);
    			printf("Addition: %d + %d = %.2f\n", x, y, result); 
    			break; 
    		case 's': case 'S': 
    			subtract(x, y);
    			printf("Subtraction: %d - %d = %.2f\n", x, y, result); 
    			break; 
    		case 'm': case 'M': 
    			multiply(x, y);
    			printf("Multiplication: %d * %d = %.2f\n", x, y, result); 
    			break; 
    		case 'd': case 'D': 
    			divide(x, y);
    			printf("Division: %d / %d = %.2f\n", x, y, result); 
    			break; 
    		default: 
    			printf("I'm sorry, the key you typed in was not in operation menu. Please try again!\n"); 
    	} 
    
    	return 0; 
    }
    Above I defined MAXLINE to be 1024 to be the max length of the buffer to be read from stdin. I also added char z[4] to be an array that can hold up to an integer 3 digits big (this includes the \0 null terminator at the end of the string that is read in fgets).
    http://www.KBeutler.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Brand new to C need favor
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 09-21-2007, 10:08 AM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM