Oct 15, 2016 at 10:23am UTC
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 Oct 15, 2016 at 10:25am UTC
Oct 15, 2016 at 11:04am UTC
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
Oct 15, 2016 at 1:14pm UTC
yeah, but i dont have an idea to do that. :(
Oct 15, 2016 at 4:00pm UTC
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
Oct 15, 2016 at 4:40pm UTC
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;
}
Oct 15, 2016 at 5:02pm UTC
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;
}
Oct 16, 2016 at 2:01am UTC
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 Oct 16, 2016 at 2:04am UTC
Oct 16, 2016 at 3:23am UTC
thank you so much everyone! :))