Question:

I have a problem with a C program evry time I compile it it shows me a parse error before '&'.

by  |  earlier

0 LIKES UnLike

This is the code...

#include <iostream.h>

#include <iomanip.h>

#include <cmath>

#include <cstdlib>

using namespace std;

void instructUser();

double doDivideZero(double &);

int main()

{

instructUser();

double displayedVal;

double newEntry;

char command_character ;

displayedVal = 0.0;

cout << " Enter accepted Operator:" ;

cin >> command_character;

while (command_character != 'Q' || command_character != 'q')

{

switch(command_character)

{

case 'c':

case 'C': displayedVal = 0.0;

break;

case ' ': cout << " Enter Number:";

cin >> newEntry;

displayedVal = displayedVal newEntry;

break;

case '-': cout << " Enter Number:";

cin >> newEntry;

displayedVal = displayedVal - newEntry;

break;

case '*': cout << " Enter Number:";

cin >> newEntry;

displayedVal = displayedVal * newEntry;

break;

case '/': cout << " Enter Number:";

cin >>newEntry;

displayedVal = displayedVal / newEntry;

if (newEntry == 0)

{

doDivideZero(double &);

}

break;

case '^': cout << " Enter Number:";

cin >> newEntry;

displayedVal = pow (displayedVal,newEntry);

break;

default : cout << " Unacceptable Operator(" << command_character << ")" << endl;

}

cout << " The result so far is: " <<displayedVal<< endl;

cout << " Enter Operator:";

cin >> command_character;

}

system ("pause");

return 0;

}

void instructUser()

{

cout << " " <<endl;

cout << " ****************************************... <<endl;

cout << " * This program takes your input and selected mathematical operator *" <<endl;

cout << " * and returns the answer to the screen. If an illegal operator is *" << endl;

cout << " * selected, an error message will be displayed. Be careful which *" <<endl;

cout << " * operator you select because the program depends on you for input. *" <<endl;

cout << " * The only error check function it has, is for unacceptable opreators. *" <<endl;

cout << " * Acceptable operators are : ( , - , / , * ,^,c). The character c sets *" <<endl;

cout << " * the value stored to ZERO. Enter Q to exit. ENJOY YOUR PROGRAM !!!!!! *" <<endl;

cout << " ****************************************... <<endl;

cout << " " <<endl;

}

double doDivideZero(double &)

{

double newEntry;

double displayedVal;

newEntry =0;

displayedVal = 0.0;

if (newEntry !=0)

{

displayedVal = displayedVal / newEntry;

} else cout << "Wrong Operation, Cannot Divide by Zero" << endl;

return 0;

}

 Tags:

   Report

4 ANSWERS


  1. Before answering this i would like to knw if you knw how to declare, define and call a function.

    You have dont first two i.e; declare and define are correct. when it come to calling a function you have the problem.

    let me xplain you better way with help of your program

    double doDivideZero(double &amp;); --&gt; You have Declared of a funcion which accepts address of another valiable of type double and retuns a double value

    double doDivideZero(double &amp;)

    {

    } --&gt; You have defined a function i.e. you siad wht the function should do.

    doDivideZero(double &amp;); --&gt; Incorrent way to call a funtion. I think that you want to call the function so that it can perform the action you defined in the body of the above function.

    Firstly it should be doDivideZero(&lt;valiable&gt;);

    Secondly because it returns a Double value

    it should be either of the below

    cout &lt;&lt; doDivideZero(&lt;valiable&gt;);

    or

    double &lt;val&gt; = doDivideZero(&lt;valiable&gt;);

    Hope it helped you !!!!!!!!!


  2. Not an answer to your question, but probably useful all the same: the condition in

    while (command_character != &#039;Q&#039; || command_character != &#039;q&#039;)

    always evaluates to true, so you can&#039;t escape this loop.

    Well, not without break or exit or something. :-)

  3. double doDivideZero(double &amp;);

    should be

    double doDivideZero(double &amp; variableName);

    your comiler should have given you a line number or highlighted where it thought the error was..you didn&#039;t specify where to look

    so the above is my guess.

  4. There are three mistakes I see off the bat.  

    First is your declaration for the doDivideZero method:

    double doDivideZero(double &amp;);

    In C/C++ &amp; is used before a variable name to denote &#039;A reference to&#039; a specific variable.  You cannot use it as a variable name.

    If you are trying to pass a &#039;Pointer to Double&#039; you&#039;d write it this way:

    double doDivideZero(double * myDoublePointer);

    If you are just trying to pass a double variable then you&#039;d write it this way:

    double doDivideZero(double myDouble);

    Note that the way I write my variable names is just a notation and doesn&#039;t mean anything to the compiler.

    The second problem is more of the same thing, but slightly different.  When you call the method doDivideZero here:

    if (newEntry == 0)

    {

    doDivideZero(double &amp;);

    }

    You&#039;re not actually passing in a double variable.  If you have a variable myOtherDouble and want to pass it into the doDivideZero method, you would write it like this:

    if (newEntry == 0)

    {

    doDivideZero(myOtherDouble);

    }

    If you&#039;re trying to pass a reference to the variable myOtherDouble, you would write it like this:

    if (newEntry == 0)

    {

    doDivideZero( &amp;myOtherDouble);

    }

    Finally, in your actual definition of doDivideZero, you&#039;re doing the same thing again.  All you need to do to fix it is to change the signature to match the correct signature you specified in your declaration (i.e. Do the same thing you did in the first problem, remember to get rid of the semicolon (;) at the end of the line if you copy paste).

    Hope that helps.

Question Stats

Latest activity: earlier.
This question has 4 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.