views:

179

answers:

3

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?

A: 

Everything worked properly for me (just copy-pasted your code) except that last cout << threw error because int_value was not initialized

Andrey
Ahh strange - may be it's something to do with the compiler.
hairyyak
Unless you are using the same compiler
hairyyak
i was using Visual Studio 2008
Andrey
Nah if you put in a cin for the int value it just skips all the values and they do not get initialized.
hairyyak
+2  A: 

A common issue with input stream, especially in visual c++, is that you need .ignore(). This will prevent the input from getting ignored. If I remember correctly, just do cin.ignore() right before (or is it after) the first cin. I think that should do it.

jghost
A: 

I think it was me because I was inputting a percent, when I should have been inputting an integer, which accounts for why it worked properly on Andrey's compiler - also I was not reading in the percent sign, which I think was not helping.

The input and output should all be succeded by a percent sign.

hairyyak