Prototype error

Everytime I think I have these prototypes figured out I realize I don't. I am getting a c2082 redifnition error on line 67

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
#include<iostream>
#include <cmath>
#include <iomanip>
using namespace std;

void getIntegers(string, double&);
double quadEquation(double, double, double, double);
double inputValidate(double);
double getDiscriminant(double, double, double);

int main()
{
    double discriminant,squared, squareRoot;
    getDiscriminant(discriminant,squared, squareRoot);



	return 0;
}

void getIntegers(string prompt, double& user_input)
{
    cout << prompt;
    user_input = inputValidate(user_input);
}

double quadEquation(double num1 , double num2, double num3, double findX)
{
    


    return 0.0;
}

double inputValidate(double num1)
{

    while (!(cin >> num1))
    {
        cout << "Error. Number must not be a letter "
            << ", it must be a number:";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    return num1;
}

double getDiscriminant(double userNum1, double userNum2, double userNum3)
{
    double userNum1, userNum2, userNum3, findX, numInt = 1;
    //getIntegers("Enter Intergers", numInt);
    for (int i = 0; i < numInt; i++)
    {
        getIntegers("Enter integer a: ", userNum1);
        getIntegers("Enter integer b: ", userNum2);
        getIntegers("Enter integer c: ", userNum3);
    }
    double a = userNum1, b = userNum2, c = userNum3, 
        discriminant, squared, squareRoot;

    discriminant = b * b - (4 * a * c);
    squared = pow(userNum2, 2);
    squareRoot = sqrt(b);

    std::cout.precision(2);
    std::cout.setf(std::ios::fixed);

    cout << "This is the discriminant: " << discriminant << endl;
    cout << "B Squared:  " << squared << endl;
    cout << "Squared Root of b:  " << squareRoot << endl;

    return discriminant,squared,squareRoot;
}



line 67

 
double userNum1, userNum2, userNum3, findX, numInt = 1;


Thanks in advanced for looking!!
(1) You already define userNum1, userNum2, userNum3 as function parameters.
(2) You are overwriting these values anyway, so they shouldn't be parameters.
(3) numInt is 1, so your for loop on line 53 of your posted code is unnecessary.
(4) You can't return multiple values at once from a function. That's not how the comma operator works. It looks like you only want to return the discriminant, considering that's the name of your function.
(5) You are returning a double from the getDiscriminant function, but you never use this value.

It looks like userNum1, userNum2, and userNum3 are just copied into a, b, c, which are the well-known constants for the quadratic formula, so why not just use a, b, c from the start?

What is the significance of printing b^2 and sqrt(b)? Those values don't matter by themselves; I assume the goal here is to find the roots of ax^2 + bx + c?

If I were you, here is how I would split up the responsibilities of the program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
    // (1) get user input (and validate)
    double a = getValidatedInput("Enter a");
    double b = getValidatedInput("Enter b");
    double c = getValidatedInput("Enter c");

    // (2) pass to quadratic formula:
    //     [x1 x2 are passed by reference to be set internally]
    double out_x1;
    double out_x2;
    quadraticRoots(a, b, c, out_x1, out_x2);
 
    cout << "x1 = " << out_x1 << ", " << "x2 = " << out_x2 << '\n'
}


As a bonus, you could make quadraticRoots return false if the roots are complex...

PS: It is usually faster to just do number * number instead of pow(number, 2) when number is a floating-point.

PPS: If you use numeric_limits<streamsize>::max(), you should #include <limits>.
Last edited on
Ok back to original question I am trying to understand prototypes. The answer 1 and 2 is what I am trying to understand.
(3) Thanks didn't think about that makes sense.
(4) Yes I only want discriminant the other two I was just playing around with pow and sqrt to get a handle on how to use they are not needed or wanted for this.
(5) I will use the value just want to make sure I have code working right to this point and I do not so back to 1 and 2.

I define them in prototype so do I not have to in main function? Which part is causing me to overwrite the values? Thanks for the tips!!

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
#include<iostream>
#include <cmath>
#include <iomanip>
#include <limits>
using namespace std;

void getIntegers(string, double&);
double quadEquation(double, double, double, double);
double inputValidate(double);
double getDiscriminant(double, double, double);

int main()
{
    double discriminant,squared, squareRoot;
    getDiscriminant(discriminant,squared, squareRoot);

    return 0;
}

void getIntegers(string prompt, double& user_input)
{
    cout << prompt;
    user_input = inputValidate(user_input);
}

double quadEquation(double num1 , double num2, double num3, double findX)
{
    


    return 0.0;
}

double inputValidate(double num1)
{

    while (!(cin >> num1))
    {
        cout << "Error. Number must not be a letter "
            << ", it must be a number:";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    return num1;
}

double getDiscriminant(double userNum1, double userNum2, double userNum3)
{
    double userNum1, userNum2, userNum3;
    //findX, numInt = 1;
   // for (int i = 0; i < numInt; i++)
    {
        getIntegers("Enter integer a: ", userNum1);
        getIntegers("Enter integer b: ", userNum2);
        getIntegers("Enter integer c: ", userNum3);
    }
    double a = userNum1, b = userNum2, c = userNum3,
        discriminant;

    discriminant = b * b - (4 * a * c);
    

    std::cout.precision(2);
    std::cout.setf(std::ios::fixed);

    cout << "This is the discriminant: " << discriminant << endl;
    

    return discriminant;
}
This is the issue:
1
2
3
double getDiscriminant(double userNum1, double userNum2, double userNum3) // function parameters
{
    double userNum1, userNum2, userNum3; // local variables 


userNum1, userNum2, and userNum3 are defined both as function parameters, and as local variables.
Well this kind of works it allows me to enter my 3 integers and gives correct answer but I got a new error: Actually not working just noticed it was I guess giving me what was the answer to the last time it ran correctly..

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
#include<iostream>
#include <cmath>
#include <iomanip>
#include <limits>
using namespace std;

void getIntegers(string, double&);
double quadEquation(double, double, double, double);
double inputValidate(double);
double getDiscriminant(double, double, double);


int main()
{
    double discriminant;
    getDiscriminant(discriminant);

    return 0;
}

void getIntegers(string prompt, double& user_input)
{
    cout << prompt;
    user_input = inputValidate(user_input);
}

double quadEquation(double num1 , double num2, double num3, double findX)
{
    


    return 0.0;
}

double inputValidate(double num1)
{

    while (!(cin >> num1))
    {
        cout << "Error. Number must not be a letter "
            << ", it must be a number:";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    return num1;
}

double getDiscriminant(double num1, double num2, double num3)
{
   
    double userNum1, userNum2, userNum3;
    double a = userNum1, b = userNum2, c = userNum3,
        discriminant;
    
        getIntegers("Enter integer a: ", userNum1);
        getIntegers("Enter integer b: ", userNum2);
        getIntegers("Enter integer c: ", userNum3);
    
   

    discriminant = b * b - (4 * a * c);
    

    std::cout.precision(2);
    std::cout.setf(std::ios::fixed);

    cout << "This is the discriminant: " << discriminant << endl;
    

    return discriminant;
}



Error C2660 'getDiscriminant': function does not take 1 arguments
Last edited on
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
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;

void getIntegers(string, double&);
double inputValidate();
double getDiscriminant();

int main()
{
	const double discrim {getDiscriminant()};

	std::cout.precision(2);
	std::cout.setf(std::ios::fixed);
	cout << "This is the discriminant: " << discrim << endl;
}

void getIntegers(string prompt, double& user_input)
{
	cout << prompt;
	user_input = inputValidate();
}

double inputValidate()
{
	double num1 {};

	while (!(cin >> num1))
	{
		cout << "Error. Number must not be a letter "
			<< ", it must be a number:";

		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
	}

	return num1;
}

double getDiscriminant()
{
	double a {}, b {}, c {};

	getIntegers("Enter integer a: ", a);
	getIntegers("Enter integer b: ", b);
	getIntegers("Enter integer c: ", c);

	return b * b - (4 * a * c);
}


Thanks seeplus I see what you did there and that helped here is what I got is there anything wrong with how this code is so far in your opinion? I think my problem is knowing when I need getDiscriminant (to define something here and when not too!)

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

#include<iostream>
#include <cmath>
#include <iomanip>
#include <limits>
using namespace std;

void getIntegers(string, double&);
double quadEquation(double, double, double, double);
double inputValidate(double);
double getDiscriminant();


int main()
{
  
    getDiscriminant();

    return 0;
}

void getIntegers(string prompt, double& user_input)
{
    cout << prompt;
    user_input = inputValidate(user_input);
}

double quadEquation(double num1, double num2, double num3, double findX)
{



    return 0.0;
}

double inputValidate(double num1)
{

    while (!(cin >> num1))
    {
        cout << "Error. Number must not be a letter "
            << ", it must be a number:";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    return num1;
}

double getDiscriminant()
{

    double a, b, c;
    double discriminant;

    getIntegers("Enter integer a: ", a);
    getIntegers("Enter integer b: ", b);
    getIntegers("Enter integer c: ", c);

    std::cout.precision(2);
    std::cout.setf(std::ios::fixed);
    discriminant = b * b - (4 * a * c);

    cout << "This is the discriminant: " << discriminant << endl;
    return discriminant;
}
I think my problem is knowing when I need getDiscriminant (to define something here and when not too!)

You define parameters to a function when you need to pass values into the function from the calling code.

Also, if you want your function to pass more than one value back to the calling code, defining parameters that pass by reference is one way of doing that.
Last edited on
1
2
3
4
5
6
7
8
double inputValidate(double num1)
{
    while (!(cin >> num1))
    {
        //..
    }
    return num1;
}
tell me what's the difference between these calls to that function
1
2
3
4
5
user_input = inputValidate(user_input);
user_input = inputValidate(0);
user_input = inputValidate(42);
user_input = inputValidate(-1);
user_input = inputValidate(foo);


double getDiscriminant();
stop doing input/output operations in functions whose purpose is to do something else than input/output operations
functions communicate using their parameters and return value, not input/output operations

if you calculate the a_j coefficients some other way, you can't pass them to your `getDiscriminant()' function
every time you calculate the discriminant you are printing to screen, perhaps there is more interesting output


think what you want to do and write pseudocode first

Everytime I think I have these prototypes figured out I realize I don't.


What I thought I was doing with the inputValidation is checking to make sure user input was a double and if not then it would run error message.

I appreciate the tips but I am really new to this and doing as much research on my own as possible.

So when you say don't don't input/output in functions not meant for that should I be using a void function?

So to answer your question I would think all would run except for inputValidate(foo)???

Thanks for your patience's.
What I thought I was doing with the inputValidation is checking to make sure user input was a double and if not then it would run error message.

What that function is doing is:

1) Get the input from the user, from standard input
2) Validate whether the input is a number
3a) If it's not a number, then reset the state flags on the standard input stream, discard everything from the stream up to the next newline, and store 0 in num1
3b) If it is a number, store that number in num1
4) Return to the calling code, the number that was put into num1

So to answer your question I would think all would run except for inputValidate(foo)???


But that doesn't answer the question, which was:

tell me what's the difference between these calls to that function

1
2
3
4
5
user_input = inputValidate(user_input);
user_input = inputValidate(0);
user_input = inputValidate(42);
user_input = inputValidate(-1);
user_input = inputValidate(foo);


Let's make it simpler:

What is the difference between
user_input = inputValidate(0);
and
user_input = inputValidate(42);
?

How do you think the behaviour of the code would differ in those two cases?

Last edited on
Is it passing the value in the round brackets so the 0 or the 42 to the calling function?
So we are setting user_input to equal the value being passed into by inputValidate (WHATEVER IS HERE)?
The point is, there is no difference between passing 0 and passing 42, because you immediately overwrite the value passed in with another value. You could just make num1 be a local variable.
Last edited on
So we are setting user_input to equal the value being passed into by inputValidate (WHATEVER IS HERE)?

You... you do realise that I already described for you exactly what your function does, and what it returns, yes? And that it WASN'T that, yes?

If you're not even going to bother reading what people write, then there seems little point any of us writing anything more.
I did read thanks for all you help
Topic archived. No new replies allowed.