我在switch语句中中断我的功能时遇到了一个问题。我希望程序工作的方式是:
-用户从菜单中选择一个选项
功能完成
-返回主菜单。
但是,即使输入了详细信息,“ 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;
}
最佳答案
只要do-while
不为0,您的iChoice
循环就会循环。但是iChoice
不能在循环内更改-您只能在do
之前读取一次。
(永远不会打印诊断test
字符串,因为在进行一次for
循环迭代后,i
会增加到1,并且for
循环会立即终止。)