Need Help with loop.

Hello everyone. I'm quite new to the programming world, but after few weeks of learning i made a programm that calculates the roots of a quadratic equation,but i can't seem to understand how to get that programm to repeat itself after it is finished, so i don't have to reopen it everytime i need to use it. I would be happy if anyone would write me the solution. BTW if you are wondering what that cin >> i; is doing there, it's there because otherwise the compiler shuts down after the roots are found.

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
#include <iostream>
#include <math.h>

using namespace std;


int main ()
{
    
    {
    int a,b,c,i,disc;
    float x1,x2;
    cout << "This programm finds the roots of a quadratic equation" << endl;
    cout << "ax^2 +- bx +- c" << endl;
    cout << "Enter values, a,b and c: " << endl;
    cout << "a: ";
    cin >> a;
    cout << "b: ";
    cin >> b;
    cout << "c: ";
    cin >> c;
    disc = ((b*b) -4*a*c);
    if (disc > 0)
    {
       cout << "The discriminant is: " << disc << endl;
       x1 = (-b +sqrt((b*b) -4*a*c))/2*a;
       x2 = (-b -sqrt((b*b) -4*a*c))/2*a;
       
       
       
    }
    else if (disc ==0)
    {
         cout << "The discriminant is: " << disc;
         x1=x2 = (-1*b)/(2*a);
         
    }
    else 
    {
         disc =-1*disc;
    cout << "No answer. Discriminant is less than 0";
    cin >> i;
    
    }
    cout << "X1 =" << x1 << endl;
    cout << "X2 =" << x2 << endl;
    cin >> i;
    return 0;
} 
}     
 
Last edited on
This code may help you:
1
2
3
4
5
6
7
8
9
10
int main()
{
	do {
		/* Here goes your code */
		cout << "Do you want to close your program? Press N if you want to." << endl;
		char Answer = 0;
		cin >> Answer;
	} while (Answer != 'n' && Answer != 'N');
	return 0;
}

EDIT: And please use [code][/code] tags.
Last edited on
there are two basic loop structures, for and while.
while is the simpler of the two:
1
2
3
4
5
//while loop
x = 0; 
while(/*this remains true*/ x < 10){
x++;// your code here
} 


the for loops allows more control over the number of times a loop executes, for example
1
2
3
for(int i = 0/*define a variable*/; i < 10/*same thing here as with while loop*/; i++/*this happens AFTER the code below executes*/){
std::cout << "*" << i;
}
Ok i got how to make a loop in the way you showed, but still i just can't figure out how to make it work in this program . I would like it to ask if you want to restart the program in all 3 situations (with disc> , < or equal to 0). if you have time and ideas, then i would be pleased to see your answer.
I already wrote you everything you need.
so you would do

//your libraries here

int main(){
//define variables
bool cycle = true;

while(cycle){

//your code here
//more code
//and yet more code

std::cout << "would you like to run the program again?;
getline(std::cin, reqcycle);

if(reqcycle == "no") cycle = false;

}

return 0;
}
Last edited on
Last one looks more like the one i need + haven't yet tried any of bool functions. Will try it a bit later, thanks.
So finally i got it working and understood how the loop works!

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
57
58
59
60
61
#include <iostream>
#include <math.h>


using namespace std;
bool Repeat = true;
int main ()
{  

{
    char symbol;
    int a,b,c,i,disc;
    float x1,x2;
    while (Repeat){
    cout << "This programm finds the roots of a quadratic equation" << endl;
    cout << "ax^2 +- bx +- c" << endl;


    cout << "Enter values, a,b and c: " << endl;
    cout << "a: ";
    cin >> a;
    cout << "b: ";
    cin >> b;
    cout << "c: ";
    cin >> c;
    disc = ((b*b) -4*a*c);
    

    
    if (disc > 0)
    {
       cout << "The discriminant is: " << disc << endl;
       x1 = (-b +sqrt((b*b) -4*a*c))/2*a; 
       x2 = (-b -sqrt((b*b) -4*a*c))/2*a;
       }
    else if (disc ==0)
    {
         cout << "The discriminant is: " << disc << endl;
         x1=x2 =(-1*b)/(2*a);

         
    }
    else 
    {
         disc =-1*disc;
    cout << "No answer. Discriminant is less than 0"<< endl;
    cout << "Would you like to calculate again (Y or N): ";
    cin >> symbol;
    Repeat = (symbol == 'Y' || symbol == 'y') ? true : false;
    
    }
    cout << "X1: " << x1 << endl;
    cout << "X2: " << x2 << endl; 
    cout << "Would you like to calculate again (Y or N): ";
    cin >> symbol;
    Repeat = (symbol == 'Y' || symbol == 'y') ? true : false;
}
    return 0;

}     
}
Topic archived. No new replies allowed.