lundi 1 mai 2017

Why are inputs not calling the right function in function overloading?

Purpose of the code:

Overload a function and based on input parameters, call the appropriate function. In my case, it doesn't.

input - one character, number of characters on that line, number of lines to be displayed

output result: if PrintLines(x,y,z) is called where x is Z, y is 3 and z is 2

output: ZZZ ZZZ

if PrintLines(x,y) is called where x is B, y is 4 output: BBBB

if PrintLines(x) is called where x is H output: H

if PrintLines() is called output: Z

Loop this 5 times in main

My code:

#include <iostream>
using namespace std;

int main()
{
   void PrintLines(int character, int number_characters, int number_lines);
   void PrintLines(int character, int number_characters);
   void PrintLines(int character);
   void PrintLines();

cout << "Enter the character to be displayed, number of times to display the character"
     <<" on each line and the number of lines to display separated by a space\n";

for(int loop =1; loop <=5 ; loop++)
{
    int c, number_character, number_lines;
    cin >> c >> number_character >> number_lines;
    PrintLines(c, number_character, number_lines);
    PrintLines(c, number_character);
    PrintLines(c);
    PrintLines();
}
return 0;
}

void PrintLines()
{
    cout << "Z";
}

void PrintLines(int character)
{
    cout << (char) character;
}

void PrintLines (int character, int number_characters)
{
    cout << character * number_characters;
}

void PrintLines(int character, int number_char, int number_lines)
{
    for (int looper =1; looper <= number_lines;)
    {
        number_char *= character;
        looper += 1;
        cout << number_char;
    }
}

Thanks!

Aucun commentaire:

Enregistrer un commentaire