Link to home
Start Free TrialLog in
Avatar of brain12
brain12

asked on

What's Wrong?

I wrote a simple, short program that came up with 36 errors when I compiled it that I don't know how to fix.  Could someone look over my program and tell me how to fix it?
Here is the program:

#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <string.h>

int y;
loop1;

void main(void)
{
clrscr();
randomize();
for(loop1=0; loop1<(2+1); loop1++)
{
start();
}
int e;

printf("Do you want to play again?\n");
printf("1. Yes\n");
printf("2. no\n");
scanf("%d", &e);
if(e==1)
{
main();
}
if(e==2)
{
exit();
}
}

start()
{
y=random(2)+1;
check;
}

check()
{
for(loop2=0; loop2<(2+1); loop2++)
{
if(y==array[loop2])
{
start();
}
else
{
array[loop1]=y
quest();
}
}

quest()
{
if(y=1)
{
a();
}
if(y=2)
{
b();
}
}

a()
{
int a;
int b;

printf("What is the strongest type of bond?\n");
printf("1. metallic\n");
printf("2. network covalent\n");
printf("3. ionic\n");
printf("4. LDF\n");
scanf("%d", &b);
if(b==3)
{
printf("Correct!\n");
sleep(1);
}
else;
{
printf("wrong! It's ionic.\n");
sleep(1);
}
}

b()
{
int c;
int d;

printf("What is the hybridization of carbon in carbon tetrachloride?\n");
printf("1. sp\n");
printf("2. sp3\n");
printf("3. sp3d\n");
scanf("%d", &d);
if(d==2)
{
printf("Correct!\n");
sleep(1);
}
else;
{
printf("Wrong! It's sp3.\n");
sleep(1);
}
}

This is not really an assignment.  I'm creating this program as an example of different functions, etc.  I would appreciate it if anyone could help me to make sure that it completely correct and operable.  I really need the help because I am still very new at this and I had a lot of help from the teacher and a few friends.  I also need help because in the class all we have learned is printf() and basic syntax.  Thanks.
Avatar of wylliker
wylliker

It seems like this may be an assignment so I will try to give you some hints as opposed to fixing your code directly.


1. Are all of the variables that your program is using declared?

2. Consider passing variables as parameters instead of global variables - optional but you will have better code if you do.

3. Take a look in your main() function - the declaration of your variable - e - looks suspiciously like it is in the wrong place.

4. Look at some of your if statements - check to see whether you are testing the conditions properly.

5. Are you familiar with function prototypes?

6. You REALLY need to look at what you are doing when a user answers Yes to the question - "Do you want to play again?" - Danger lurks


It might so you some good to take a good look at the code you have written and do some work based on these suggestions.

If you have any questions - ask away - I, or many of the experts would be glad to help you along.


Avatar of ozo
"10320468.c", line 7: warning(1233): explicit type is missing ("int" assumed)
                  loop1;
                  ^

"10320468.c", line 17: error(1241): declaration may not appear after
          executable statement in block
                  int e;
                  ^

"10320468.c", line 36: error(1020): identifier "check" is undefined
                  check;
                  ^

"10320468.c", line 37: warning(1116): non-void function "start" (declared at
          line 33) should return a value
                  }
                  ^

"10320468.c", line 41: error(1020): identifier "loop2" is undefined
                  for(loop2=0; loop2<(2+1); loop2++)
                      ^

"10320468.c", line 43: error(1020): identifier "array" is undefined
                  if(y==array[loop2])
                        ^

"10320468.c", line 50: error(1065): expected a ";"
                  quest();
                  ^

"10320468.c", line 55: error(1065): expected a ";"
                  {
                  ^
Avatar of brain12

ASKER

Edited text of question.
Avatar of brain12

ASKER

Edited text of question.
Avatar of brain12

ASKER

Edited text of question.
Declare the variable "loop1" with a type, for a start--e.g. "int loop1;". Declaration of variables within a function can only be done at the beginning of an execution unit in C--e.g. directly after an opening {. The call to function "check" in "start" must include empty parentheses "check()", or the compiler will think it's a variable. The variable "loop2" used in the check() function is not declared anywhere. I could continue, but the errors reported by the C compiler should be telling you all this information--you'd be better off learning how to interpret them, because you'll be seeing them a lot, no matter how experienced you get!
Hello there brain12. Your program is a simple one. I made some modifications (not much though) and here it is. I tried to put comments where needed, and so all you have to do is to compile this one and it will run smoothly. although now, it doesn't have any trapping for input that will cause it to stop, I'll leave that to you, you can do it. I compiled and executed this one using Visual C++ ver 6.0, and I made suctomized clrscr() as well as sleep(). You can see it for yourself. Good day and good luck in studying C programming. Here is the modified code:


/**************************************/
#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <string.h>

int y;
int loop1;

void start();
void a();
void b();
void check(int in);
void clrscr();
void sleep(int y);
void quest(int in);

void main(void)
{
      int e;

      clrscr();
      //randomize();
      for(loop1=0; loop1<3; loop1++)
            start();

      printf("Do you want to play again?\n");
      printf("1. Yes\n");
      printf("2. no\n");
      scanf("%d", &e);

      if(e==1)
      {
            main();
      }

      if(e==2)
      {
            return;
      }
}



void start()
{
      y=(2+1);
            //formerly random(2)+1;
      check(y);
          //formerly check;
}


void check(int in)
{
      int loop2;
      int array[3];

      for(loop2=0; loop2<3; loop2++)
      {
            if(in==array[loop2])
            {
                  start();
            }

            else
            {
                  array[loop1]=in;
                  quest(in);
            }
      }
}


void clrscr()
{
      int x;
      for(x=0; x<10; x++)
            printf("\n");
}


void sleep(int y)
{
      int x;

      for(x=0; x<(y*10000); x++)
            ;
}


void quest(int in)
{
      if(in=1)
      {
            a();
      }
      if(in=2)
      {
            b();
      }
}


void a()
{
      //int a;
      int b;

      printf("What is the strongest type of bond?\n");
      printf("1. metallic\n");
      printf("2. network covalent\n");
      printf("3. ionic\n");
      printf("4. LDF\n");
      scanf("%d", &b);
      if(b==3)
      {
            printf("Correct!\n");
            sleep(1);
      }
      else
      {
            printf("wrong! It's ionic.\n");
            sleep(1);
      }
}



void b()
{
      //int c;
      int d;

      printf("What is the hybridization of carbon in carbon tetrachloride?\n");
      printf("1. sp\n");
      printf("2. sp3\n");
      printf("3. sp3d\n");
      scanf("%d", &d);
      if(d==2)
      {
            printf("Correct!\n");
            sleep(1);
      }
      else
      {
            printf("Wrong! It's sp3.\n");
            sleep(1);
      }
}

Hi again brain12. Your program is already complete, just missing some function prototypes, data types, etc. But content-wise, it's okay. So don't worry, you're doing great. About the progra I sent you, it's working (as you can probably see by now), but there is one thing I would be very happy to see you accomplish. That is, find the entry point for the input trapping, that would make the program stop and exit (or return for that matter). Because right now, it asks for those two questions, over and over. So you should designate a key (preferably the letter X) as the exit key. That means when the user presses the X key as answer to any of those two questions, the program terminates. Wouldn't that be great?,..c'mon, you can do it. Good luck and happy studying.

euy

Avatar of brain12

ASKER

Although this program may work on a C++ compiler, we are using the Borland turbo C 2.01 version in class and I am using it at home also so that my programs from home work at shchool.  If you could rewrite it so that it works for this compiler it would be much appreciated.  Thanks.
ASKER CERTIFIED SOLUTION
Avatar of AlexVirochovsky
AlexVirochovsky

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of brain12

ASKER

Thank you so much for your help. It ran beautifully.  I only wish I understood everything in the code.  Oh well.  I'l save that for another day.  Thanks again.