### Trying to create a generic graphing tool

Hello everyone,

So I'm trying to write a couple of functions & classes that should allow me to create ascii graphs of functions that the user inputs.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253`` ``````#include #include #include #include #define NEWLINE '\n' #define TAB '\t' #define VTAB '\v' #define BEEP '\a' #define BACK '\b' using namespace std; enum plotpoint { zero, one, two, three, four, five, six, seven, eight, nine, dash, vertbar, ppoint, space}; enum reltype {constant, linear, quadratic, cubic, quartic}; void Output_Plot_Point(plotpoint pp); plotpoint Assign_PlotPoint_by_int(int c); double Input_double(string prompt); string Input_string(string prompt); int Round_double_to_int (double in); bool Compare_string(string i, string comp); void ExitCall(); class DataPlot { public: vector plot; DataPlot(void); DataPlot(int data [80] [100], int h); ~DataPlot(void); void DisplayPlot(); }; class CFunction { public: int datadesc [80] [100]; reltype function_type; double a, b, c, d, e; CFunction(void); CFunction(double ai, double bi, double ei); // f(x) = a(x-b) + e Overloaded Linear Constructor CFunction(double ai, double bi, double ci, double ei); // f(x) = a(x-b)(x-c) + e Overloaded Quadratic Constructor CFunction(double ai, double bi, double ci, double di, double ei); // f(x) = a(x-b)(x-c)(x-d) + e Overloaded Cubic Constructor void PlotFunction(); int get_plot_cell(int hofs, int vofs); ~CFunction(void); }; void User_cfunction(); void User_init_cfunction(CFunction * pFunction);``````

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277`` ``````#include "FRepo.h" #include #include #include #include #include #include #include #define NEWLINE '\n' #define TAB '\t' #define VTAB '\v' #define BEEP '\a' #define BACK '\b' using namespace std; double Input_double(string prompt) { while(true) { cout << prompt; double data; cin >> data; if (!cin.fail()) { return data; cout << NEWLINE << NEWLINE; } cin.clear(); cin.ignore(); cout << NEWLINE << NEWLINE; } } string Input_string(string prompt) { while(true) { cout << prompt; string data; getline (std::cin,data); if (!cin.fail()) { return data; cout << NEWLINE << NEWLINE; } cin.clear(); cin.ignore(); cout << NEWLINE << NEWLINE; } } int Round_double_to_int (double in) { int d; d = in; in = in - d; if (in == 0.5) { return d; } else if (in > 0.5) { d++; return d; } else if (in < 0.5) { return d; } } bool Compare_string(string i, string comp) { transform (i.begin (), i.end (), i.begin (), toupper); if (i == comp) { return true; } else if (i != comp) { return false; } } void ExitCall() { exit(0); } plotpoint Assign_PlotPoint_by_int(int c) { if (c == 0) { return zero; } if (c == 1) { return one; } if (c == 2) { return two; } if (c == 3) { return three; } if (c == 4) { return four; } if (c == 5) { return zero; } if (c == 6) { return six; } if (c == 7) { return seven; } if (c == 8) { return eight; } if (c == 9) { return nine; } if (c == 10) { return dash; } if (c == 11) { return vertbar; } if (c == 12) { return ppoint; } if (c == 13) { return space; } } void Output_Plot_Point(plotpoint pp) { if (pp == zero) { cout << "0"; } if (pp == one) { cout << "1"; } if (pp == two) { cout << "2"; } if (pp == three) { cout << "3"; } if (pp == four) { cout << "4"; } if (pp == five) { cout << "5"; } if (pp == six) { cout << "6"; } if (pp == seven) { cout << "7"; } if (pp == eight) { cout << "8"; } if (pp == nine) { cout << "9"; } if (pp == dash) { cout << "-"; } if (pp == vertbar) { cout << "|"; } if (pp == ppoint) { cout << "*"; } if (pp == space) { cout << " "; } } DataPlot::DataPlot(void) { } DataPlot::DataPlot(int data [80] [100], int h) { int height = 0; plotpoint copyline [80]; while (true) { int p = 0; int c = data [p] [height]; copyline [p] = Assign_PlotPoint_by_int(c); p++; if (p == 79) { plot.emplace(plot.begin(), copyline); p = 0; height++; if (height == h) { break; } } } } void DataPlot::DisplayPlot() { vector::iterator it; for (it = (plot.end()-1); it != (plot.begin()-1); --it) { while (true) { int p_out = 0; Output_Plot_Point((*it) [p_out]); p_out++; if (p_out == 79) { cout << NEWLINE; } } } } DataPlot::~DataPlot(void) { } void User_cfunction() { CFunction * pFx; User_init_cfunction(pFx); string i; i = Input_string("Plot Function? "); transform (i.begin (), i.end (), i.begin (), toupper); if ((i == "YES")||(i == "Y")) { cout << NEWLINE << NEWLINE; pFx->PlotFunction(); DataPlot Output (pFx->datadesc, 80); Output.DisplayPlot(); delete pFx; } else { cout << "deleting plot." << endl; delete pFx; } } void User_init_cfunction(CFunction * pFunction) { string i; reltype ftype; while(true) { i = Input_string("Please input function type: "); transform (i.begin (), i.end (), i.begin (), toupper); if ((i == "LINEAR")||(i == "L")) { ftype = linear; double ai, bi, ei; ai = Input_double("a value: "); bi = Input_double("b value: "); ei = Input_double("e value: "); pFunction = new CFunction(ai, bi, ei); break; } else if ((i == "QUADRATIC")||(i == "Q")) { ftype = quadratic; double ai, bi, ci, ei; ai = Input_double("a value: "); bi = Input_double("b value: "); ci = Input_double("c value: "); ei = Input_double("e value: "); pFunction = new CFunction(ai, bi, ci, ei); break; } else if ((i == "CUBIC")||(i == "C")) { ftype = quadratic; double ai, bi, ci, di, ei; ai = Input_double("a value: "); bi = Input_double("b value: "); ci = Input_double("c value: "); di = Input_double("d value: "); ei = Input_double("e value: "); pFunction = new CFunction(ai, bi, ci, di, ei); break; } } } CFunction::CFunction(double ai, double bi, double ei) { function_type = linear; a = ai; b = bi; c = 0; d = 0; e = ei; } CFunction::CFunction(double ai, double bi, double ci, double ei) { function_type = quadratic; a = ai; b = bi; c = ci; d = 0; e = ei; } CFunction::CFunction(double ai, double bi, double ci, double di, double ei) { function_type = cubic; a = ai; b = bi; c = ci; d = di; e = ei; } int CFunction::get_plot_cell(int hofs, int vofs) // a-horizontal ofs (right) b-vertical ofs (down) { double xvar, fxvar; int check; if (function_type = linear) // f(x) = a(x-b) + e Overloaded Linear Constructor { xvar = hofs; fxvar = ((a*(xvar - b))+e); check = Round_double_to_int (fxvar); if (check == vofs) { return 12; } else { return 13; } } else if (function_type = quadratic) // f(x) = a(x-b)(x-c) + e Overloaded Quadratic Constructor { xvar = hofs; fxvar = ((a*(xvar - b)*(xvar - c))+e); check = Round_double_to_int (fxvar); if (check == vofs) { return 12; } else { return 13; } } else if (function_type = cubic) // f(x) = a(x-b)(x-c)(x-d) + e Overloaded Cubic Constructor { xvar = hofs; fxvar = ((a*(xvar - b)*(xvar - c)*(xvar - d))+e); check = Round_double_to_int (fxvar); if (check == vofs) { return 12; } else { return 13; } } } void CFunction::PlotFunction() { int hofs, vofs; vofs = 0; while (true) { hofs = 0; datadesc [hofs] [vofs] = get_plot_cell(hofs, vofs); hofs++; if (hofs == 79) { hofs = 0; vofs++; if (vofs == 99) { break; } } } }``````

Everything appears okay until I try to compile, when I get this:
 ``123456`` ``````1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xmemory(280) : see reference to function template instantiation 'void std::allocator<_Ty>::construct(plotpoint (*)[80],_Other)' being compiled 1> with 1> [ 1> _Ty=plotpoint [80], 1> _Other=plotpoint (&)[80] 1> ]``````

I'm not sure what that means, but it sounds a lot like some issue with what I'm passing to the vector class as its content. Can anyone clarify what I'm doing wrong here?
Hi there,

In your .h: `vector<plotpoint [80]> plot;`

You can't store arrays in STL containers. You could use std::array if your compiler supports it:

`vector< std::array<plotpoint, 80>> plot;`

Don't forget to alter your .cpp file as well:
Line 147: `vector<std::array<plotpoint, 80>>::iterator it;`

Hope that helps.

All the best,
NwN
Last edited on
Tried it...

And no luck. I'm using MSVC++ 2010, so I guess my compiler doesnt support it?

Any ideas on how else I could handle the graph lines? Maybe I'll just leave the size as 80x100 standard...
MSVC 2010 does support std::array<>

 ``12345`` ``````#include // etc std::array test;``````

Andy
Hi,

Just to clarify Andywestken's post, did you `#include <array> ` ?

All the best,
NwN
Ahh, yes I did, but no luck anyways. I decided to make the plot size fixed at 80x100, and made quite a few other changes under the hood, but I cant get it to work yet. Im going to do a bit of a tear down & rebuild on this to fix the spaghetti code monster that I've created. If all goes well, I'll post the results here later.
Topic archived. No new replies allowed.