Default Arguments Help

Hi guys,

I have a program that try's to use default parameters (arguments) as a way for a function to accept varied arguments, however, I keep getting errors:-

main.cpp: In function ‘int main()’:
main.cpp:17: error: too few arguments to function ‘void PrintLine(char, int, int)’
main.cpp:25: error: at this point in file
main.cpp: At global scope:
main.cpp:30: error: default argument for ‘char character’ has type ‘const char [2]’

This is my code:-

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

#include <iostream>
void PrintLine (char character, int timesToBePrinted, int noOflines);
using namespace std;

int main()
{
   cout << "Enter a character and two integers: \n";
   cin.get();
   PrintLine();
   return 0;
}

void PrintLine(char character = "Z", int timesToBePrinted = 1, int noOfLines = 1)
{
   for (int counter = 0; counter < noOfLines; counter++)
   {
      for (int count = 0; count < timesToBePrinted; count++)
      {
         cout << character;
      }
      cout << "\n";
   }
}


Any help is much appreciated.
Error on Line 14: you need single quotes around your Z
You could also just use function overloading to allow omission or inclusion of extra arguments.
Thanks for your help!

Am still getting the following errors:-

Assignment_4_Question3.cpp: In function ‘int main()’:
Assignment_4_Question3.cpp:18: error: too few arguments to function ‘void PrintLine(char, int, int)’
Assignment_4_Question3.cpp:24: error: at this point in file
Why pass variables at all if you are going to set defaults to all of them

instead write two functions.. one that declares local variables as defaults and takes no parameters and one function w/SAME NAME that does take parameters?

But if you want you could try setting the default values in the prototype instead of the definition
Last edited on
Thanks again efficacious!

Actually I can't use overloading; am asked to write one function that can take a variable number of arguments using default arguments! I can't use any standard library headers as well :(

Thanks again
Ic.. setting defaults in the prototype work for ya then?
Interesting!

Having the defaults in the prototype actually works, however, it doesn't adapt depending on the input provided; the output is always the default, even though I change the input!

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

#include <iostream>
using namespace std;

void PrintLine(char character = 'Z', int timesToBePrinted = 1, int noOflines = 1);

int main()
{
   cout << "Enter a character and two integers: \n";
   cin.get();
   PrintLine();
   return 0;
}

void PrintLine(char character, int timesToBePrinted, int noOfLines)
{
   for (int counter = 0; counter < noOfLines; counter++)
   {
      for (int count = 0; count < timesToBePrinted; count++)
      {
         cout << character;
      }
      cout << "\n";
   }
}


Can you see something?
How are you passing the result you get from Line 10 into Line 11?
I thought cin.get() would read whatever is provided in the input stream, or am I lost here?!?!
So where is it stored? What variable? I can't seem to find it anywhere.
Am a bit confused here! Firstly the problem is that if I used variables then I have to prompt the user to input some numbers; now if I prompt the user using "cin >> a;" for example then I am defaulting to one input variable. However, I would like the user to have the choice of just inputting 0,1,2 or 3 variables and using default arguments in the function, the appropriate calls are made. I am not allowed to use strings/arrays/library functions and IF statements within the function itself.

This is my code thus far, although I don't think am solving the problem (again cin is asking for three input variables, if I don't supply cin with three variables then the program doesn't run)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

#include <iostream>
using namespace std;
const int PROMPTS = 5;

void PrintLine(char character = 'Z', int timesToBePrinted = 1, int noOflines = 1);

int main()
{
   cout << "Enter a character and two integers: \n";
   
   for (int counter = 0; counter < PROMPTS; counter++)
   {
      char c;
      int a, b;
      cin >> c >> a >> b;
      
      //Zero parameters
      if (c == ' ' && a == 0 && b == 0)
         PrintLine();
      
      //One Parameter - could be character or timesToBePrinted or noOflines
      else if (c != ' ' && a == 0 && b == 0)//character provided
         PrintLine(c);
      else if (c == ' ' && a != 0 && b == 0)//timesToBePrinted provided
         PrintLine(a);
      else if (c == ' ' && a == 0 && b != 0)//noOfLines provided
         PrintLine(b);
      
      //Two Parameters
      else if (c != ' ' && a != 0 && b == 0)//character and timesToBePrinted provided
         PrintLine(c,a);
      else if (c != ' ' && a == 0 && b != 0)//character and noOfLines provided
         PrintLine(c,b);
      else if (c == ' ' && a != 0 && b != 0)// timesToBePrinted and noOfLines provided
         PrintLine(a,b);
         
         //Three Parameters
      else if (c != ' ' && a != 0 && b != 0)
         PrintLine(c, a, b);
      return 0;
   }
}

void PrintLine(char character, int timesToBePrinted, int noOfLines)
{
   for (int counter = 0; counter < noOfLines; counter++)
   {
      for (int count = 0; count < timesToBePrinted; count++)
      {
         cout << character;
      }
      cout << "\n";
   }
}


Any suggestions?
well i think your SOL bro, i can't think of anyway of doing that without using an array and some end of input notation like "-1".
It's quite simple of you have them separate them with commas instead of spaces, then you can store them in a cstring and parse to see what they gave you. Inputting 12,34 could be taken in all at once because there are no spaces or newlines.
Last edited on
closed account (zb0S216C)
You can define the same function more than once, providing that your arguments are different. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void TestMethod( void );
void TestMethod( int ArgumentOne );
void TestMethod( double ArgumentOne );

int main( )
{
    TestMethod( );     // Calling TestMethod( void )
    TestMethod( 4 );   // Calling TestMethod( int )
    TestMethod( 4.5 ); // Calling TestMethod( double )
}

void TestMethod( void )
{
}

void TestMethod( int ArgumentOne )
{
}

void TestMethod( double ArgumentOne )
{
}
The issue is that his assignment is to use default parameter values, and he is NOT allowed to use function overloading.

EDIT: Response to below: Just because he wrote "try's" instead of "tries" doesn't mean he didn't write it at all.
Last edited on
closed account (zb0S216C)
The issue is that his assignment is to use default parameter values, and he is NOT allowed to use function overloading.

Sorry, C++ police. He should of specified that information within his original post, not n post after the original post. Since that information was missed out, I assumed he was allowed to overload methods.
Topic archived. No new replies allowed.