Matthew Hoggan
Matthew Hoggan

Reputation: 7604

C++: Need More Help on Streams

I have been reading about streams, but when I try and deviate from the books to accomplish a task I seem to be doing something wrong. Anyways here is the start of my code. If this code can work with slight modifications please let me know, else if you could provide a better "more C++ like" route I would greatly appreciate it.

#include <map>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <iterator>

using namespace std;

std::multimap<std::string,float> _map;

istream& operator>>(istream& stream, pair<string,float> in ) { 
    return stream >> in.first >> in.second;
}

int main( int argc, char *argv[ ] ) { 
    istream is( );
    do {
        pair<string,float> input;
        is >> input;
        _map.insert(input);
    } while( is );
}

Compiler errors are:

[mehoggan@desktop bjarne_stroustrup]$ g++ -o map -Wall ./key_value_stats.cpp
./key_value_stats.cpp: In function ‘int main(int, char**)’:
./key_value_stats.cpp:20:15: error: no match for ‘operator>>’ in ‘is >> input’
./key_value_stats.cpp:12:10: note: candidate is: std::istream& operator>>(std::istream&, std::pair<std::basic_string<char>, float>)
./key_value_stats.cpp:22:14: warning: the address of ‘std::istream is()’ will always evaluate as ‘true’

UPDATE[0]:

After removing the ( ) and changing:

istream& operator>>(istream& stream, pair<string,float> in ) {

to

istream& operator>>(istream& stream, pair<string,float> &in ) {

I get a different set of compiler errors:

/usr/lib/gcc/i686-redhat-linux/4.5.1/../../../../include/c++/4.5.1/istream: In function ‘int main(int, char**)’:
/usr/lib/gcc/i686-redhat-linux/4.5.1/../../../../include/c++/4.5.1/istream:582:7: error: ‘std::basic_istream<_CharT, _Traits>::basic_istream() [with _CharT = char, _Traits = std::char_traits<char>]’ is protected
./key_value_stats.cpp:17:13: error: within this context

with the following code:

#include <map>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <iterator>

using namespace std;

std::multimap<std::string,float> _map;

istream& operator>>(istream& stream, pair<string,float> &in ) {
    return stream >> in.first >> in.second;
}

int main( int argc, char *argv[ ] ) { 
    istream is;
    do {
        pair<string,float> input;
        is >> input;
        _map.insert(input);
    } while( is );
}

UPDATE[1]

Okay I solved the issue I think. Going back to Stroustrup's desk_calculator example, I have the following code which at least compiles, I will add the final features into it, and then re-post the final product for anyone interested.

#include <map>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <iterator>

using namespace std;

std::multimap<std::string,float> _map;

istream& operator>>(istream& stream, pair<string,float> &in ) {
    return stream >> in.first >> in.second;
}

//void print_pair( pair<string,float>

int main( int argc, char *argv[ ] ) { 
    istream *is = &cin;
    do {
        pair<string,float> input;
        (*is) >> input;
        _map.insert(input);
    } while( is );
}

UPDATE[2]

I don't think I met the requirements of the problem but I got the technical stuff to work.

#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <iterator>

/********************************************************************
 * Read a sequence of possibly whitespace-separated (name,value)
 * pairs, where the name is a single whitespaace-separated word and
 * the value is an integer or floating-point value. Compute and print
 * the sum and mean for each name and the sum and mean for all names
 * ******************************************************************/

using namespace std;

std::multimap<string,float> _map;

istream& operator>>(istream& stream, pair<string,float> &in ) {
    return stream >> in.first >> in.second;
}

ostream& operator<<(ostream& stream, pair<string,float> &out ) {
    return stream << "(" << out.first 
                  << ", " << out.second << ")" << endl;
}

int main( int argc, char *argv[ ] ) { 
    istream *is = &cin;
    do {
        pair<string,float> input;
        (*is) >> input;
        _map.insert(input);
    } while( is->peek( ) != EOF );

    ostream *os = &cout;
    multimap<string,float>::iterator mit = _map.begin( );
    float sum = 0.0;
    while( mit != _map.end( ) ) {
        pair<string,float> p_pair = (*mit);
        (*os) << p_pair;
        sum+=p_pair.second;
        mit++;
    }
    float mean = static_cast<float>( sum/_map.size( ) );
    (*os) << "Sum: " << sum << " Mean: " << mean << endl;
}

Upvotes: 0

Views: 579

Answers (2)

J-16 SDiZ
J-16 SDiZ

Reputation: 26930

use

#include <map>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <iterator>

using namespace std;

std::multimap<std::string,float> _map;

istream& operator>>(istream& stream, pair<string,float>& in ) { 
    return stream >> in.first >> in.second;
}

int main( int argc, char *argv[ ] ) { 
    do {
        pair<string,float> input;
         cin >> input;
        _map.insert(input);
    } while( cin );
}

Upvotes: 2

rodrigo
rodrigo

Reputation: 98526

Remove the () from the line:

istream is();

Should be:

istream is;

Or:

istream is("text 1.2");

Or else is is a function that returns an istream.

Upvotes: 0

Related Questions