Thread: External Errors & Unable to exit via switch case.

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    3

    External Errors & Unable to exit via switch case.

    Sup.

    The basics of the program is that:

    It will ask the user whether they want to: Pop, Push , Peek or Exit the program. When it comes to the Exit case:

    I can't figure out how do so( tried exit(1) & exit(0) using the internet hasn't brought me much luck either. Maybe a forum will


    I am getting these two errors also, without any idea how to fix it.

    "Error LNK2019 : Unresolved external symbol "int_cdcel peek(void)" (?peek@@YAHXZ)"


    Each applying to the functions below.


    "Error LNK1120: 3 Unresolved Externals"


    I'm not sure their is much more I can say about the problem.

    Hopefully that is more then enough to go on.

    Thankies

    Carl

    [code is below!]

    Code:
    #include "stdafx.h"#include <stdio.h>
    
    
    /* Libary Collection.*/
    
    
    #define MAX 10
    
    
    /* Define: The max size of what we are working with.*/
    
    
    int push (void);
    int  pop(void);
    int peek (void);
    
    
    
    
    /* Declaring the three global functions to be used in the program.*/
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        int stack[MAX];
        int top = 0;
        
        int ch; /* Choice variable for switch statement*/
        do
        {
         printf("\n\n\n\n1.\tPush\n2.\tPop\n3.\tDisplay\n4.\tExit\n"); 
         printf("\nEnter your choice: "); 
         scanf_s("%d", &ch); 
    
    
         /* Above prints out the users choices & prompts & scan for the users input and following accordingly*/
    
    
         switch(ch) 
         { 
         case 1: 
         push(); 
         break; 
     
         case 2: 
         pop(); 
         break; 
     
         case 3: 
         peek(); 
         break; 
    
    
         case 4:
        // I need to set case 4 : to exit the program: However, I can't figure out how too.
         break;
            
         default: 
     printf("\n\nInvalid entry. Please try again...\n"); 
     } 
     } while(ch!=4); 
     getchar(); 
    }
    
    
    
    
    
    
    /* I have 4 other errors on the program:
    
    
    "Error LNK2019 : Unresolved external symbol "int_cdcel peek(void)" (?peek@@YAHXZ)"
    
    
    Each applying to the functions below.
    
    
    &&
    
    
    "Error LNK1120: 3 Unresolved Externals"
    
    
    
    
    
    
    
    
    /* Three functions created: Peek, Push & Pop*/
    
    
     int pop(int &top, int stack[])
    {
        if(top > 0) 
        {
            top--; 
            return stack[top]; 
        }
        else
        {
        return -1;
        }
    }
    
    
    int push(int &top, int stack[], int value)
    
    
    {
        if(top < MAX)
        {
            stack[top] = value; 
            top++; 
            return 1;
        }
        else
        {
        return 0;
        }
    }
    
    
    int peek(int top, int stack[])
        {
            return stack[top];
        }
    Edit: I was also told that I could us if statements instead but, that still will remove the first errors

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Instead of these in-correct prototypes try using the correct ones.

    Code:
    int push (void);
    int  pop(void);
    int peek (void);
    I am guessing this is the correct ones.

    Code:
    int pop(int &top, int stack[]);
    int push(int &top, int stack[], int value);
    int peek(int top, int stack[]);
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    What you're doing now should work.

    If ch is 4 then the while(ch!=4) condition fails and the do while loop ends. You don't even need to handle it in the switch()

  4. #4
    Registered User Suhasa010's Avatar
    Join Date
    Sep 2013
    Posts
    10

    This should work

    Code:
    case 2: 
         pop();
    .
    .
    .
    Code:
    int pop(int &top, int stack[])
    where are you passing the parameters while calling the three functions in main() ??

    for exiting u should use the same exit(0) or exit(1) function and include stdlib.h or process.h header files. Without that also your program will work fine. :-)

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    3
    Quote Originally Posted by stahta01 View Post
    Instead of these in-correct prototypes try using the correct ones.

    Code:
    int push (void);
    int  pop(void);
    int peek (void);
    I am guessing this is the correct ones.

    Code:
    int pop(int &top, int stack[]);
    int push(int &top, int stack[], int value);
    int peek(int top, int stack[]);
    Tim S.
    It works however, only causing another error : That the functions called in the switch have too few arguments(trying to figure a solution for that now.)

    Quote Originally Posted by nonpuz View Post
    What you're doing now should work.

    If ch is 4 then the while(ch!=4) condition fails and the do while loop ends. You don't even need to handle it in the switch()
    Maybe so, however I'd like to be able to press "4" & it exits the program.

    Quote Originally Posted by Suhasa010 View Post
    Code:
    case 2: 
         pop();
    .
    .
    .
    Code:
    int pop(int &top, int stack[])
    where are you passing the parameters while calling the three functions in main() ??

    for exiting u should use the same exit(0) or exit(1) function and include stdlib.h or process.h header files. Without that also your program will work fine. :-)

    Exit works now thank you.

    I'm passing the parameters && input into the stack to be used, so I can push pop n peek into the array

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    If you still have a problem; I suggest posting the current code and error message after reading this link.

    Functions in C - Cprogramming.com

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Oct 2013
    Posts
    3
    Well

    Thank you for replies feels like i'm wasting more & more time to do this task so I will just try an alternate approach.

    Thankies for your time ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using read on tcp socket stream unable to exit loop.
    By dtow1 in forum Linux Programming
    Replies: 4
    Last Post: 03-04-2013, 01:57 PM
  2. Replies: 11
    Last Post: 08-25-2008, 12:01 PM
  3. Unable to explain these errors.
    By kris.c in forum C Programming
    Replies: 2
    Last Post: 02-20-2008, 02:55 AM
  4. Cannot get my program to exit properly (switch related)
    By Skarjak in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2005, 10:14 AM
  5. Exit from the switch-case
    By Ashutosh in forum C Programming
    Replies: 3
    Last Post: 06-30-2003, 10:03 AM