Problem with Function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sonic
    New Member
    • Nov 2006
    • 40

    Problem with Function

    I have a program for class that I must write using functions. Before I continued with the program I wanted to see if I could get my initial call to my first function working. Have not had luck though. Here are the error messages I'm recieving:

    "error LNK2019: unresovled external symbol_main referenced in function_tmainC RTStartup"

    "fatal error LNK1120: 1 unresolved externals"

    Here's a copy of my code:

    Code:
    #include <iostream>
    using namespace std;
    
    // Function prototypes
    
    void printError();
    void getInput(int&, char&);
    void printLastLine(int, char);
    int printPattern(int, char);
    
    int main()
    {
    	int numRows;
    	char typeChar;
    
    	getInput();
    //	printPattern(inputNum, inputChar);
    //	printLastLine(inputNum, inputChar);
    
    	return 0;
    }
    
    //************************************************************************
    // Definition of getInput which obtains user info on how to build pattern*
    //************************************************************************
    
    void getInput(int&, char&)
    {	
    	int inputNum = 0;
    	char inputChar = 0;
    
    	cout << "How many rows do you want in the pattern (even numbers only: 2-14)?\n";
    	cin >> inputNum;
    	cout << "What character do you want the pattern made of (select: * + # or $)?\n";
    	cin >> inputChar;
    	
    	printError(inputNum&, inputChar&);
    	
    	return;
    }
    
    //************************************************************************
    // Definition of printError which acts as input validation when program  *
    // is obtaining number and type of pattern to be used by user            *
    //************************************************************************
    
    void printError ()
    {
    
    	if ((inputNum < 2) || (inputNum > 14) ||(inputNum%2 != 0))
    		cout << "Invalid number of rows. Please retry.";
    		getinput();
    
    		// Code here for 2nd validation (How to compare multiple literal strings?)
    
    	return;
    }
    What am I doing wrong with the syntax? Thanks for any help!
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    sonic-

    I'm pretty sure it's that you define getInput() in the prototype to pass an int and a char, but when you call it in main, you don't pass it anything - you just instantiate them first thing in the function. Try passing those values.

    Comment

    • teddarr
      New Member
      • Oct 2006
      • 143

      #3
      OK, I got the getInput() function working and tested to make sure the inputs are passed to main(). Pay close attention to your function prototypes, function calls and function definitions.

      One tip I was give is that once you have written your function and have the definition written, copy and paste in just like it is to the function prototype section. While all of this info is not required, it sure cuts down on confusion.

      I didn't touch printerror() since you didn't mention any problems with it.

      #include <iostream>
      using namespace std;

      // Function prototypes

      void printError();
      void getInput(int& inputNum, char& inputChar); //copy and paste from function definition, line 27
      void printLastLine(i nt, char);
      int printPattern(in t, char);

      int main()
      {
      int numRows;
      char typeChar;

      getInput(numRow s, typeChar);
      // printPattern(in putNum, inputChar);
      // printLastLine(i nputNum, inputChar);

      return 0;
      }

      //*************** *************** *************** ***** *************** *******
      // Definition of getInput which obtains user info on how to build pattern*
      //*************** *************** *************** ***** *************** *******

      void getInput(int& inputNum, char& inputChar)
      {
      //deleted these declarations as they are now passed from main()

      cout << "How many rows do you want in the pattern (even numbers only: 2-14)?\n";
      cin >> inputNum;
      cout << "What character do you want the pattern made of (select: * + # or $)?\n";
      cin >> inputChar;

      //printError(inpu tNum, inputChar); //change on this line

      // return;//no return statement needed on a void function
      }

      Comment

      • sonic
        New Member
        • Nov 2006
        • 40

        #4
        Initially what I need the program to do is just ask the user for the information. I do need the function to pass entered info to another function but I have nothing initially to pass until user answers questions in getInput function. Am I going about this wrong?

        Comment

        • sonic
          New Member
          • Nov 2006
          • 40

          #5
          Sorry, meant to post code.

          Code:
          #include <iostream>
          using namespace std;
          
          // Fution prototypes
          
          void printError();
          void getInput(int&, char&);
          void printLastLine(int, char);
          int printPattern(int, char);
          
          int main()
          {
          	int inputNum = 0;
          	char inputChar = 0;
          
          	getInput(inputNum, inputChar);
          //	printPattern(inputNum, inputChar);
          //	printLastLine(inputNum, inputChar);
          
          	return 0;
          }
          
          //************************************************************************
          // Definition of getInput which obtains user info on how to build pattern*
          //************************************************************************
          
          void getInput(int&, char&)
          {	
          
          	cout << "How many rows do you want in the pattern (even numbers only: 2-14)?\n";
          	cin >> inputNum;
          	cout << "What character do you want the pattern made of (select: * + # or $)?\n";
          	cin >> inputChar;
          	
          	printError(inputNum&, inputChar&);
          	
          }

          Comment

          • teddarr
            New Member
            • Oct 2006
            • 143

            #6
            I think you need to look at the call for printerror. I've never seen a call like that. I don't think you use the '&' character in a call.

            Comment

            • sivadhas2006
              New Member
              • Nov 2006
              • 142

              #7
              Originally posted by sonic
              I have a program for class that I must write using functions. Before I continued with the program I wanted to see if I could get my initial call to my first function working. Have not had luck though. Here are the error messages I'm recieving:

              "error LNK2019: unresovled external symbol_main referenced in function_tmainC RTStartup"

              "fatal error LNK1120: 1 unresolved externals"

              Here's a copy of my code:

              Code:
              #include <iostream>
              using namespace std;
              
              // Function prototypes
              
              void printError();
              void getInput(int&, char&);
              void printLastLine(int, char);
              int printPattern(int, char);
              
              int main()
              {
              	int numRows;
              	char typeChar;
              
              	getInput();
              //	printPattern(inputNum, inputChar);
              //	printLastLine(inputNum, inputChar);
              
              	return 0;
              }
              
              //************************************************************************
              // Definition of getInput which obtains user info on how to build pattern*
              //************************************************************************
              
              void getInput(int&, char&)
              {	
              	int inputNum = 0;
              	char inputChar = 0;
              
              	cout << "How many rows do you want in the pattern (even numbers only: 2-14)?\n";
              	cin >> inputNum;
              	cout << "What character do you want the pattern made of (select: * + # or $)?\n";
              	cin >> inputChar;
              	
              	printError(inputNum&, inputChar&);
              	
              	return;
              }
              
              //************************************************************************
              // Definition of printError which acts as input validation when program  *
              // is obtaining number and type of pattern to be used by user            *
              //************************************************************************
              
              void printError ()
              {
              
              	if ((inputNum < 2) || (inputNum > 14) ||(inputNum%2 != 0))
              		cout << "Invalid number of rows. Please retry.";
              		getinput();
              
              		// Code here for 2nd validation (How to compare multiple literal strings?)
              
              	return;
              }
              What am I doing wrong with the syntax? Thanks for any help!
              Hi,

              Can I know which IDE you are using to write the program?
              Because this may be due to the IDE Settings.

              Regards,
              M.Sivadhas.

              Comment

              • kirubanantham
                New Member
                • Nov 2006
                • 8

                #8
                Hai Sonic,
                As per your requirement, you are just using getInput() function to get some value. you are not doing anything with parameters, so i think you can just make this function with out any parameters like void getInput( void ); i feel this may do.

                cheers,
                kiruba.

                Comment

                • sonic
                  New Member
                  • Nov 2006
                  • 40

                  #9
                  I am using Microsoft Visual Studios 2005. I think my problem is that I really do not understand how functions work. Let me do some leg work and I'll get back when I have a little better understanding and have started over on my code. Thank you for help so far!

                  Comment

                  • sonic
                    New Member
                    • Nov 2006
                    • 40

                    #10
                    Okay, after two days of trying to figure out the inner workings of functions, I'm starting to gain some. Before I write the code for my printPattern function though I was hoping someone might be able to help me with a logic problem I'm having. It is in my printError function. I've got my input validation working for integers a user may enter, but for some reason I cannot get the function to correctly decide on whether or not the character entered is valid or not (* + # or $). Here is my code so far:

                    Code:
                    // PFunction(RDE).cpp
                    // 11/26/06
                    // Program allows user to select number and type of pattern to be printed to screen
                    
                    #include <iostream>
                    using namespace std;
                    
                    // Function prototypes
                    
                    void getInput(int&, char&);
                    void printError(int&, char&);
                    int printPattern(int, char);
                    
                    int main()
                    {
                    	int numRows = 0;
                    	char typeChar = 0;
                    
                    	getInput(numRows, typeChar);
                    	
                    	return 0;
                    }
                    
                    //************************************************************************
                    // Definition of getInput which obtains user info on how to build pattern*
                    //************************************************************************
                    
                    void getInput(int& rows, char& characterSelect)
                    {
                    	
                    	cout << "How many rows do you want in the pattern (even numbers only: 2-14)? ";
                    	cin >> rows;
                    	cout << "What character do you want the pattern made of (select from: * + # or $)? ";
                    	cin >> characterSelect;
                    	printError(rows, characterSelect);
                    
                    }
                    
                    //************************************************************************
                    // Definition of printError which determines if user has entered         *
                    // acceptable input.                                                     *
                    //************************************************************************
                    
                    void printError(int& rows, char& characterSelect)
                    {
                    
                    	if ((rows < 2) || (rows > 14) || (rows%2 != 0))
                    	{
                    		cout << "\nInvalid number of rows. Please retry.\n";
                    		cout << "\n";
                    		getInput(rows, characterSelect);
                    	}
                    	else if ((characterSelect != '*') || (characterSelect != '+') || (characterSelect != '#') || (characterSelect != '$'))
                    	{
                    		cout << "\nInvalid character. Please retry using one of the four characters given.\n";
                    		cout << "\n";
                    		getInput(rows, characterSelect);
                    	}
                    }
                    
                    //************************************************************************
                    // Definition of printPattern displays pattern based on user entered data*
                    //************************************************************************
                    
                    int printPattern()
                    Thanks for everyone's continued help and input.

                    Comment

                    Working...