Help with a While loop.

Hey guys, a bit of a C++ beginner here, just need some help figuring out where to add a while loop that will ask the user if they want to solve another equation with a (y/n). Code below works and looks like it supposed to, just need a loop in here. Any help would be awesome.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <cmath>
using namespace std;

int main()
{

double a, b, c;
double root1;
double root2;
double discriminant;

cout.setf(ios::fixed);     //
cout.setf(ios::showpoint); //Magic Formula
cout.precision(2);         //

cout << "Enter the value of a: ";
cin >> a;
cout << "Enter the value of b: ";
cin >> b;
cout << "Enter the value of c: ";
cin >> c;

cout << "For the equation " << a << "(x * x) + " << b << "x + " << c << " = 0\n";

discriminant = (pow(b,2) - 4*a*c);
root1 = (((-b) + sqrt(discriminant))/(2*a));
root2 = (((-b) - sqrt(discriminant))/(2*a));


if (discriminant > 0)
    {
    cout << "The roots are " << root1 << " and " << root2 <<endl;
    }
else
    {
    cout << "No real roots.\n";
    }


return(0);
}
one way to do this is to declare a variable

 
char another_calc='y';


then start your while loop on line 16

1
2
3
4
5
6
while (another_calc == 'y')
{
...
cout << "Another calc? ";
cin >> another_calc;
}


with the loop ending on line 39
Thanks for the reply if i want it to recognize a capital "Y" and also the lower case "y" , how would i set that variable up? I know it has to do with the ll command. Would it be something like this?

 
char another_calc = 'y' || 'Y';


1
2
3
4
5
6
while (another_calc == 'y' || 'Y')
{
...
cout << "Another calc? ";
cin >> another_calc;
}


And if the user inputs an 'n' or 'N' where would a line like
 
cout << "End of program";

go?
Last edited on
When dealing with the || (Logical OR) operator (and every other operator for that matter), each statement is separate from the other. You must say it twice, as in:
while (another_calc == 'y' || another_calc == 'Y')

Edit: Also that isn't how you initialize a char, (x || y) returns a bool, not a char. Just declare it and you can initialize it using cin as you seem to already be doing.

For your code, I would suggest a "do while" loop.
1
2
3
4
5
6
7
8
9
10
11

char another_calc;

// ...

do 
{
    //stuff
    cin >> another_calc;
    cout << "Another calc? ";
} while (another_calc == 'y' || another_calc == 'Y');

but either way would work.
Last edited on
Thanks for the help guys, this ended up working.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <cmath>
using namespace std;

int main()
{

double a, b, c;
double root1;
double root2;
double discriminant;
char another_calc;

cout.setf(ios::fixed);     //
cout.setf(ios::showpoint); //Magic Formula
cout.precision(2);         //


do
{
cout << "Enter the value of a: ";
cin >> a;
cout << "Enter the value of b: ";
cin >> b;
cout << "Enter the value of c: ";
cin >> c;

cout << "For the equation " << a << "(x * x) + " << b << "x + " << c << " = 0\n";

discriminant = (pow(b,2) - 4*a*c);
root1 = (((-b) + sqrt(discriminant))/(2*a));
root2 = (((-b) - sqrt(discriminant))/(2*a));


if (discriminant > 0)
  {
  cout << "The roots are " << root1 << " and " << root2 <<endl;
  }
else
  {
  cout << "No real roots.\n";
  }

cout << "Do you want to solve another equation (y/n)?";
cin >> another_calc;
}
while (another_calc == 'y' || another_calc == 'Y');

if (another_calc != 'y' || another_calc != 'Y')
  {
  cout << "End of program\n";
  }

return(0);

}


For some reason i just cant get a normal while loop to work. If someone could edit the code for a normal while loop that would be cool, id like to see both.
closed account (48T7M4Gy)
Before you go too far. Try your original program with:

if (disciminant >=0);

to get it to work with a while loop, just use my original suggestion, except change the while condition

 
while (another_calc = 'y' || another_calc = 'Y')
Thanks for all the help, got the normal while loop to work, had the end bracket in the wrong spot the first time.
Topic archived. No new replies allowed.