Reputation: 1
I am having a bit of an issue getting my functions to break in my switch statement. The way I want the program to work is : -user selects an option from menu -function completes -returns to main menu.
However, the "setuserdetails" function keeps executing repeatedly even after the details have already been entered: any advice? thanks in advance.
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;
}
Upvotes: 0
Views: 66
Reputation: 46667
Your do-while
loop will loop as long as iChoice
is not 0. But iChoice
cannot change within the loop - you only read it once, before the do
.
(Your diagnostic test
string is never printed because after one for
-loop iteration, i
increases to 1 and the for
-loop immediately terminates.)
Upvotes: 2