Solved

What's Wrong?

Posted on 2000-03-30
11
301 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:brain12
11 Comments
 
LVL 4

Expert Comment

by:wylliker
ID: 2671690
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.


0
 
LVL 84

Expert Comment

by:ozo
ID: 2671798
"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 ";"
                  {
                  ^
0
 

Author Comment

by:brain12
ID: 2671955
Edited text of question.
0
 

Author Comment

by:brain12
ID: 2671957
Edited text of question.
0
 

Author Comment

by:brain12
ID: 2671961
Edited text of question.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 12

Expert Comment

by:pjknibbs
ID: 2672151
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!
0
 

Expert Comment

by:euy
ID: 2672189
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);
      }
}

0
 

Expert Comment

by:euy
ID: 2672200
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

0
 

Author Comment

by:brain12
ID: 2673081
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.
0
 
LVL 14

Accepted Solution

by:
AlexVirochovsky earned 100 total points
ID: 2673823
You code may be something as:
#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#define MAX1  3
void a(void);
void b(void);
void start(int loop1);
void quest(int y);

void main(void)
{
  int loop1 = 0;
  clrscr();
  randomize();
  while (loop1 <= MAX1)
    {
      int e;
      printf("Do you want to play again?\n");
      printf("1. Yes\n");
      printf("2. no\n");
      scanf("%d", &e);
      if(e==1)
        start(loop1);
      else
        break;
      loop1++;
    }
}

void start(int loop1)
{
   int y=random(MAX1);
   if (y == loop1)
     quest(y);
}
void quest(int y)
{
   if (y == 1)
     a();
   else if (y == 2)
     b();
}

void a(void)
{
  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! Press any key\n");
 else
     printf("wrong! It's ionic.Press any key\n");
  getch();
}

void b(void)
{
  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! Press any key\n");
  else;
    printf("Wrong! It's sp3.Press any key\n");
}
I hope, it helps. Alex
0
 

Author Comment

by:brain12
ID: 2674185
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.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now