Please support our C++ advertiser: Programming Forums
Views: 1623 | Replies: 9
![]() |
So I had this nifty program on my calculator that I wrote that sloves quadratic equations for me. I decided it would be good practice to try and write it in C++. I've actually had 4 different versions, each one (atleast in my opinon) getting better than the last. When I went to compile this one, for some reason when Dev C++ gets to the opening brace for the whatnext function it says there is a parse error. I bet I'm doing something stupidly small. Could I get some help? Any other suggestions on makeing the program more sleek and efficient would also be appreciated.
Source code:
<< moderator edit: added code tags: [code][/code] >>
Source code:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
// declare prototypes
void whatnext (void);
void solver (void);
//global varible declarations
double a;
double b;
double c;
double y;
double e;
double d;
int main (int nNumberofArgs, char* pszArgs[])
{
// Start of main program loop
for (;; )
{
int chose;
cout << "Type 1 to solve for an equation and 2 to exit:";
cin >> chose;
//Exit or solve
//solve
if (chose == 1)
{
// ask for variables
cout << "What does A equal?:";
cin >> a;
cout << "What does B equal?:";
cin >> b;
cout << "What does C equal?:";
cin >> c;
cout << "What does Y equal?:";
cin >> y;
//call to decsion function
whatnext();
}
//exit
else
{
cout << "Thank you for using Kurt's Quadsolver" << endl;
//pause and let them read my beautiful statement
system("PAUSE");
return 0;
}
}
void whatnext (void)
{
//preliminary computations
c -= y;
d = -(b / (2 * a));
e = (b * b) - (4 * a * c);
//decide what to do
//if there is no solution
if (e < 0)
{
cout << "No Solution. \nSorry.\n";
system("PAUSE");
//return to caller
}
//call the solving function
else
{
solver();
}
}
//solver function
void solver(void)
{
//for one solution
if (e == 0)
{
//declare helper f variable
double f;
e = sqrt ( e ) / (2 * a );
f = d - e;
//display answers and let them see it
cout << "Solution 1: \n";
cout << f;
cout << "\nYour welcome. \n";
system("PAUSE");
//return to calling function
}
//two solutions
else
{
e = sqrt( e ) / (2 * a);
//declare helper f and g variables
double f;
double g;
f = d - e;
g = d + e;
//display solutions
cout << "Solution 1: \n";
cout << f;
cout << "\nSolution 2: \n";
cout << g;
cout << "\nYour welcome. \n";
system ("PAUSE");
//return to calling function
}
}•
•
Join Date: May 2005
Location: Wellington, New Zealand
Posts: 182
Reputation:
Rep Power: 4
Solved Threads: 3
Your missing the close bracket on your main method. This would be a little more obvious if your code was better formatted (did it lose some formatting on posting?).
Cheers.
Cheers.
Yea, I know. I was going to have them passed but then I got scared lol. I mean as you saw, I'd have to pass it from one function which would then inturn have to pass the same variables to another function. I don't think the book I had did a very good job in explaining exactly how to pass variables. It confused me with the arguments. Could you explain how that works? Also, why are global variables bad?
I had to scratch my head a lot to come up with this, hope it helps explain a few things ...
[php]// example shows you how to pass a number of variables to and from a function
// also shows how to get things screwed up with a global variable
#include <cstdlib>
#include <iostream>
using namespace std;
int c = 5; // our global
void pass_by_ref(int &a, int &b)
{
a = a + c;
c = 2 * c; // forgot that c will be used later
b = b + c;
}
int a_times_5(int a)
{
a = a * c;
return a;
}
int main()
{
int a, b;
a = 10;
b = 20;
cout << "Before pass_by_ref(a, b) a = " << a << " b = " << b << endl;
pass_by_ref(a, b);
cout << "After pass_by_ref(a, b) a = " << a << " b = " << b << endl;
cout << "Here comes a potential booboo ..." << endl;
a_times_5(a);
// we still think c is 5
cout << a << " times 5 = " << a_times_5(a) << " oops!" << endl;
cin.get(); // wait
return EXIT_SUCCESS;
}
[/php]
[php]// example shows you how to pass a number of variables to and from a function
// also shows how to get things screwed up with a global variable
#include <cstdlib>
#include <iostream>
using namespace std;
int c = 5; // our global
void pass_by_ref(int &a, int &b)
{
a = a + c;
c = 2 * c; // forgot that c will be used later
b = b + c;
}
int a_times_5(int a)
{
a = a * c;
return a;
}
int main()
{
int a, b;
a = 10;
b = 20;
cout << "Before pass_by_ref(a, b) a = " << a << " b = " << b << endl;
pass_by_ref(a, b);
cout << "After pass_by_ref(a, b) a = " << a << " b = " << b << endl;
cout << "Here comes a potential booboo ..." << endl;
a_times_5(a);
// we still think c is 5
cout << a << " times 5 = " << a_times_5(a) << " oops!" << endl;
cin.get(); // wait
return EXIT_SUCCESS;
}
[/php]
May 'the Google' be with you!
Yea I kinda get it. But now you've brought in the one other concept in C++ that I don't understand. Pointer Variables. I understand how they are used, but now why they are useful. Can't you write the program you just wrote without Pointer Variables? I guess not though because then the changes you made to b and a wouldn't take. Hmmm... So is this statement correct: Without the pointer variables the changes to a and b that occur in the the function pass_by_ref would be undone once control passed back to the main function after execution of the pass_by_ref function. I think I understand now. Heres another statement that I think is correct: Pointer variables allow you to pass the changes to variables from the function that was called to the calling function. Is that also correct? Also, when are global variables good to use? (side note: wow we've really gotten away from the original topic...I like it! you've all been really helpful.)
So tired this:
but my compiler has a problem when I make the call to slover from whatnext. Dev C++ doesn't like the line
What am I doing wrong?
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
// declare prototypes
void whatnext (double &a, double &b, double &c, double &y);
void solver (double &a, double &b, double &c, double &y, double &d, double &e);
int main (int nNumberofArgs, char* pszArgs[])
{
// Start of main program loop
for (;;)
{
string chose;
cout << "Type 'solve' to solve for an equation and 'exit' to exit:";
cin >> chose;
//Exit or solve
//solve
if (chose == "solve")
{
//declar equation variables
double a;
double b;
double c;
double y;
// ask for variables
cout << "What does A equal?:";
cin >> a;
cout << "What does B equal?:";
cin >> b;
cout << "What does C equal?:";
cin >> c;
cout << "What does Y equal?:";
cin >> y;
//call to decsion function
whatnext(a, b, c, y);
}
//exit
else
{
cout << "Thank you for using Kurt's Quadsolver" << endl;
//pause and let them read my beautiful statement
system("PAUSE");
return 0;
}
}
}
void whatnext (double &a, double &b, double &c, double &y)
{
//helper variables
double d;
double e;
//preliminary computations
c -= y;
d = -(b / (2 * a));
e = (b * b) - (4 * a * c);
//decide what to do
//if there is no solution
if (e < 0)
{
cout << "No Solution. \nSorry.\n";
system("PAUSE");
//return to caller
}
//call the solving function
else
{
solver(a, b, c, y, d, e,);
}
}
//solver function
void solver(double &a, double &b, double &c, double &y, double &d, double &e)
{
//for one solution
if (e == 0)
{
//declare helper f variable
double f;
e = sqrt ( e ) / (2 * a );
f = d - e;
//display answers and let them see it
cout << "Solution 1: \n";
cout << f;
cout << "\nYour welcome. \n";
system("PAUSE");
//return to calling function
}
//two solutions
else
{
e = sqrt( e ) / (2 * a);
//declare helper f and g variables
double f;
double g;
f = d - e;
g = d + e;
//display solutions
cout << "Solution 1: \n";
cout << f;
cout << "\nSolution 2: \n";
cout << g;
cout << "\nYour welcome. \n";
system ("PAUSE");
//return to calling function
}
} but my compiler has a problem when I make the call to slover from whatnext. Dev C++ doesn't like the line
solver(a, b, c, y, d, e,)
What am I doing wrong?
•
•
•
•
Originally Posted by Kob0724
Dev C++ doesn't like the line
What am I doing wrong?solver(a, b, c, y, d, e,)
High Plains Blogger #plains #lounge ## I, for one, welcome our new socialist overlords.
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
O...yeah lol. I always make those stupid little mistakes. I'm gonna look for those more instead of just assuming I've done something big wrong. What about the statements I made.
and
Are those good, correct statements.
•
•
•
•
Without the pointer variables the changes to a and b that occur in the the function pass_by_ref would be undone once control passed back to the main function after execution of the pass_by_ref function.
and
•
•
•
•
Pointer variables allow you to pass the changes to variables from the function that was called to the calling function.
Are those good, correct statements.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode