功能永远不会在Switch语句中结束?

时间:2017-03-27 15:08:59

标签: c++ function class switch-statement

我在使用switch语句中断函数时遇到了一些问题。我希望程序运行的方式是: -user从菜单中选择一个选项 功能完成 - 返回主菜单。

但是,即使已经输入了详细信息,“setuserdetails”函数仍会重复执行:任何建议?提前谢谢。

users.h

#ifndef USERS_H
#define USERS_H


#include <string>


using namespace std;
class Users

{
public:
    //Users();
    void SetUserDetails(string &sName, string &sSurname, string &sEmail, int &iAge, char &Gender);
    void ReturnUserDetails(string &sName, string &sSurname, string &sEmail, int &iAge, char &Gender);
private:
    string sName;
    string sSurname;
    string sEmail;
    int iAge;
    char cGender;


};
#endif 

users.cpp

#include "stdafx.h"
#include "users.h"
#include <iostream>
#include <string>


using namespace std;

/*Users::Users()
{

}*/
void Users::SetUserDetails(string &sName, string &sSurname, string &sEmail, int &iAge, char &Gender)
{

        cout << "Welcome to the Customer Details Section" << endl; //enter user details
        cout << "Please Enter Your Name: " << endl;
        cin >> sName;
        cout << "Please Enter Your Surname: " << endl;
        cin >> sSurname;
        cout << "Welcome " << sName << " Please Now enter Your Email Address: " << endl;
        cin >> sEmail;
        cout << "Now Please Enter Your age  " << sName << " : " << endl;
        cin >> iAge;
        cout << "Finally, Please enter your Gender (as M for male or F for Female): " << endl;
        cin >> cGender;
        return;
}


void Users::ReturnUserDetails(string &sName, string &sSurname, string &sEmail, int &iAge, char &Gender)
{
    cout << "Thank you for logging on to the system: your Details are as follows:" << endl; //show user details, printed out to console 

    cout << "Name: " << sName << " " << sSurname << endl;
    cout << "Email Address: " << sEmail << endl;
    cout << "Age: " << iAge << " Gender: " << cGender << endl;

    cout << "Please start ordering your Items!" << endl;

    return;

}

的main.cpp

#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h>
#include "menu.h"
#include "users.h"

using namespace std;


int main()
{
    //declare variables
    //variables for main menu navigation
    Menu menu; //object of the menu class 
    Users user; //object of the user class 
    menu.ShowMainMenu();
    int iChoice; //user input 
    cin >> iChoice; //get user input

    //user details 
    string sFirstName; //intialise string 
    string sSurname;
    string sEmailAddress;
    int iAge = 0; //intialise char
    char cGender ='a'; //intialise char


    do
    { 


        switch (iChoice)
        {
            //if user presses 1
        case 1:
            for (int iCount = 0; iCount < 1; iCount++)
            {
                user.SetUserDetails(sFirstName, sSurname, sEmailAddress, iAge, cGender); // get user details
                user.ReturnUserDetails(sFirstName, sSurname, sEmailAddress, iAge, cGender); //show user details
                if (iCount == 1)
                {
                    cout << "test" << endl; // never appears

                    break;
                }
            }
                break;
            //if user presses 2
        case 2:
            cout << "test" << endl;
            break;
            //if user presses 3
        case 3:
            break;
            //if user presses 4
        case 4:
            break;
            //default action
        default:
            menu.ShowMainMenu();
            break;


        }


    } while (iChoice != 0);

    return 0;
}

1 个答案:

答案 0 :(得分:2)

只要do-while不为0,您的iChoice循环就会循环播放。iChoice无法在循环内更改 - 您只能在do之前阅读一次。

(从不打印您的诊断test字符串,因为在一次for - 循环迭代后,i增加到1并且for - 循环立即终止。)