RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 1623 | Replies: 9
Reply
Join Date: Jul 2005
Posts: 51
Reputation: Kob0724 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Kob0724's Avatar
Kob0724 Kob0724 is offline Offline
Junior Poster in Training

Why won't this compile

  #1  
Jul 17th, 2005
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:


#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
    }
}
<< moderator edit: added code tags: [code][/code] >>
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2005
Location: Wellington, New Zealand
Posts: 182
Reputation: alpha_foobar is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 3
alpha_foobar's Avatar
alpha_foobar alpha_foobar is offline Offline
Junior Poster

Re: Why won't this compile

  #2  
Jul 17th, 2005
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.
Reply With Quote  
Join Date: Jul 2005
Posts: 51
Reputation: Kob0724 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Kob0724's Avatar
Kob0724 Kob0724 is offline Offline
Junior Poster in Training

Re: Why won't this compile

  #3  
Jul 18th, 2005
Yea, it was all formated, i just lost all the indentation when I copied and pasted. Your right! I knew it was something small and stupid. I Just needed an extra set of eyes. Thank you! By the way, what do you think of the code? Good program? Anything you would've done different?
Reply With Quote  
Join Date: Oct 2004
Posts: 2,545
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 11
Solved Threads: 178
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Solution Re: Why won't this compile

  #4  
Jul 19th, 2005
One improvement would be to avoid all those exposed globals, a major source of problems in any program. You got to learn to pass these variables properly to and from functions. Hey, it's a start, congratulations!
May 'the Google' be with you!
Reply With Quote  
Join Date: Jul 2005
Posts: 51
Reputation: Kob0724 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Kob0724's Avatar
Kob0724 Kob0724 is offline Offline
Junior Poster in Training

Re: Why won't this compile

  #5  
Jul 19th, 2005
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?
Reply With Quote  
Join Date: Oct 2004
Posts: 2,545
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 11
Solved Threads: 178
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Solution Re: Why won't this compile

  #6  
Jul 19th, 2005
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]
May 'the Google' be with you!
Reply With Quote  
Join Date: Jul 2005
Posts: 51
Reputation: Kob0724 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Kob0724's Avatar
Kob0724 Kob0724 is offline Offline
Junior Poster in Training

Re: Why won't this compile

  #7  
Jul 19th, 2005
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.)
Reply With Quote  
Join Date: Jul 2005
Posts: 51
Reputation: Kob0724 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Kob0724's Avatar
Kob0724 Kob0724 is offline Offline
Junior Poster in Training

Re: Why won't this compile

  #8  
Jul 20th, 2005
So tired this:

#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?
Reply With Quote  
Join Date: Apr 2004
Posts: 3,763
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Why won't this compile

  #9  
Jul 20th, 2005
Originally Posted by Kob0724
Dev C++ doesn't like the line
solver(a, b, c, y, d, e,)
What am I doing wrong?
Get rid of the extra comma. (And the line should end in a semicolon, but in the whole code this is already so.)
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."
Reply With Quote  
Join Date: Jul 2005
Posts: 51
Reputation: Kob0724 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Kob0724's Avatar
Kob0724 Kob0724 is offline Offline
Junior Poster in Training

Re: Why won't this compile

  #10  
Jul 20th, 2005
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.

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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:13 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC