Monday, May 24, 2010

C++ How do I use a char as a sentinel value when all other inputs are integers? I want the while loop to check

..for the user inputting 'q' or "Q". However, all my other inputs (A, B and C) are integers. I run into a problem when trying to compare them.





#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;


#include %26lt;cmath%26gt;


using namespace std;





int main()


{


float A, B, C;


float discriminant, denominator, numerator, plusSolution, minusSolution, root;


char SENTINEL = 0;


A = 1;


B = 5;


C = 1;





cout %26lt;%26lt; "This program uses the quadratic formula to solve a quadratic equation.\n"


%26lt;%26lt; "Enter Q to quit.\n";





while (SENTINEL= ('Q' || 'q')) //start sentinel


{


cout %26lt;%26lt; "Please input the A coefficient.\n";


cin %26gt;%26gt; A;


if (A == 0)


{


cout %26lt;%26lt; "Zero Divide.\n";


break;


}


denominator = A * 2;


cout %26lt;%26lt; "Please input the B coefficient.\n";


cin %26gt;%26gt; B;


cout %26lt;%26lt; "Please input the C coefficient.\n";


//to be continued

C++ How do I use a char as a sentinel value when all other inputs are integers? I want the while loop to check
/* I think this should do the trick */


#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;


#include %26lt;cmath%26gt;


using namespace std;





int main()


{


float A, B, C;


float discriminant, denominator, numerator, plusSolution, minusSolution, root;


char SENTINEL;


bool loop = true;


A = 1;


B = 5;


C = 1;


do{


beginloop:





cout %26lt;%26lt; "This program uses the quadratic formula to solve a quadratic equation.\n"


%26lt;%26lt; "Enter Q to quit or C to continue."%26lt;%26lt;endl;


cin%26gt;%26gt;SENTINEL;


if(SENTINEL == 'C' || SENTINEL == 'c'){


/************/


cout %26lt;%26lt; "Please input the A coefficient.\n";


cin %26gt;%26gt; A;


if (A == 0)


{


cout %26lt;%26lt; "Zero Divide.\n";


break;


}


denominator = A * 2;


cout %26lt;%26lt; "Please input the B coefficient.\n";


cin %26gt;%26gt; B;


cout %26lt;%26lt; "Please input the C coefficient.\n";


cin %26gt;%26gt; C;


discriminant = ((B*B) - 4*A*C);





if (discriminant %26lt; 0)


{


cout %26lt;%26lt; "No real roots.\n";


// break; Continue the program. Break gets you out of the While/Do loop


goto beginloop;


}


else if (discriminant %26gt;= 0)


{


root = sqrt(discriminant);


plusSolution = (-B + root)/denominator;


minusSolution = (-B - root)/denominator;


cout %26lt;%26lt; "The solutions are:\n"


%26lt;%26lt; plusSolution %26lt;%26lt; "\n"


%26lt;%26lt; minusSolution %26lt;%26lt; "\n\n";


}


}else{


if( SENTINEL == 'Q' || SENTINEL == 'q') loop = false;else{ goto beginloop;}


}


}while( loop );


/************/


cout%26lt;%26lt; "Press any alpha or numerical key and then the enter key to exit." %26lt;%26lt; endl;


char *pause;


cin%26gt;%26gt;pause;


// system("PAUSE");


return 0;


} //end main function


/* You want to check for the Q or q key before you continue to your main program .... then if it isnt the Q or q check for the */





http://en.wikipedia.org/wiki/Quadratic_e...


No comments:

Post a Comment