user1304516
user1304516

Reputation: 13

Keep getting errors when compiling C program :(

I'm completely useless when it comes to programming, so keep that in mind!

We had to write a code which generates two random numbers, and the two random numbers were then passed into a function which produced and then returned the sum. The student is prompted to answer the questions, if they get it wrong the program should loop until they get it right, and if they are correct the program should loop and ask another question.

When I compile I keep getting these errors:

multi.c: In function ‘multiply’:
multi.c:6:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
multi.c:27:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
multi.c:31:1: error: expected ‘{’ at end of input

Here is my code, can someone please help me:

#include <stdio.h>

int multiply(int x, int y)

int main()
{
    int multiply(int x, int y);
    int x = rand()%20;
    int y = rand()%20;
    int i, answer;
    i = multiply(x,y);

    printf("what is %d multiplied by %d\n?" x, y);
    scanf("%d\n", &answer);

    while(answer != i)
    {
        printf("wrong try again!");
        scanf("%d\n", &answer);
    }

    printf("very good!");
}

int multiply(int x, int y)
{
    int k;
    (x*y = k);
    return k;
}

Upvotes: 1

Views: 377

Answers (3)

torrential coding
torrential coding

Reputation: 1765

int multiply(int x, int y);

Why do you have this in main()? You can't have function prototypes inside a function.

Upvotes: 0

Pubby
Pubby

Reputation: 53017

int multiply(int x, int y)

This is missing a semicolon.


int multiply(int x, int y);

You can't declare functions inside function bodies. Just delete this line.


(x*y = k);

You've got assignment backwards. k = x * y; is correct.

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

The assignment on line 27 x*y = k should be k = x*y.

There is a missing semicolon after int multiply(int x, int y) on line 6.

There is a missing coma after the string literal on line 13:

printf("what is %d multiplied by %d\n?" /* here */ x, y);

Upvotes: 6

Related Questions