//WHATS WRONG WITH MY CODE?
#include %26lt;stdio.h%26gt;
#include %26lt;stdlib.h%26gt;
#include %26lt;time.h%26gt;
#include %26lt;string.h%26gt;
#include %26lt;conio.h%26gt;
int main ()
{
int operand[19];
int ans;
int a,b;
int operation;
/* initialize random seed: */
srand ( time(NULL) );
for(a = 0;a%26lt;10;a++)
{
operand[a] = rand() % 10 + 1;
operand[a+1]= rand() % 10 + 1;
b = rand() % 1 + 1;
if(b == 0) //THE OPERATION IS SUBTRACTION
{
printf("%d - %d = ",operand[a],operand[a+1]);
scanf(" %d ", %26amp;ans);
else //ELSE THE OPERATION IS ADDITION
printf(" %d + %d= ",operand[a],operand[a+1]);
scanf(" %d ", %26amp;ans);
}
}
getch();
return 0;
}
/* I WANT TO CREATE A PROGRAM THAT RANDOMIZE A NUMBER AND ALSO IF THE VARIABLE B WILL DECLARE 0 THE OPERATION IS SUBTRACTION BUT IF 1 THE OPERATION IS ADDITION, I THINK MY LOGIC IS RIGHT BUT THE PROGRAM ALWAYS SAY THAT THERE IS A SYNTAX ERROR BEFORE "ELSE" */
Help! C Program probs....?
Your syntax error is that you didn't close the if statement before else
You want
if(b == 0) //THE OPERATION IS SUBTRACTION
{
printf("%d - %d = ",operand[a],operand[a+1]);
scanf(" %d ", %26amp;ans);
} /* %26lt;%26lt;------------------------------ Add that */
else //ELSE THE OPERATION IS ADDITION
{ /* %26lt;%26lt;------------------------------ Add that */
printf(" %d + %d= ",operand[a],operand[a+1]);
scanf(" %d ", %26amp;ans);
}
} /* %26lt;%26lt;------------------------------ Delete that */
Also, I think you want
b = rand() % 2
As it is now
b = rand() % 1 + 1
You'll get a random number, divide it by 1, which will be remainder 0 every time, and then add 1, so it will be 1 every single time.
Reply:conio.h is not a standard header. make sure you have the necessary libs.
And, as other have rightly noticed, there should be braces around 'else //ELSEE THE OPERATION IS ADDITION'. That should look like '} else { //ELSE THE OPERATION IS ADDITION'.
Reply:Well,
if(b == 0) //THE OPERATION IS SUBTRACTION
{
printf("%d - %d = ",operand[a],operand[a+1]);
scanf(" %d ", %26amp;ans);
else //ELSE THE OPERATION IS ADDITION
printf(" %d + %d= ",operand[a],operand[a+1]);
scanf(" %d ", %26amp;ans);
}
Looks wrong. The { and } that should encapsulate the "if" part of the statement is including the "else" part as well, which won't work. Try
if(b == 0) //THE OPERATION IS SUBTRACTION
{
printf("%d - %d = ",operand[a],operand[a+1]);
scanf(" %d ", %26amp;ans);
}
else //ELSE THE OPERATION IS ADDITION
{
printf(" %d + %d= ",operand[a],operand[a+1]);
scanf(" %d ", %26amp;ans);
}
instead.
Reply:The format of the if statement is
if ()
{
}
else
{
}
I think you have
if ()
{
else
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment