Just a quick question.
I have developed the following code:
#pragma once
#include <iostream>
using namespace std;
class Percent
{
public:
friend bool operator ==(const Percent& first,
const Percent& second);
friend bool operator <(const Percent& first,
const Percent& second);
friend Percent operator +(const Percent& first, const Percent& second);
friend Percent operator -(const Percent& first, const Percent& second);
friend Percent operator *(const Percent& first, const int int_value);
Percent();
Percent(int percent_value);
friend istream& operator >>(istream& ins,
Percent& the_object);
//Overloads the >> operator to the input values of type
//Percent.
//Precondition: If ins is a file stream, then ins
//has already been connected to a file.
friend ostream& operator <<(ostream& outs,
const Percent& a_percent);
//Overloads the << operator for output values of type
//Percent.
//Precondition: If outs if a file stream, then
//outs has already been connected to a file.
private:
int value;
};
And this is the implementation:
// [Ed.: irrelevant code omitted]
istream& operator >>(istream& ins, Percent& the_object)
{
ins >> the_object.value;
return ins;
}
ostream& operator <<(ostream& outs, const Percent& a_percent)
{
outs << a_percent.value << '%';
return outs;
}
The problem is when I try to do any input using the overloaded input stream operator the program does not wait for input after inputting one value - in a sequence of inputs. In other words, this is my main function:
#include "stdafx.h"
#include "Percent.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Please input two percent values and an integer value: ";
Percent first, second;
int int_value;
cin >> first;
cin >> second;
cout << "The first percent added to the second percent is: "
<< first + second << endl;
cout << "The first percent subtracted from the second is: "
<< second - first << endl;
cout << "The first multiplied by the integer value is: "
<< first * int_value << endl;
return 0;
}
It just skips them and the variables get assigned values like they weren't initialized - apart from the first variable, which does get a value.
My question is how do I prevent this behaviour?