Just as an initial note, this is NOT a homework problem. As you may know from my earlier posts, I have certain difficulties understanding classes. This was just a practice problem I picked up. Basically, I want to take input of 5 integers, and print like a bar graph of the sort (for say 1, 2, 3, 4, 5):
*
* *
* * *
* * * *
* * * * * |
This is what I've done so far:
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
#include<iostream.h>
#include<conio.h>
class PlotNumbers
{
public:
int p1, p2, p3, p4, p5, j, max;
PlotNumbers();
PlotNumbers(int, int, int, int, int);
void Plot(void);
void Fill();
char plotter[5][15];
};
PlotNumbers::PlotNumbers()
{
p1=3;
p2=4;
p3=3;
p4=4;
p5=3;
}
void PlotNumbers::Fill()
{
max=(p1>p2)?p1:p2;
max=(max>p3)?max:p3;
max=(max>p4)?max:p4;
max=(max>p5)?max:p5;
for (int i=1; i<=5; i++)
{
for (j=1; j<=15; j++)
{
plotter[i][j]=' ';
}
}
for (j=1; j<=p1; j++)
{
plotter[1][j]='*';
}
for (j=1; j<=p2; j++)
{
plotter[2][j]='*';
}
for (j=1; j<=p3; j++)
{
plotter[3][j]='*';
}
for (j=1; j<=p4; j++)
{
plotter[4][j]='*';
}
for (j=1; j<=p5; j++)
{
plotter[5][j]='*';
}
}
void PlotNumbers::Plot(void)
{
for (j=max; j>=1; j--)
{
for (int i=1; i<=5; i++)
{
cout<<plotter[i][j];
}
cout<<"\n";
}
}
void main()
{
PlotNumbers a;
clrscr();
a.Fill();
a.Plot();
getch();
}
|
My issues:
1) How do I take input from user for the parameters p1, p2, p3, p4, p5 using the same constructor name, but with different argument parameters, one being void (if user chooses default option) and of 5 ints (if user wants to enter his own ints)? When I try using Plotnumbers a(1, 2, 3, 4, 5), it doesn't compile.. Shows error of "Undefined symbol" or something...
2) This program works fine, but at the end of output, the compiler closes (I use TC++, I know, outdated, but still getting used to VC++, and am not very comfortable writing programs in that...) So any idea why this might be happening?
Thank you in advance! :)