English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

//WHATS WRONG WITH MY CODE?

#include
#include
#include
#include
#include
int main ()
{
int operand[19];
int ans;
int a,b;
int operation;
/* initialize random seed: */


srand ( time(NULL) );
for(a = 0;a<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 ", &ans);
else //ELSE THE OPERATION IS ADDITION
printf(" %d + %d= ",operand[a],operand[a+1]);
scanf(" %d ", &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" */

2007-12-20 22:33:50 · 4 answers · asked by AngryProgrammer 1 in Computers & Internet Programming & Design

4 answers

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 ", &ans);
} /* <<------------------------------ Add that */
else //ELSE THE OPERATION IS ADDITION
{ /* <<------------------------------ Add that */
printf(" %d + %d= ",operand[a],operand[a+1]);
scanf(" %d ", &ans);
}
} /* <<------------------------------ 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.

2007-12-20 22:43:19 · answer #1 · answered by Zurahn 4 · 0 1

Well,

if(b == 0) //THE OPERATION IS SUBTRACTION
{
printf("%d - %d = ",operand[a],operand[a+1]);
scanf(" %d ", &ans);
else //ELSE THE OPERATION IS ADDITION
printf(" %d + %d= ",operand[a],operand[a+1]);
scanf(" %d ", &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 ", &ans);
}
else //ELSE THE OPERATION IS ADDITION
{
printf(" %d + %d= ",operand[a],operand[a+1]);
scanf(" %d ", &ans);
}

instead.

2007-12-20 22:40:44 · answer #2 · answered by David W 6 · 1 1

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'.

2007-12-21 00:00:14 · answer #3 · answered by Anonymous · 0 0

The format of the if statement is
if ()
{
}
else
{
}

I think you have
if ()
{
else
}

2007-12-20 22:45:09 · answer #4 · answered by AnalProgrammer 7 · 0 0

fedest.com, questions and answers