Newbie , trying to make program repeat (loop)

hey guys!

Im trying to learn how to make a program repeat itself and then terminate it with a y/n function ? i know i use a "while ( again == y)" line like that im just not sure were ?


Heres my source file (I think it goes here ?) could someone show me how to accomplish this ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "vehiclee.h" //include the declaration of the class
#include "Motorbike.h"
#include <iostream>
using namespace std;

int main() {

	vehicle vehicle ; //object of the class vehicle
	cout << "\nThe speed is: " << vehicle.getSpeed(); //shows speed on screen

	motorbike motorbike;
	cout << "\nThe Number Of Wheels is: " << motorbike.askWheels();
	cout << "\nThe Bike Type is: " << motorbike.askBikeType();
	cout << "\nThe Radius Of The Petrol Tank is: " << motorbike.askTankRadius();
	cout << "\nThe Length Of The Petrol Tank is: " <<motorbike.askTankLength();
    cout << "\nThe Tank Volume is: " << motorbike.getTankVolume();
	return 0;
}
You could use a do-while loop for the code to execute once without checking if again is y:

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
#include "vehiclee.h" //include the declaration of the class
#include "Motorbike.h"
#include <iostream>
using namespace std;

int main() {
    char again = 'y';
    
    do {
        vehicle vehicle ; //object of the class vehicle
        cout << "\nThe speed is: " << vehicle.getSpeed(); //shows speed on screen
        
        motorbike motorbike;
        cout << "\nThe Number Of Wheels is: " << motorbike.askWheels();
        cout << "\nThe Bike Type is: " << motorbike.askBikeType();
        cout << "\nThe Radius Of The Petrol Tank is: " << motorbike.askTankRadius();
        cout << "\nThe Length Of The Petrol Tank is: " <<motorbike.askTankLength();
        cout << "\nThe Tank Volume is: " << motorbike.getTankVolume();
        
        cout << "\nWould you like to repeat (y or n): ";
        cin >> again;
    } while (again == 'y');
    
    return 0;
}
Oh ok know i understand thank you :D but lets say i wanted the program to repeat itself automatically then after lets say 3 repeats it asks the user to enter again
You could do something like this:
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
#include "vehiclee.h" //include the declaration of the class
#include "Motorbike.h"
#include <iostream>
using namespace std;

int main() {
    char again = 'y';
    
    do {
        static int iterations = 0;

        vehicle vehicle ; //object of the class vehicle
        cout << "\nThe speed is: " << vehicle.getSpeed(); //shows speed on screen
        
        motorbike motorbike;
        cout << "\nThe Number Of Wheels is: " << motorbike.askWheels();
        cout << "\nThe Bike Type is: " << motorbike.askBikeType();
        cout << "\nThe Radius Of The Petrol Tank is: " << motorbike.askTankRadius();
        cout << "\nThe Length Of The Petrol Tank is: " <<motorbike.askTankLength();
        cout << "\nThe Tank Volume is: " << motorbike.getTankVolume();
        
        iterations++;
        
        if (iterations >= 3) {
            cout << "\nWould you like to repeat (y or n): ";
            cin >> again;
        }
    } while (again == 'y');
    
    return 0;
}
Topic archived. No new replies allowed.