Loop Issues

So for my very first program I decided to make a quadratic formula solver. It works well but I am having problems with my loop. For some reason it repeats no matter what I enter. What am I doing wrong?

Also, how would I do this same kind of loop structure but with strings (I.E., type yes to do it again)?


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
/*
  Name: "Quadratic Formula Solver"
  Author: Matt Cromer
  Date: 08/11/11 17:09
  Description: Used to solve the quadratic formula to find X intercepts. 
*/

#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;





int main ()

{
  int n;
  do {
    double a,b,c,x_1,x_2;
    int n;
     
     system("cls");
     
cout << "Enter A please. > ";
cin >> a;
cout << "Enter B please. > ";
cin >> b;
cout << "Enter C please. > ";
cin >> c;
    
    x_1=(-b + sqrt(pow (b,2)-4*a*c))/(2*a);
    x_2=(-b - sqrt(pow (b,2)-4*a*c))/(2*a);
    
cout << endl << "X = " << x_1 << " AND X = " << x_2 << endl;
    
    
  cout << endl << "Done? Enter 1 to end or 2 to do another!. > ";
  cin >> n;
  
} while (n != 1);
}


Thanks a ton


Oh, one more thing:

If I input weird numbers, like a=1,b=2,c=3, I get this:
"-1.#ind"
What am I doing wrong that makes this display improperly?
You're defining n twice, and the compiler doesn't like it. I'd move all your variable declarations to the top of your program (where your first "int n" is).

And, it appears if you put "weird" numbers in, your program chokes on the sqrt() function (probably because you're trying to take the sqrt of a negative number). The output you're getting is just telling you you've ended up with an invalid/undefined result.
Last edited on
Ohhhhh I see, thank you so much.
And as for the weird numbers thing, I feel really stupid for asking that one heheh..

Also, how would I do this same kind of loop structure but with strings (I.E., type yes to do it again)?
If you've got time, I'd appreciate it if you took a look at this thread also:

http://www.cplusplus.com/forum/beginner/54527/
You'd use strings the same way you use the int, but with a string variable, and string comparators to process the user input. You'll have to do some stuff to make sure that you've covered all combinations of case, so that "yes" and "YES" and even "Yes" are accepted.

Do a little reading on strings, and I think you'll quickly get the hang of it. Try to use C++ style strings, which are an object, and not C style strings, which are just character arrays. Good habit to get into.
Topic archived. No new replies allowed.