Help with simple (I think) function

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.

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
#include<iostream>
#include<cstring>
using namespace std;
void inputPoint (char point, double x, double y)
{
    cout << "Please select a name for your point: ";
    cin >> point;
        cin.ignore();
    cout << "Please input the coordinates of " << point << " : ";
    cin >> x >> y;
}
void inputLine (char line, string equation)
{
    cout << "Please select a name for your line: ";
    cin >> line;
        cin.ignore();
    cout << "Please write the linear equation of " << line <<" : ";
    getline (cin, equation);
}
double getX(string eq)
{
    int len = eq.size();
    string helper;
    int i = 0;
    int sum = 0;
    if (eq[0] == 'x')
        return 1;
    while (eq[i] != 'x')
    {
        helper[i] = eq[i];
    }
    cout << helper;
}
void inLine()
{
    char point;
    double x, y;
    inputPoint (point, x, y);
    char line;
    string equation;
    inputLine (line, equation);
    getX(equation);
}
int main ()
{
    inLine();
}
Hi,

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.
Last edited on
I actually get 0 warnings :) and tnx for your help
I actually get 0 warnings
In this case your warning leve is clearly not high enough. Depending on yourcompiler/IDE there are differeent ways to tune it up.

Additionally I suggest you to look at stringstream. It allows you to crate a stream (like cin) from string and work with it as with normal stream:
http://en.cppreference.com/w/cpp/io/basic_istringstream
http://en.cppreference.com/w/cpp/io/basic_istringstream/basic_istringstream
I actually get 0 warnings :)


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);
                              ^
Last edited on
Topic archived. No new replies allowed.