菜单驱动器输入DO循环

Menu Drive Input Do Loop

本文关键字:循环 DO 输入 驱动器 菜单      更新时间:2023-10-16

我需要为我的编程类创建菜单驱动的输入[案例/开关语句]。我的教授还没有教我们如何创建菜单,而我很难理解我的教科书。我需要菜单循环,直到用户选择退出以终止程序为止。我还需要一条错误消息,其中包括案例/开关中的默认值,错误消息必须描述错误由用户引起。

有人可以指导我做什么吗?我只需要开始,其余的通常对我来说是自然的。

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
ofstream ofs("bafia_lab5.txt");
string msg = "eofmessage ";
string cno = "blank ";
string name = "blank ";
string dat = "2/16/17 ";
string lab = "blank ";
string phn = "blank ";
string sum = "Create  menu that allows users to choose between While Loop, Do Loop, For Loop and quit. ";
string whlo = "While Loop:";
string dooo = "Do While Loop:";
string forro = "For While Loop";
int counter;
int option;
//Header for lab with name, class#, due date, and lab number
void hdr()
{
    ofs << name << cno << dat << lab << endl;
    ofs << endl;
}
void menu()
{
    do 
    {
        ofs << "1. Do Loop " << dooo << endl;
        ofs << "2. While Loop " << whlo << endl;
        ofs << "3. For Loop" << forro << endl;
        ofs << "4. Quit " << endl;
        ofs << endl;
    } while (option <= 4)
}

//Function for "while loop"
void whl()
{
    ofs << whlo << endl;
    counter = 1;
    while (counter <= 10)
    {
        ofs << counter << endl;
        counter++;
    }
    ofs << endl;
}
//Function for "do while loop"
void doo()
{
    ofs << dooo << endl;
    counter = 1;
    do
    {
        ofs << counter << endl;
        counter++;
    } while (counter <= 10);
    ofs << endl;
}
//Function for "for loop"
void forr()
{
    ofs << forro << endl;
    for (counter = 1; counter <= 10; counter++)
    {
        ofs << counter << endl;
    }
    ofs << endl;
}
//Function for description of lab
void ftr()
{
    ofs << sum << endl;
    ofs << endl;
}
//End of file function with name, class#, due date, and lab number
void eof()
{
    ofs << msg << name << cno << dat << lab << endl;
}
//Call all functions
int main()
{
    hdr();
    menu();
    whl();
    doo();
    forr();
    ftr();
    eof();
    return 0;
}

您可以做这样的事情:

char choice = 'y';
while(choice == 'y' || choice == 'Y'){
    cout << "Enter choice : ";
    cin >> choice;
    switch(choice) {
         case 'a' : cout << "This is case-an";
                    //do something
                    break;
         ...
         default : //invalid choice alert
                    break;
    }
    cout << "Enter y/Y to continue else anything else to exit : ";
    cin >> choice;
}