Hey, I'm trying to write a function that will return the value before x in a linear equation, note: the user will always input it as such: 34x + 5y + 1 = 0, so I don't need additional checks, all I want is to add the string before x into another string and then add it into an intiger as actual numbers (don't know if I make much sense yet) but it keeps getting stuck even at printing the string before x, it won't even return 1 when there's nothing before x and I have no clue why.
I think you would benefit from reading up about returning values from functions: yours don't - even getX returns 1.0 sometimes but nothing otherwise. And the scope of variables.
For example the x and y in inputPoint should be references, so that the values are changed in the scope of inLine.
Also, you are using x , y,line and equation while they are not initialised. Always initialise your variables to some thing.
Just wondering what compiler warning levels you have set - you should have had warnings about getX not always returning a value, and the uninitialised variables.
If you do have warnings, then post them here in full.
Hope all goes well :+)
Edit:
Have a look at the tutorial at the top left of this page.
But I am saying you should have had more warnings.
If one uses the g++ compiler, one set these warnings as a minimum:
-Wall -Wextra -pedantic
Find out how to set the warnings for your compiler, in your IDE if you are using one.
:+)
$ g++ -Wall -Wextra -pedantic poly.cpp -o poly
poly.cpp: In function ‘double getX(std::string)’:
poly.cpp:22:9: warning: unused variable ‘len’ [-Wunused-variable]
int len = eq.size();
^
poly.cpp:25:9: warning: unused variable ‘sum’ [-Wunused-variable]
int sum = 0;
^
poly.cpp:33:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
poly.cpp: In function ‘void inLine()’:
poly.cpp:38:28: warning: ‘point’ is used uninitialized in this function [-Wuninitialized]
inputPoint (point, x, y);
^
poly.cpp:38:29: warning: ‘x’ is used uninitialized in this function [-Wuninitialized]
inputPoint (point, x, y);
^
poly.cpp:38:29: warning: ‘y’ is used uninitialized in this function [-Wuninitialized]
poly.cpp:41:30: warning: ‘line’ may be used uninitialized in this function [-Wmaybe-uninitialized]
inputLine (line, equation);
^