how to create one program consisting four program inside

how to make a program that has ( for loop, while, do while, array ) on it.

example
when you press 1 for loop will appear and run,
when you press 2 array will appear and run,


thank you.
Last edited on
You have to have a menu of options, say 1 for for loop, 2 for while, etc and then a switch statement with cases corresponding to the options
yeah, but i dont have an idea to do that. :(
OK, at least post the code for your separate bits - for loop, while etc and then I'll try and help you to make a switch statement out of them
for loop.


#include <iostream>
using namespace std;

int main ()
{

for( int a = 10; a < 20; a = a + 1 )
{
cout << "value of a: " << a << endl;
}

return 0;
}





do while

#include <iostream>
using namespace std;

int main ()
{

int a = 10;


do
{
cout << "value of a: " << a << endl;
a = a + 1;
}while( a < 20 );

return 0;



}






while


#include<iostream>
using namespace std;

int main()
{
int counter, howmuch;

cin >> howmuch;

counter = 0;
while ( counter < howmuch)
{
counter++;
cout << counter << '\n';
}
return 0;
}
is that what you asking?
ARRAY

#include<iostream.h>
#include<stdio.h>
#include<conio.h>

int main()

{

double distance[] = {11.1, 12.2, 13.2, 14.3, 15.6};

cout<<"2nd member = " <<distance[1] << endl;
cout<<"5th member = " <<distance[4] << endl;

getch;
return 0;

}
closed account (LA48b7Xj)
You could structure your program 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
using namespace std;

void program_1()
{
    
}

void program_2()
{
    
}

void program_3()
{
    
}

void program_4()
{
    
}

int main()
{
    cout << "Please enter 1-4\n";

    int program_option;
    cin >> program_option;

    switch(program_option)
    {
    case 1:
        program_1();
        break;
    case 2:
        program_2();
        break;
    case 3:
        program_3();
        break;
    case 4:
        program_4();
        break;
    }
}

And you can add some bells and whistles like (a) having the menu option repeat itself until you choose to exit and (b) checking against out of range switch values:

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<iostream>
using namespace std;

void for_program();
void dowhile_program();
void while_program();
void array_program();

int main(){
	
	bool fQuit = false;
	int choice;
		
	while(!fQuit){
		
		cout<<"Please choose one of the following options: "<<endl;
		cout<<"1. Run the for loop"<<endl;
		cout<<"2. Run the do-while"<<endl;
		cout<<"3. Run the while"<<endl;
		cout<<"4. Run the array"<<endl;
		cout<<"5. Exit"<<endl;
		
		cin>>choice;
		switch (choice){
			case 1:
				for_program();
					break;
			case 2: 
				dowhile_program();
					break;
			case 3:
				while_program();
					break;
			case 4: 
				array_program();				
				break;
			case 5: 
				cout<<"You've chosen to exit, Goodbye!"<<endl;
				fQuit = true;
				break;
			default: 
				cout<<"Please choose valid option number!"<<endl;
				break;
			}
	}
}

void for_program(){
	for( int a = 10; a < 20; a++ ){
					cout << "value of a: " << a << endl;
	}
}
void dowhile_program(){
	int a = 10;
				do{
					cout << "value of a: " << a << endl;
					a ++;
				}
					while( a < 20 );

}
void while_program(){
	int counter, howmuch;
				cout<<"Enter variable 'howmuch'"<<endl; 
				cin >> howmuch;
				counter = 0;
				while ( counter < howmuch){
				cout << counter <<endl;
				counter++;
				}
}
void array_program(){
	double distance[] = {11.1, 12.2, 13.2, 14.3, 15.6};
				cout<<"2nd member = " <<distance[1] << endl;
				cout<<"5th member = " <<distance[4] << endl;
}
Last edited on
thank you so much everyone! :))
Topic archived. No new replies allowed.