Errors in Code i am unfamiliar with, Please Help

This is a discussion on Errors in Code i am unfamiliar with, Please Help within the C Programming forums, part of the General Programming Boards category; I had to write a program for a simple math game, i wrote the program it had a few errors ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    4

    Errors in Code i am unfamiliar with, Please Help

    I had to write a program for a simple math game, i wrote the program it had a few errors i fixed the errors, or so i thought, the program compiled but then when i went to run the exe file i got 3 error LNK2019: unresolved external symbols _If
    _mult
    _sub
    i have no idea how to resolve these 3 errors nor what the error is referring to, an insight would be greatly appreciated.
    Here is my Code for the Program.

    Code:
    #include <stdio.h>
    
    int sub ( int x, int y );
    int div ( int x, int y );
    int mult ( int x, int y );
    int add ( int x, int y );
    int func;
    int s;
    int d;
    int m;
    int a;
    int x;
    int y;
    float num;
    int q;
    int yes;
    int no;
    int Yes;
    int No;
    
    int main()
    {	
    	{
    	  printf("Please enter a number: /n 1 To Play/n 2 Instructions/n 3 Exit/n");
    	  scanf("%f", &num);
    	}
    	   {
    	    If (num == 1) ;
    	    printf("Press a to Add/n s to subtract/n d to divide/n m to multiple/n");
    	    scanf("%d", &func );
    	   }	
    	
    		{
    		 If (func == s);
      		  printf( "Please input two numbers to be subtracted: " );
      		  scanf( "%d", &x );
      		  scanf( "%d", &y );
      		  printf( "The subtraction of your two numbers is %d\n", sub( x, y ) );
      		  getchar(); 
    		}
    
    		{
    		  int sub (int x, int y);
    		}
    		{
     		  return x - y;
    		} 
    		{
    		 If (func == m);
      		  printf( "Please input two numbers to be multiplied: " );
     /* 50 */	  scanf( "%d", &x );
      		  scanf( "%d", &y );
      		  printf( "The product of your two numbers is %d\n", mult( x, y ) );
      		  getchar(); 
    		}
    		{
    		 int mult (int x, int y);
    		}
    		{
      		 return x * y;
     /* 60 */	}
    		{
    		 If (func == d); 
      		  printf( "Please input two numbers to be divided: " );
      		  scanf( "%d", &x );
      		  scanf( "%d", &y );
      		  printf( "The division of your two numbers is %d\n", div( x, y ) );
      		  getchar(); 
    		}
    		{
     /* 70 */	  int div (int x, int y);
    		}
    		{
    		  return x / y;
    		}
    		{
    		 If (func == a);
      		  printf( "Please input two numbers to be added: " );
     		  scanf( "%d", &x );
      		  scanf( "%d", &y );
     /* 80 */ 	  printf( "The addition of your two numbers is %d\n", div( x, y ) );
      		  getchar(); 
    		}
    		{
    		 int add (int x, int y);
    		}
    		{
     		 return x + y;
    		}
    	  {
    	   If (num == 2); 
    	     printf("Instructions are as Follows:/n You will choose any two numbers you like/n You will then choose an operation to be performed/n enter a s m or d./n a is addition, s is subtraction, m is multiplication, and d is division./n After your choose your operation you will receive your total./n You can then choose to play again or exit the game.");
    	     printf("Now Would you like to play the Game?(yes or no): ");
                 scanf("%d", &q);
    	  }
    		{
    		 If (q == yes || Yes);
    		  printf("Press a to Add/n s to subtract/n d to divide/n m to multiple/n");
    	    	  scanf("%d", &func );
    		}
    		{
    		 If (q == no || No);
    		  return 0;
    		}
    	 {
    	  If (num == 3);
    	
    	  return 0;
    	 }
    	
    }

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    "If" should be "if"

    And all your if statements have semicolons after them, which means they don't actually do anything, and you can't do comparisons like this:
    Code:
    if (q == no || No)
    , so it's time to go back to your books/notes and reread them.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    haha thank you, i am currectly teaching myself with online tutorials i am still very very shaky with it, my teacher gave us about a 10 minute overview and then gave us the assignment.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    okay so this is what i have now.... i still dont know what the errors mentioned above are

    Code:
    #include <stdio.h>
    
    int sub ( int x, int y );
    int div ( int x, int y );
    int mult ( int x, int y );
    int add ( int x, int y );
    int func;
    int s;
    int d;
    int m;
    int a;
    int x;
    int y;
    float num;
    int q;
    int yes;
    int no;
    
    
    int main()
    {	
    	{
    	  printf("Please enter a number: /n 1 To Play/n 2 Instructions/n 3 Exit/n");
    	  scanf("%f", &num);
    	}
    	   {
    	    if (num == 1) 
    	    printf("Press a to Add/n s to subtract/n d to divide/n m to multiple/n");
    	    scanf("%d", &func );
    	   }	
    	
    		{
    		 if (func == s)
      		  printf( "Please input two numbers to be subtracted: " );
      		  scanf( "%d", &x );
      		  scanf( "%d", &y );
      		  printf( "The subtraction of your two numbers is %d\n", sub( x, y ) );
      		  getchar(); 
    		}
    
    		{
    		  int sub (int x, int y);
    		}
    		{
     		  return x - y;
    		} 
    		{
    		 if (func == m)
      		  printf( "Please input two numbers to be multiplied: " );
     /* 50 */	  scanf( "%d", &x );
      		  scanf( "%d", &y );
      		  printf( "The product of your two numbers is %d\n", mult( x, y ) );
      		  getchar(); 
    		}
    		{
    		 int mult (int x, int y);
    		}
    		{
      		 return x * y;
     /* 60 */	}
    		{
    		 if (func == d) 
      		  printf( "Please input two numbers to be divided: " );
      		  scanf( "%d", &x );
      		  scanf( "%d", &y );
      		  printf( "The division of your two numbers is %d\n", div( x, y ) );
      		  getchar(); 
    		}
    		{
     /* 70 */	  int div (int x, int y);
    		}
    		{
    		  return x / y;
    		}
    		{
    		 if (func == a)
      		  printf( "Please input two numbers to be added: " );
     		  scanf( "%d", &x );
      		  scanf( "%d", &y );
     /* 80 */ 	  printf( "The addition of your two numbers is %d\n", div( x, y ) );
      		  getchar(); 
    		}
    		{
    		 int add (int x, int y);
    		}
    		{
     		 return x + y;
    		}
    	  {
    	   if (num == 2)
    	     printf("Instructions are as Follows:/n You will choose any two numbers you like/n You will then choose an operation to be performed/n enter a s m or d./n a is addition, s is subtraction, m is multiplication, and d is division./n After your choose your operation you will receive your total./n You can then choose to play again or exit the game.");
    	     printf("Now Would you like to play the Game?(yes or no): ");
                 scanf("%d", &q);
    	  }
    		{
    		 if (q == yes);
    		  printf("Press a to Add/n s to subtract/n d to divide/n m to multiple/n");
    	    	  scanf("%d", &func );
    		}
    		{
    		 if (q == no)
    		  return 0;
    		}
    	 {
    	  if (num == 3)
    	
    	  return 0;
    	 }
    	
    }

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    Well, you prototyped the mult, div, add and sub functions...did you expect them to just magically appear? Well the compiler (actually, the linker to be specific) doesn't...it expects you to provide them, and when you don't -- or provide them differently that prototyped -- what's the error? You guessed it...Frank Stallone.

    Or "unresolved external symbol" for each function it can't find or match up to its prototype.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    im sorry Rags, thank you for your help, and i dont want to frustrate or annoy you.

    But i dont understand really. How did i prototype them?

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    14

    Allow me to try to help

    Quote Originally Posted by msween21 View Post
    im sorry Rags, thank you for your help, and i dont want to frustrate or annoy you.

    But i dont understand really. How did i prototype them?

    I am very new myself, but I think I can give you some help. I am merely learning from a book, but perhaps my knowledge is useful.

    There are very many errors in your code. The first thing I noticed was that you declared variables outside of a function.
    Code:
    #include <stdio.h>
    
    int sub ( int x, int y );
    int div ( int x, int y );
    int mult ( int x, int y );
    int add ( int x, int y );
    int func;
    int s;
    int d;
    int m;
    int a;
    int x;
    int y;
    float num;
    int q;
    int yes;
    int no;
    
    
    int main()
    Notice how all those variables are declared before invoking main()? As far I know, variables declarations are function specific (this has nothing to do with using variables from one function as input for another one).

    The next thing I noticed was that you never defined what your other functions, like "int sub(int x, int y)" etc. do with their input. You prototyped them (gave them a name, and defined what kind of input they take) but never wrote them (or did you just not post them?). Actually, it looks like you tried to define them, but did it inside main(). This won't work at all.

    Also, you mixed data type quite a bit:
    Code:
    ...
      scanf("%d", &q);
    	  }
    		{
    		 if (q == yes);
    		  printf("Press a to Add/n s to subtract/n d to divide/n m to multiple/n");
    	    	  scanf("%d", &func );
    		}
    ...
    You are asking for alphabetic input and reading it as an integer. I believe this can cause serious problems. Also, "yes" and "no" have never been given values (at least I couldn't find where you did it) so comparing q to them is meaningless.

    As the last thing, I really couldn't understand your methodology when using braces {}. For example, the statement "if (q==yes)" (well, the first issue is that it is followed by a semicolon, meaning nothing will happen if q is equal to yes) only applies to the printf function directly following it (or it would if you took out the semicolon). You would need to open braces right before the printf and close right after ...&func); to join the printf and scanf as part of the if statement.
    Code:
    ...
    if (q == yes)
      {
        printf("Press a to Add/n s to subtract/n d to divide/n m to multiple/n");
        scanf("%d", &func );
      }
    ...
    My advice to you is to put this program aside for a little while, read a bit more about C, and then write some programs that are much shorter and simpler. These will be so much easier to debug, and you will be able to apply what you learn directly to the one you are trying to write right now. For example, it might be educational to write a program that uses a function other than main() for a completely trivial task, so as to learn the syntax of multiple functions, while not bothering with the logic of a larger program.

    I hope this was of help,
    Good luck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 02:29 PM
  3. RAM/Swap Memory Code Errors
    By ghe1 in forum Linux Programming
    Replies: 2
    Last Post: 04-01-2002, 07:37 AM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21