Trouble with passing variables into a function

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

    Trouble with passing variables into a function

    I am having trouble passing two variable types into my printPattern function. I need to pass rows and characterSelect from my getInput function into my printPattern function. I keep getting a "printPatte rn function does not take 0 arguments" error. I'm pretty sure this means nothing is being passed to my printPattern function, but I can't figure out why. Does anyone have any suggestions? Here's my code:

    Code:
    // 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);
    	printPattern(int& rows, char& characterSelect);
    	
    	
    	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 printPatter(int& rows, char& characterSelect)
    {
    	int halfValue = rows / 2;
    
    	return 0;
    }
    Thanks for help any may have. For me, understanding functions has been one of the hardest things to master in C++ so far.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    sonic-

    I see 4 things right off the top of my head - first of all:
    Code:
    int printPatter(int& rows, char& characterSelect)
    {
    	int halfValue = rows / 2;
    
    	return 0;
    }
    You pass printPatter() a char*, but you never do anything with it in the function. If you can (if you're not going to use it in a future draft/implementation) , remove the char reference. And you mis-spelled printPattern in the definition - you forgot the 'n' at the end. (I think you might be having some trouble with the '&'s - your prototype, call, and function declarations don't match)

    Second, your prototype
    Code:
    int printPatter(int& rows, char& characterSelect);
    The int at the beginning is what it is going to return. You say it is going to return an integer, but you don't get that integer in your main():

    Code:
    	printPattern(int& rows, char& characterSelect);
    That should be saved to some sort of integer variable, so it can return its value

    Finally, you declare two new variables in the functioncall. When you are in your main() you just want to pass it the variables you already have, numRow and typeChar, otherwise you are creating two new variables with "junk" in them (whatever was in the memory space at the place the compiler decided to give to those variables), and passing those values.

    try:
    Code:
    int main()
    {
        int numRows = 0;
        char typeChar;
        int result = 0;
    
        getInput(numRows, typeChar);
        result = printPattern(&numRows);
    
        return 0;
    }
    
    int printPattern(int numRows)
    {
    // implementation here
    }
    Try that and see what happens.

    Comment

    • sicarie
      Recognized Expert Specialist
      • Nov 2006
      • 4677

      #3
      sonic-

      Making the changes I mentioned above, and removing all the '&'s in the prototype above main, the call in main, and the function declaration below main, it should compile and run.

      Comment

      • sonic
        New Member
        • Nov 2006
        • 40

        #4
        I'm sorry Sicarie. You should be very familiar with this code as you helped me with the original program. This new program is simply an extension of that using functions. I didn't mean to post my code with the unfinished printPattern function for any to see. What I am trying to pass to the printPattern function is the input I recieve from the user in my getInput function. However, I don't think that information is being passed back to my main which is why I set up int numRows & char typeChar. Here's my newest code:

        Code:
        // 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;
        	int countCharacters;
        
        	getInput(numRows, typeChar);
        	printPattern(int rows, char characterSelect);
        	
        	
        	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()
        {
        	int halfValue = rows / 2;
        	int countCharacters = 0;
        
        for (int row = 0; row <= halfValue; row++)
        	{	
        		for(int character = 0; character < row; character++)
        			cout << characterSelect;
        			countCharacters += 1;
        			cout << "\n";
        	}
        	
        	for (int row2 = halfValue; row2 >= 0; row2--)
        	{
        		for(int character2 = 0; character2 < row2; character2++)
        			cout << characterSelect;
        			countCharacters += 1;
        			cout << "\n";
        	}
        
        	return countCharacters;
        }
        Hope this helps a little. Thank you for the quickness of your last reply.

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          Originally posted by sonic
          Code:
          #include <iostream>
          using namespace std;
          
          // Function prototypes
          
          void getInput(int&, char&);
          void printError(int&, char&);
          int printPattern(int, char); // Look!
          
          // ...
          
          //************************************************************************
          // Definition of printPattern displays pattern based on user entered data*
          //************************************************************************
          
          int printPattern() // Look!
          {
          	int halfValue = rows / 2;
          	int countCharacters = 0;
          
          for (int row = 0; row <= halfValue; row++)
          	{	
          		for(int character = 0; character < row; character++)
          			cout << characterSelect;
          			countCharacters += 1;
          			cout << "\n";
          	}
          	
          	for (int row2 = halfValue; row2 >= 0; row2--)
          	{
          		for(int character2 = 0; character2 < row2; character2++)
          			cout << characterSelect;
          			countCharacters += 1;
          			cout << "\n";
          	}
          
          	return countCharacters;
          }
          Simple; currently, your function header and prototype don't match up. You are attempting to pass two variables to one of two functions; one that doesn't accept arguments, and one without a definition. Change your header to reflect the arguments it is being passed.

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by Ganon11
            Simple; currently, your function header and prototype don't match up. You are attempting to pass two variables to one of two functions; one that doesn't accept arguments, and one without a definition. Change your header to reflect the arguments it is being passed.
            And assign a variable to pick up that int that is returned.

            Comment

            • sonic
              New Member
              • Nov 2006
              • 40

              #7
              Here is my new code, still having problems however.

              Code:
              // 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;
              	int countCharacters = 0;
              
              	getInput(numRows, typeChar);
              	countCharacters = printPattern(int rows, char characterSelect);
              	
              	
              	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(int rows, char characterSelect)
              {
              	int halfValue = rows / 2;
              	int countCharacters = 0;
              
              for (int row = 0; row <= halfValue; row++)
              	{	
              		for(int character = 0; character < row; character++)
              			cout << characterSelect;
              			countCharacters += 1;
              			cout << "\n";
              	}
              	
              	for (int row2 = halfValue; row2 >= 0; row2--)
              	{
              		for(int character2 = 0; character2 < row2; character2++)
              			cout << characterSelect;
              			countCharacters += 1;
              			cout << "\n";
              	}
              
              	return countCharacters;
              }
              I keep getting three errors which state: "syntax error: ')' ", "syntax error: int should be preceded by ')' ", and last "printPatte rn: function does not take 0 arguments". Here is the line of code all three of these errors appear on:

              Code:
              	countCharacters = printPattern(int rows, char characterSelect);
              I still don't think I'm properly passing the info user has entered in my getInput function.

              Thanks again for the help.

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                Remove the int part in int rows, and the char part in char characterSelect when you call the function in your main - i.e. the function call should be similar to the getInput call you have previously.

                Comment

                • sonic
                  New Member
                  • Nov 2006
                  • 40

                  #9
                  Ah yes! Thank you so much Gannon, that did it! One more function and I've got the program working. I've been stuck on that for the last hour or so, many thanks to you and Sicarie!

                  Comment

                  Working...