Welcome to Dream.In.Code
Become a C++ Expert!

Join 136,929 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,831 people online right now. Registration is fast and FREE... Join Now!




Bar function

 
Reply to this topicStart new topic

Bar function, Overloading problem

Entropicvamp
31 Mar, 2008 - 04:27 PM
Post #1

New D.I.C Head
*

Joined: 31 Mar, 2008
Posts: 14


My Contributions
So here is my code, and here is my problem I have to create an overloaded version of the drawbar function. The overloaded function should accept two parameters: The first one will indicate the length, the second will be the character to be printed. For some reason this isn't working out the way I intend it to, where am I going wrong on this?
CODE
# include <iostream>

using namespace std;

void drawBar(int length, int character);
void printTriangle(int userInput);


int main ()

{
    int length = 0;
    int character = 0;
    cout << "Enter the triangle base size: ";
    cin >> length;
    
return 0;
}

//Function Definitiion
void drawBar(int length)
{
    for (int a = 1; a <= length; a++)
    {
      printTriangle(userInput);//This is where it is telling me it is wrong and this is the code I get: error C2065: 'userInput' : undeclared identifier    

    }
    cout << endl;
}

void printTriangle(int userInput)
{
    for (int a = 1; a <= userInput; a++)
    {
        for (int b = 1; b <= a; b++)
            {
                if (b <= userInput)
                    cout << "*";
                else
                    cout << "";
                }
             cout << endl;
            }
   }

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Bar Function
31 Mar, 2008 - 04:57 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,998



Thanked: 126 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
If you look at your code, you get the value for the variable length, which is passed to drawBar


cpp

int length = 0;
int character = 0;
cout << "Enter the triangle base size: ";
cin >> length;


But for your userInput variable you never declare that variable, nor do you set it's value. In your main method you should be declaring userInput and getting/setting it's value, am I correct?

cpp


int main ()
{
//here you declare and set the value to
//be passed to the drawBar method, but
//nothing for printTriangle

int length = 0;
int character = 0;
//declare userInput
int userInput = 0;
cout << "Enter the triangle base size: ";
cin >> length;

//set the value for userInput
cout << "Enter the character to be printed:";
cin >> userInput;

return 0;
}



Another issue I see you could be having is you declare the drawBar method with 2 parameters


cpp

void drawBar(int length, int character);



But when you define the method you have but 1 parameter


cpp

//Function Definitiion
void drawBar(int length)
{
for (int a = 1; a <= length; a++)
{
printTriangle(userInput);//This is where it is telling me it is wrong and this is the code I get: error C2065: 'userInput' : undeclared identifier

}
cout << endl;
}

User is offlineProfile CardPM
+Quote Post

Entropicvamp
RE: Bar Function
31 Mar, 2008 - 05:30 PM
Post #3

New D.I.C Head
*

Joined: 31 Mar, 2008
Posts: 14


My Contributions
I got the code to work with the input on the variables, which works fine but now it will not print out the triangle pattern. Where am I going wrong with this? It will accept the input but the output will not show any pattern.
CODE
# include <iostream>

using namespace std;

void drawBar(int length, int character);
void printTriangle(int userInput);


int main ()

{
    int length = 0;  
    int character = 0;  
    //declare userInput  
    int userInput = 0;  
    cout << "Enter the triangle base size: ";  
    cin >> length;  

    //set the value for userInput  
    cout << "Enter the character to be printed: ";  
    cin >> userInput;
    
return 0;
}

//Function Definitiion
void drawBar(int userInput)
{
    for (int a = 1; a <= userInput; a++)
    {
        printTriangle(userInput);
    }
    cout << endl;
}

void printTriangle(int userInput)
{
    for (int a = 1; a <= userInput; a++)
    {
        for (int b = 1; b <= a; b++)
            {
                if (b <= userInput)
                    cout << "*";
                else
                    cout << "";
                }
             cout << endl;
            }
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 10:31PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month