Q: In what situation does this program print something?
1 2 3 4 5 6 7 8 9 10 11
|
#include <iostream>
int main ( )
{
float foo;
if ( foo != -999 )
{
std::cout << "Hello\n";
}
return 0;
}
|
A: Nobody knows. Why? The value of foo has not been set.
Tip: a conditional expression can have more than one subexpression.
WHILE ( can read foo AND foo is not -999 ) |
If the user enters -999, the program must terminate without prompting for b and c. |
1 2 3 4 5
|
cin >> a;
cout<< "Enter value b: ";
cin >> b;
cout << "Enter value c: ";
cin >> c;
|
Whoa! You got a from the user, but you ask for b and c no matter what value the a has.
1 2 3 4 5 6 7
|
float a;
float c;
// code
while (a != -999) // you compare floats
{
// code
if ((a - 7.0 * c) == 0.0) // you compare doubles
|
Equality of two floating point values is not trivial. Endless pages have been written on the subject.
Who says that the a, b, c must be floats? Can they be int? The division must be done with floats though.
You have to compute
1 2
|
r + sqrt( s ) / t
r - sqrt( s ) / t
|
... and you do that, but you do compute the r, s, t multiple times. Compute once, store result, and then use the result.
Xcode is not affecting anything.
There is one requirement on the task that is not related to C++:
clear the screen at the beginning off the run |
That does not depend on Xcode. It does not depend on OS either. The required code does depend on the terminal that is used when running the program.
IMHO C++ beginner homework should never have such stuff.