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;

	}

Recommended Answers

All 2 Replies

The problem is in your Add function - When you read anything using cin >> , there'll be (at least) a newline character left over from when the user hit 'enter';

using cin.ignore() will discard one character, but if the user typed in any other junk, you might sometimes need to discard more; a better way to do that would be to tell cin.ignore to ignore everything

#include <limits>
std::cin >> x;
std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );

plz tell me the code of sum of this series
1/2+2/3+3/4+4/5......10/11

commented: No. You haven't even posted it in the right place. -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.