1

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;
}
2
  • Please write standard English, including capital letters. Commented Mar 31, 2012 at 4:07
  • @thb: While I agree on writing for readers, there doesn't seem to be such a thing as standard English. Unlike Spanish (Castellano) and French, English doesn't have any regulating institution behind itself that defines what's standard and correct and what's not. Commented Mar 31, 2012 at 4:41

3 Answers 3

6

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);
Sign up to request clarification or add additional context in comments.

5 Comments

@user1304516 These are syntax errors, compiler cannot parse your program before you fix them.
WOOOOO I DID IT!!!!!!!!!! thanks so much guys!!! :D:D i need the make it loop now if the answer is correct, how would i do that?
@user1304516 You need to remove \n from both scanf calls to make it work.
ok well i got that working, just the numbers are repeated each time the program loops :|
@user1304516 You should add a call to srand before calling rand for the first time. For example, you can call srand(clock());
2
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.

4 Comments

kk i changed those two, but now im getting these errors multi.c:12:42: error: expected ‘)’ before ‘x’ multi.c:12:42: warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat] multi.c:12:42: warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat]
rand() keeps generating the same numbers over again, i keep getting "what is 3 multiplied by 6", how i do i make so it's different numbers every time?
@user1304516 You have to change the seed. Try srand ( time(NULL) ); before you call rand.
i'm sorry, but i have no idea what srand does.. :\ why would it work?
0
int multiply(int x, int y);

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.