简体   繁体   English

主 cpp 外部 while 循环不 go 回到 switch 语句选项,内部 while 循环工作正常

[英]main cpp outer while loop does not go back into switch statements options, and inner while loop works fine

The program asks the user which department they would like to enter.该程序询问用户他们想进入哪个部门。 Goes into that department and then asks user what would they like to do inside that department.进入那个部门,然后询问用户他们想在那个部门做什么。 At end of whatever user would like to do the inner while loop asks "what is next?".在任何用户想做的事情结束时,内部 while 循环都会询问“下一步是什么?”。 If user enters "0" they are taken back to the question of "which department would they like to enter, user enters the index for corresponding department, but the loop does not go back into whichever department the user chooses.如果用户输入“0”,他们将返回“他们想进入哪个部门,用户输入相应部门的索引,但循环不会 go 回到用户选择的任何部门。

In short: the outer while loop executes, and proceeds to the switch statements with inner while loops, but when the user returns to the outer while loop - that loop does not reenter the user specified department.简而言之:执行外部 while 循环,并使用内部 while 循环继续执行 switch 语句,但是当用户返回外部 while 循环时 - 该循环不会重新进入用户指定的部门。

// MAIN PROPGRAM

#include "client.h" // include appropriate .h files
#include "salesrep.h"
#include "product.h"
#include "sales.h"

#include <iostream> // include appropriate libraries
#include <fstream>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>

using namespace std; // standard namespace

int main() {

    Client alpha; // client class object
    SalesRep beta; // sales rep class object
    Product gamma; // product class object
    Sales delta; // sales class object
    int index; // index to traverse main menu
    int subIndex; // index to traverse sub menu

    cout << "Welcome to Wedling Central; we are happy you are here!" << endl; // greeting
    cout << "Please select which department you would like to enter:" << endl; // orientation
    cout << "Clients (Enter: 1)" << endl; // directories
    cout << "Sales reps (Enter: 2)" << endl;
    cout << "Products (Enter: 3)" << endl;
    cout << "Sales (Enter: 4)" << endl;
    cout << "Quit (Enter: 0)" << endl; // option to exit
    cin >> index; // user input index for main menu
    cin.ignore(); // ignore cin after index input

    while (index != 0) { // while user does not wish to exit program

    switch(index) {
        case 1: // client department

        while (subIndex != 0){ // while user does not wish to return to main menu

            cout << "What would you like to do?" << endl; // prompt
            cout << "Modify client (Enter: 1)" << endl; // options
            cout << "Add new client (Enter: 2)" << endl;
            cout << "Get a client's info (Enter: 3)" << endl;
            cout << "View clients (Enter: 4)" << endl;
            cout << "Go back to main menu (Enter: 0)" << endl; // return to main menu

            cin >> subIndex; // user input for sub menu
            cin.ignore(); // ignore cin after sub index input
            

            switch(subIndex) { // options within department
                case 1:

                    alpha.SetModifyClient(); // update a client
                    
                    break; // return to sub menu

                case 2:

                    alpha.SetNewClient(); // add new client

                    break; // return to sub menu

                case 3:

                    alpha.GetSpecificClientInfo(); // view specific client's information

                    break; // return to sub menu

                case 4:

                    alpha.GetAllClients(); // view all clients

                    break; // return to sub menu

            }
        }

            break; // return to main menu

        case 2:

        while (subIndex != 0){ // while user does not wish to return to main menu

            cout << "What would you like to do?" << endl; // prompt
            cout << "Modify sales rep (Enter: 1)" << endl; // options
            cout << "Add new sales rep (Enter: 2)" << endl;
            cout << "Get a sales rep's info (Enter: 3)" << endl;
            cout << "View sales reps (Enter: 4)" << endl;
            cout << "View a sales rep's bonus (Enter: 5)" << endl;
            cout << "Go back to main menu (Enter: 0)" << endl;

            cin >> subIndex; // user input for sub menu
            cin.ignore(); // ignore cin after sub index input

            switch(subIndex) { // options within department
                case 1:

                    beta.SetModifySalesRep(); // update a sales rep

                    break; // return to sub menu 

                case 2:

                    beta.SetNewSalesRep(); // add new sales rep

                    break; // return to sub menu

                case 3:

                    beta.GetSpecificSalesRepInfo(); // view specific sales rep's information

                    break; // return to sub menu

                case 4:

                    beta.GetAllSalesReps(); // view all sales reps

                    break; // return to sub menu

                case 5:

                    beta.SalesRepBonus(); // view a sales rep's bonus

                    break; // return to sub menu

            }
        }

            break; // return to main menu

        case 3:

        while (subIndex != 0) // while user does not wish to return to main menu
        {
            cout << "What would you like to do?" << endl; // prompt
            cout << "Modify product (Enter: 1)" << endl; // options
            cout << "Add new product (Enter: 2)" << endl;
            cout << "Get a product's info (Enter: 3)" << endl;
            cout << "View products (Enter: 4)" << endl;
            cout << "View product sales (Enter: 5)" << endl;
            cout << "Go back to main menu (Enter: 0)" << endl;

            cin >> subIndex; // user input for sub menu
            cin.ignore(); // ignore cin after sub index input

            switch (subIndex)
            {
            case 1:

                gamma.SetUpdateProduct(); // update product's information

                break; // return to sub menu

            case 2:

                gamma.SetNewProduct(); // add new product

                break; // return to sub menu 

            case 3:

                gamma.GetSpecificProductInfo(); // view specific product's informatioin

                break; // return to sub menu

            case 4:

                gamma.GetAllProduct(); // view all products

                break; // return to sub menu
            
            case 5:

                gamma.MonthlySalesReport(); // view monthly sales report

                break; // return to sub menu

            }
        }    

            break; // return to main menu

        case 4:

        while (subIndex != 0) // while user does not return to main menu
        {
            cout << "What would you like to do?" << endl; // prompt
            cout << "Purchase a product (Enter: 1)" << endl; // options
            cout << "View sales for the year (Enter: 2)" << endl;
            cout << "View sales for a specific client (Enter: 3)" << endl;
            cout << "Update client's sale (Enter: 4)" << endl;
            cout << "Go back to main menu (Enter: 0)" << endl;
            
            cin >> subIndex; // user input for sub menu
            cin.ignore(); // ignore cin after sub index input

            switch (subIndex)
            {
            case 1:

                delta.PurchaseProduct(); // purchase a product

                break; // return to sub menu
                
            case 2:

                delta.ViewAllSales(); // view sales

                break; // return to sub menu
            
            case 3:

                delta.ViewClientSales(); // view specific client sales

                break; // return to sub menu
            
            case 4:

                delta.UpdateClientSales(); // update client sales

                break; // return to sub menu

            }
        }

        break; // return to main menu
        
    }

    cout << "What is next?" << endl; // main menu return greeting
    cout << "Please select which department you would like to enter:" << endl; // department orientation
    cout << "Clients (Enter: 1)" << endl; // options
    cout << "Sales reps (Enter: 2)" << endl;
    cout << "Products (Enter: 3)" << endl;
    cout << "Sales (Enter: 4)" << endl;
    cout << "Quit (Enter: 0)" << endl; // exit index

    cin >> index; // user input
    cin.ignore(); // ignore cin after index input

    }

    cout << "Have a good day - thanks for stopping by Welding Central!" << endl; // farewell prompt

    return 0; // end main
}

any help would be appreciated任何帮助,将不胜感激

Clear out subindex in the outer loop.清除外循环中的子索引。 What is probably happening is that you get an index of 1 and do the first of the switch statements.可能发生的情况是您得到一个索引 1 并执行第一个 switch 语句。 You then get subindexes for a while until they hit 0, then you go back to the outer loop.然后你得到子索引一段时间,直到它们达到 0,然后你 go 回到外循环。 Pick 2 this time.这次选2。

But subindex already has a 0 in it, so the while loop exits right away.但是 subindex 中已经有一个 0,所以 while 循环立即退出。

This code would be a LOT safer if you made better use of functions.如果你更好地使用函数,这段代码会更安全。 Each of the inner loops could be a separate function.每个内部循环都可以是一个单独的 function。 That would eliminate variables drifting from one of the switch conditions to the next one the next time around.这将消除下一次从一个切换条件漂移到下一个切换条件的变量。 And the code would be more readable.并且代码将更具可读性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM