Can anyone tell me why it loops my menu() twice after i enter the numbers from Add(). I know it has to do something with cin.ignore() but I don't know where to place it so it doesn't loop my Menu() after.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Time
{
int Minutes;
double Hours;
public:
// Mutators
void SetHours (double Hrs)
{
Hours = Hrs;
}
void SetMinutes (int Mins)
{
Minutes = Mins;
}
// Accessors
double GetHours() {return Hours;}
int GetMinutes() {return Minutes;}
// Displayer
void Show()
{
cout << "Total Hours this week is: " << Hours <<":"<< Minutes << endl;
}
};
void Menu();
int Add(Time[],int MaxSize);
int main()
{
string Command;
Time Total[10];
while(true){
Menu();
cout << "Enter Command: ";
getline (cin,Command);
cout << endl;
cout << endl;
if (Command=="Quit")
break;
else if (Command=="Add")
Add(Total,8);
else {
cout << "Invalid Command" << endl;
cout << endl;
}
}
}
void Menu()
{
cout << "=====Welcome to Adding Work hours=====" << endl;
cout << "--------Please choose a command-------" << endl;
cout << "1. Add up Hours." << endl;
cout << "2. Save Hours." << endl;
cout << "3. Load Hours." << endl;
cout << "4. Show Loaded Hours." << endl;
cout << "---------------------------------------" << endl;
}
int Add(Time Total[],int MaxSize)
{
double Hrs, Mins;
int T=0,SumMins=0;
double SumHrs=0;
cout << "Enter amount of hours:" << endl;
for (T=1;T<MaxSize;T++){
cout << "Day " << T << " :" << endl;
cout.width(10); cout << "Hours: ";
cin >> Hrs;
cout.width(10); cout << "Minutes: ";
cin >>Mins;
Total[T].SetHours(Hrs);
Total[T].SetMinutes(Mins);
SumHrs=SumHrs+Total[T].GetHours();
SumMins=SumMins+Total[T].GetMinutes();
}
//Find Minutes remainder
int TotalMins=SumMins%60;
//Find Hours From Minutes
int SumHM=SumMins/60;
cout << endl;
cout << "Total Hours: " << SumHrs+SumHM << "." << TotalMins << endl;
return T;
}