只是一个简单的问题。没有等待在C键盘输入(重载输入流运算符)++
我已经开发了下面的代码:
#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;
};
而这正是实现:
// [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;
}
问题是,当我尝试使用重载的输入流运营商的程序没有做任何输入输入一个值后不等待输入 - 在一系列输入中。换句话说,这是我的主要功能:
#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;
}
它只是跳过他们,像他们未初始化变量会被分配值 - 除了第一个变量,它确实得到一个值。
我的问题是我要如何防止这种行为?
请使用“代码saple”格式来编辑您的文章 – Ando 2010-03-03 16:14:44
请格式正确 – Andrey 2010-03-03 16:14:53
这次您格式化得太多:) – Ando 2010-03-03 16:18:29