need help with simple problem

Soldato
Joined
9 Dec 2006
Posts
9,289
Location
@ManCave
im making a random maths game (first app)

PHP:
int number1 = rand() % 25;
int number2 = rand() % 30;
int USER;
int CPU = ("%d"*"%d", &USER ,&CPU);
printf("what is %d times %d\n",number1, number2);
printf("Enter Your Answer: ");
scanf("%d", &USER);

im having trouble with int CPU
can anyone tell me how to mutiply to variables together correctly?

also would you say this is a clean program so far?
 
You can multiply variables much simpler than how you've attempted, like this:

Code:
int number1 = 2;
int number2 = 5;
int result = number1 * number 2;
printf("%d", result); // This outputs 5

Does that make sense? Format specifiers like "%d" are used only by the printf() family of functions so they know how to print stuff out to the screen.
 
thanks i did work it out just before you posted, i come across another problem now,

i played around with it but not got it right yet

PHP:
// loop.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdlib.h>
int _tmain(int argc, _TCHAR* argv[])
{
/*declare your varibles*/ 
int number1 = rand() % 25;
int number2 = rand() % 30;
int USER;
int CPU = (number1*number2);
int Questions;
int QN;
printf("How many questions would you like to answer?");
scanf("&d",Questions);
for (QN = 0; QN < "%d",Questions; QN++ ) {
printf("what is %d times %d\n",number1, number2);
printf("Enter Your Answer: ");
scanf("%d", &USER);
if ("%d" == "%d" ,USER,CPU){
printf("You are Right\n");
}
else {
printf("You are wrong\n");
}
printf("Next Question");
}


getchar();
return 0;
}
 

im trying to loop the question around for as many times as user input to answer,

im going though the c programming tutorail but does show how to do this.

if you wish to help please do :)
 
So your still using "%d" incorrectly, it should only be used with printf(). There is no need to use it elsewhere and you just use the variable name directly. So your loop condition is wrong and should just be QN < Questions and the if condition should also just use variable names directly. Otherwise all good :)

Try this for your loop:

Code:
for (QN = 0; QN < Questions; QN++ ) {
    printf("what is %d times %d\n",number1, number2);
    printf("Enter Your Answer: ");
    scanf("%d", &USER);
    if (USER == CPU) {
        printf("You are Right\n");
    }
    else {
        printf("You are wrong\n");
    }
    printf("Next Question");
}

You will also see you get the same question on each loop iteration, to fix this you need to reset the numbers inside the loop :)
 
Last edited:
yeh i tried that at first and failed so tried %d lol

retried it as suggested and works correctly thanks!,

i think i forgot the space between < and questions

now just got work out why the if statement doesnt work :)

keeps saying your right on every question..
 
Back
Top Bottom