Thread: keep getting an to me unknown error

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    5

    keep getting an to me unknown error

    Hello everyone,
    I get an error in connection with scanf_s (at least i think it's because of this) and i've never seen it before. I hope someone can help me
    I will add a picture of it aswell as the source code

    The program is meant to the basic calculation forms with fractions.
    keep getting an to me unknown error-unbenannt-jpg

    The error should be in there
    Code:
    rational read_rational(void){
    	printf("Geben sie den Zaehler des ersten Bruchs ein\n");
    	scanf_s("%i", fraction1.numerator);
    
    
    	printf("Geben sie den Nenner des ersten Bruchs ein\n");
    	scanf_s("%i", fraction1.denominator);
    
    
    	printf("Geben sie den Zaehler des zweiten Bruchs ein\n");
    	scanf_s("%i", fraction2.numerator);
    
    
    	printf("Geben sie den Nenner des zweiten Bruchs ein\n");
    	scanf_s("%i", fraction2.denominator);
    
    
    	ggT(fraction1.denominator, fraction2.denominator);
    	fraction1.numerator *= (LMC / fraction1.denominator);
    	fraction1.denominator = LMC;
    
    
    	fraction2.numerator *= (LMC / fraction2.denominator);
    	fraction2.denominator = LMC;
    
    
    	return fraction1, fraction2;
    }
    In this header the variables I used are defined
    Code:
    #ifndef rational_h#define rational_h
    
    
    #include <stdio.h>
    
    
    
    
    typedef struct {
    	long numerator, denominator;
    }rational;
    
    
    rational fraction1;
    rational fraction2;
    rational result;
    long LMC;
    
    
    rational addition(rational, rational);
    rational subtraction(rational, rational);
    rational multiplication(rational, rational);
    rational division(rational, rational);
    void print_rational(rational);
    rational read_rational(void);
    long ggT(long, long);
    
    
    #endif

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You need to pass the address of the variable to scanf.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    5
    Ah, got it now. Thank you
    Maybe someone can help me here too. Got everything done now, but I still get two errors when I use the programme.
    Weirldy, some fractions dont make me problems at all, but on some the program just randomly stops doing anything in the division part, nothing happens anymore.
    The other one is when I use the loop. I made it, that you can go for several loops until you tipe a "0" to stop the program. But when I go for the second loops it says Stack Overflow and I dont know why.

    This is the main .c
    Code:
    #include "rational.h"
    
    int main(void)
    {
    	int test;
    
    
    	do {
    		fraction1.denominator=0;
    		fraction1.numerator=0;
    		fraction2.denominator = 0;
    		fraction2.numerator = 0;
    		result.denominator = 0;
    		result.numerator = 0;
    		LMC = 0;
    
    
    
    
    		read_rational();
    		getch();
    		addition(fraction1, fraction2);
    		if (result.denominator < 0)
    		{
    			result.denominator = -result.denominator;
    			result.numerator = -result.numerator;
    		}
    		printf("Das Ergebnis der Addition beider Brüche ist:\n");
    		print_rational(result);
    
    
    
    
    
    
    
    
    
    
    		result = subtraction(fraction1, fraction2);
    		if (result.denominator < 0)
    		{
    			result.denominator = -result.denominator;
    			result.numerator = -result.numerator;
    		}
    		printf("Das Ergebnis der Subtraktion beider Brüche ist:\n");
    		print_rational(result);
    
    
    
    
    
    
    
    
    		result = division(fraction1, fraction2);
    		if (result.denominator < 0)
    		{
    			result.denominator = -result.denominator;
    			result.numerator = -result.numerator;
    		}
    		printf("Das Ergebnis der Division beider Brüche ist:\n");
    		print_rational(result);
    
    
    
    
    
    
    		result = multiplication(fraction1, fraction2);
    		if (result.denominator < 0)
    		{
    			result.denominator = -result.denominator;
    			result.numerator = -result.numerator;
    		}
    		printf("Das Ergebnis der Multiplikation beider Brüche ist:\n");
    		print_rational(result);
    
    
    		printf("Wenn sie aus der Schleife aussteigen wollen, geben sie '0' ein. Ansonsten etwas beliebiges anderes\n");
    		scanf_s("%d\n\n\n", &test);
    	} while (test != 0);
    
    
    
    
    	return 0;
    }
    here is the header, but there shouldnt be a problem
    Code:
    #ifndef rational_h#define rational_h
    
    
    #include <stdio.h>
    
    
    
    
    typedef struct {
    	long numerator, denominator;
    }rational;
    
    
    rational fraction1;
    rational fraction2;
    rational result;
     long LMC;
    
    
    rational addition(rational, rational);
    rational subtraction(rational, rational);
    rational multiplication(rational, rational);
    rational division(rational, rational);
    void print_rational(rational);
    rational read_rational(void);
    long ggT(long, long);
    long ggT1(long, long);
    
    
    #endif
    and here is the .c for the module. there is the division part also where the program stops. and when I get the other error with stack overflow its at the ggT function (at least there is where the cursor stops
    Code:
    #include "rational.h"
    
    
    
    
    
    rational addition(rational fraction1, rational fraction2)
    {
    
    
    	result.numerator = fraction1.numerator + fraction2.numerator;
    	result.denominator = fraction1.denominator;
    	if (result.numerator < result.denominator)
    	{
    		LMC = ggT1(result.numerator, result.denominator);
    	}
    	else
    	{
    		LMC = ggT1(result.denominator, result.numerator);
    	}
    
    
    	result.numerator /= LMC;
    	result.denominator /= LMC;
    	return result;
    }
    
    
    
    
    
    
    rational subtraction(rational fraction1, rational fraction2)
    {
    	result.numerator = fraction1.numerator-fraction2.numerator;
    	result.denominator = fraction1.denominator;
    	if (result.numerator < result.denominator)
    	{
    		LMC = ggT1(result.numerator, result.denominator);
    	}
    	else
    	{
    		LMC = ggT1(result.denominator, result.numerator);
    	}
    
    
    	result.numerator /= LMC;
    	result.denominator /= LMC;
    	return result;
    }
    
    
    
    
    
    
    rational multiplication(rational fraction1, rational fraction2)
    {
    	result.numerator = fraction1.numerator * fraction2.numerator;
    	result.denominator = fraction1.denominator*fraction2.denominator;
    	if (result.numerator < result.denominator)
    	{
    		LMC = ggT1(result.numerator, result.denominator);
    	}
    	else
    	{
    		LMC = ggT1(result.denominator, result.numerator);
    	}
    
    
    	result.numerator /= LMC;
    	result.denominator /= LMC;
    
    
    	return result;
    }
    
    
    
    
    
    
    rational division(rational fraction1, rational fraction2)
    {
    
    
    	result.numerator = fraction1.numerator * fraction2.denominator;
    	result.denominator = fraction1.denominator*fraction2.numerator;
    
    
    	if (result.numerator < result.denominator)
    	{
    		LMC=ggT1(result.numerator, result.denominator);
    	}
    	else
    	{
    		LMC = ggT1(result.denominator, result.numerator);
    	}
    
    
    	result.numerator /= LMC;
    	result.denominator /= LMC;
    	
    	return result;
    }
    
    
    
    
    
    
    long ggT(long a, long b)
    {
    	static long helper = 0;
    	helper += b;
    
    
    
    
    
    
    	if (helper%a == 0 && helper%b == 0)
    	{
    		return helper;
    	}
    	else 
    	{
    		return ggT(a, b);
    	}
    
    
    	
    	}
    
    
    long ggT1(long a, long b)
    {
    	int r = 0;
    
    
    	if (a == 0)
    	{
    		return b;
    	}
    	while (b != 0)                       
    	{
    		if (a > b)
    		{
    			a = a - b;             
    		}
    		else
    		{
    			b = b - a;            
    		}
    	}
    	return a;                           
    }
    
    
    
    
    rational read_rational(void)
    {
    	printf("Geben sie den Zaehler des ersten Bruchs ein\n");
    	scanf_s("%x", &fraction1.numerator);
    
    
    	printf("Geben sie den Nenner des ersten Bruchs ein\n");
    	scanf_s("%x", &fraction1.denominator);
    
    
    	printf("Geben sie den Zaehler des zweiten Bruchs ein\n");
    	scanf_s("%x", &fraction2.numerator);
    
    
    	printf("Geben sie den Nenner des zweiten Bruchs ein\n");
    	scanf_s("%x", &fraction2.denominator);
    
    
    	LMC=ggT(fraction1.denominator, fraction2.denominator);
    	fraction1.numerator *= (LMC / fraction1.denominator);
    	fraction1.denominator = LMC;
    
    
    	fraction2.numerator *= (LMC / fraction2.denominator);
    	fraction2.denominator = LMC;
    
    
    	return fraction1;
    }
    
    
    void print_rational(rational result)
    {
    	printf("%ld/%ld\n\n", result.numerator, result.denominator);
    }

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    It would be easier to help you if the prompts were translated to English. Also, you should provide the inputs that cause the problems you're seeing (what input are you giving it, what output do you expect, and what output are you actually getting).

    Quote Originally Posted by crowlow View Post
    Weirldy, some fractions dont make me problems at all, but on some the program just randomly stops doing anything in the division part, nothing happens anymore.
    Look at the loop in the ggT1() function - it's very possible that for certain values, the condition will never be false, leading to an infinite loop. (This would be a good time to learn how to use a debugger.)

    Get rid of the newlines in the scanf, they are not needed:

    Code:
    scanf_s("%d\n\n\n", &test);
    Don't declare your variables in a header file, and avoid global variable declarations. Declare them in the scope they are needed.

    You are not assigning the return value here:

    Code:
    addition(fraction1, fraction2);
    Last edited by Matticus; 12-28-2017 at 01:28 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help, getting unknown error
    By halien in forum C Programming
    Replies: 11
    Last Post: 01-28-2010, 07:13 PM
  2. Unknown error...
    By yaya in forum C++ Programming
    Replies: 8
    Last Post: 07-11-2007, 03:14 AM
  3. unknown error
    By pktcperlc++java in forum C++ Programming
    Replies: 8
    Last Post: 12-29-2004, 06:08 PM
  4. unknown error!
    By black_sol in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2004, 06:51 PM

Tags for this Thread