1

I don't really understand why it's returning a segmentation fault... I'm sorry. Can someone please explain to me what I did wrong here? I have the main method ask questions before it calls the input method to create new structs that will be passed to the multiFunction, which will multiply the fractions. I try to have it passed to the outputFunction to print it, but that doesn't seem to want to work.

struct rational {
    int firstNum;
    int secondNum;
    int result;
};
void inputFunction(struct rational ratNum);
void outputFunction(struct rational ratNum);
void addFunction(struct rational one, struct rational two);
void multFunction(struct rational one, struct rational two);

void inputFunction (struct rational ratNum){
    ratNum.firstNum = 0;
    ratNum.secondNum = 0;
    printf("Enter the first int: ");
    scanf("%d", &ratNum.firstNum);
    printf("Enter the second int: ");
    scanf("%d", &ratNum.secondNum);
 }
 void outputFunction(struct rational ratNum){
    printf("Output: %d /", ratNum.firstNum);
}
void multFunction (struct rational one, struct rational two){

    struct rational ratNum;
    ratNum.firstNum = 0;
    ratNum.secondNum = 0;
    ratNum.result = 0;
    int resultNum = one.firstNum * two.firstNum;
    int resultNum2 = one.secondNum * two.secondNum;
    ratNum.firstNum = resultNum;
    ratNum.secondNum = resultNum2;
    int result = resultNum / resultNum2;
    ratNum.result = result;
    outputFunction(ratNum);
}
int main(void){

    struct rational ratNum;
    char yesNo = 'y';
    int choice = 0;
    while(yesNo == 'y'){
            printf("Do you want to run the program (y/n): ");
            scanf("%c", &yesNo);

            inputFunction(ratNum);

            struct rational firstRational;
            firstRational.firstNum = ratNum.firstNum;
            firstRational.secondNum = ratNum.secondNum;

            inputFunction(ratNum);

            struct rational secondRational;
            secondRational.firstNum = ratNum.firstNum;
            secondRational.secondNum =  ratNum.secondNum;

            printf("Enter 1 for add and 2 for multiply: ");
            scanf("%d", &choice);
            if(choice == 1){
                    addFunction(firstRational, secondRational);
            }
            else if(choice = 2){
                    multFunction(firstRational, secondRational);
            }
            printf("Do you want to run the program again (y/n): ");
            scanf("%c", yesNo);
    }

}

  • 4
    C uses pass by value. Calling `inputFunction(ratNum);` makes a copy of the `ratNum` object and does not modify the original in any way. The `scanf`s in `inputFunction` only modify the copy which is then discarded. The fact that the parameter `ratNum` in `inputFunction` has the same name as the variable `ratNum` in `main` doesn't change this but makes it more confusing. Anyway, back in `main` you are left with the original `ratNum` which still contains uninitialized garbage. – Nate Eldredge Apr 13 '21 at 03:28
  • You probably want to pass by reference instead, by passing a pointer to `inputFunction`; see https://stackoverflow.com/a/13555817/634919 for an example. Or perhaps you want `inputFunction` to take no arguments and return a `struct rational` instead. – Nate Eldredge Apr 13 '21 at 03:30
  • 1
    `int result` has no business being a member of `struct rational`. Functions in C can return values. You probably don't want to pass anything by reference. Instead, do something like `struct rational input(void)` and `struct rational add(struct rational one, struct rational two)` (lose the meaningless suffix "function"). – n. m. could be an AI Apr 13 '21 at 03:49

0 Answers0