Jawad Adil
Jawad Adil

Reputation: 67

how to make my program run again C++

I have given the following question.

Write a menu driven program with following options:

  1. Add new value
  2. Search value
  3. Modify value
  4. Print value
  5. Print sum of all values
  6. Quit / Terminate

You have to create 5 options as five function. Add another function to show menu options.

and here is my code:

#include<iostream>
using namespace std;
float f[100]={0};
    //1st option
void AddNewValue(){int input;
cout<<"Enter a value\n";
cin>>f[input];
}
//2nd option 
void SearchValue(){int i, search;
    cout<<"Enter a value to search\n";
    cin>>search;
    int match=0;
    for (int i=1;i<=100;i++)
    {if (f[i]==search)
    {match=1;
    break;}
    }
    if (match==1){cout<<"Matched value found\n";
    }
    else {cout<<"No match found\n";}        
}
//3rd option
void ModifyValue(){int input1;
    cout<<"Enter the position at which you want to modify value\n";
    cin>>input1;
    cout<<"Enter a value\n";
    cin>>f[input1-1];
}
//4th option
void PrintValue(){int i;
for (i=1;i<=100;i++)
    {cout<<f[i]<<' ';}
}
//5th option
void PrintSum(){int i,sum;
    for(i=1;i<=100;i++)
    {sum=f[i]+f[i+1];}
    cout<<"Sum is : "<<sum;
}
//starting Function
void menu(){int x;
    cout<<"Enter an option: \n";
    cout<<"1- Add new value\n2- Search Value\n3- Modify value\n4- Print Value\n5- Print sum of all values\n6- Quit/terminate\n";
    cin>>x;
    if(x==1){
        AddNewValue();
    }
    else if (x==2){
        SearchValue();
    }
    else if(x==3){
        ModifyValue();
    }
    else if(x==4){
        PrintValue();
    }
    else if(x==5){
        PrintSum();
    }
    else{
    }
}
int main(){

        menu();
}

And I want to make my full program run again and again until the user enter a wrong option.

Upvotes: 0

Views: 14600

Answers (4)

Mahesh Suthar
Mahesh Suthar

Reputation: 146

You can use a loop and make it run untill user give a specific input. In below code once the program will run and then it will ask user to input 5 to exit or any other key to run again, and if user gives 5 as input, then it will stop or if user gives any other input then program will run again.

Hope it is clear and help you.

#include<iostream>
using namespace std;
float f[100]={0};
    //1st option
void AddNewValue(){int input;
cout<<"Enter a value\n";
cin>>f[input];
}
//2nd option 
void SearchValue(){int i, search;
    cout<<"Enter a value to search\n";
    cin>>search;
    int match=0;
    for (int i=1;i<=100;i++)
    {if (f[i]==search)
    {match=1;
    break;}
    }
    if (match==1){cout<<"Matched value found\n";
    }
    else {cout<<"No match found\n";}        
}
//3rd option
void ModifyValue(){int input1;
    cout<<"Enter the position at which you want to modify value\n";
    cin>>input1;
    cout<<"Enter a value\n";
    cin>>f[input1-1];
}
//4th option
void PrintValue(){int i;
for (i=1;i<=100;i++)
    {cout<<f[i]<<' ';}
}
//5th option
void PrintSum(){int i,sum;
    for(i=1;i<=100;i++)
    {sum=f[i]+f[i+1];}
    cout<<"Sum is : "<<sum;
}
//starting Function
void menu(){int x;
    cout<<"Enter an option: \n";
    cout<<"1- Add new value\n2- Search Value\n3- Modify value\n4- Print Value\n5- Print sum of all values\n6- Quit/terminate\n";
    cin>>x;
    if(x==1){
        AddNewValue();
    }
    else if (x==2){
        SearchValue();
    }
    else if(x==3){
        ModifyValue();
    }
    else if(x==4){
        PrintValue();
    }
    else if(x==5){
        PrintSum();
    }
    else{
    }
}
int main(){

int repeater;
do{
        menu();
cout<<"Enter 5 to exit or any other key to run the program again :";
cin>>repeater;
}while(repeater != 5);
}

Upvotes: 1

Devansh
Devansh

Reputation: 313

If the user selects a correct option, call the menu function again. Otherwise, return control to main. I think this may work.

 int menu(){int x;
    cout<<"Enter an option: \n";
    cout<<"1- Add new value\n2- Search Value\n3- Modify value\n4- Print Value\n5- Print sum of all values\n6- Quit/terminate\n";
    cin>>x;
    if(x!=1||x!=2||x!=3||x!=4||x!=5) return 0;
    if(x==1){
        AddNewValue();
    }
    else if (x==2){
        SearchValue();
    }
    else if(x==3){
        ModifyValue();
    }
    else if(x==4){
        PrintValue();
    }
    else if(x==5){
        PrintSum();
    }
    menu();
}

You could also wrap your code in a do-while loop.

void menu(){
    do{
        int x;
        cout<<"Enter an option: \n";
        cout<<"1- Add new value\n2- Search Value\n3- Modify value\n4- Print Value\n5- Print sum of all values\n6- Quit/terminate\n";
        cin>>x;     
        if(x==1){
            AddNewValue();
        }
        else if (x==2){
            SearchValue();
        }
        else if(x==3){
            ModifyValue();
        }
        else if(x==4){
            PrintValue();
        }
        else if(x==5){
            PrintSum();
        }

    }while(x==1||x==2||x==3||x==4||x==5);
}

Upvotes: 0

YSC
YSC

Reputation: 40060

Keep it simple. The function in charge of displaying the menu should only display the menu and return the user's choice.

enum choice
{
    ADD_NEW_VALUE,
    SEARCH_VALUE,
    // ...
    QUIT
};

choice menu()
{
    choice result = QUIT;
    // display menu
    // input choice
    // (the more I think about it, the more I'd split it in two separate functions)
    return result;
}

int main()
{
    while (true) {
        switch (menu())
        {
        case ADD_NEW_VALUE: AddNewValue(); break;
        // ...
        case QUIT: return 0;
        }
    }
}

This helps menu()'s testing and make it easier to add new options and new features.

Upvotes: 0

Christopher Phoenix
Christopher Phoenix

Reputation: 19

One option available to you is to create a while loop encapsulating the body of your menu() function, and using break; if the user enters 6.

void menu(){
    int x = 0;
    while(1){
        cin >> x;
        //your code here
        if(x==6)
            break;
    }
}

This example will cause your menu to repeat until the user enters 6.

The purpose of break; is to 'break' from the while loop. In this case, breaking will end your menu()and return to main().

Further, it is good practice to add return 0; to the end of your main function.

Upvotes: 1

Related Questions