0
// //**********************************************************************************
//File Name: averages.cc
//Description: Calculate the smallest, largest, and the average of the input values.
//Date: 10-6-09
//**********************************************************************************

#include <iostream>
#include <string>

using namespace std;

int main() {

string line;
int min=2000000;
int max=0;
double avg;
int next;
double sum = 0;
double count = 0;

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(7);

cin>> next;

	while (!cin.eof()){
	getline(cin,line)=next;
		if (!cin.fail()) {
			if (next < min)
			min = next;
	
			if (next > max)
			max = next;
			
			count++;
			sum=sum+next;
 }
}

	avg=sum/count;
cout << min;
cout << max;
cout << avg;
 
}

i get the error:
averages.cc:30: error: no match for 'operator=' in 'std::getline [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((std: :basic_istream<char, std::char_traits<char> >&)(&std::cin)), ((std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)(&line))) = next'/usr/sfw/lib/gcc/i386-pc-solaris2.10/3.4.3/../../../../include/c++/3.4.3/iosfwd:62: note: candidates are: std::basic_istream<char, std::char_traits<char> >&std::basic_istream<char, std::char_traits<char> >::operator=(const std::basic_istream<char, std::char_traits<char> >&)bash-3.00$

plz i need help!

Edited by DC257209: n/a

4
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by firstPerson
Featured Replies
  • Line 29 won't work. getline reads a line, so you should have this (no = sign): [code] getline (cin, line); [/code] If you then want to convert line into an integer, use atoi or strtol. These work on C-style strings, not C++ style strings, so convert line using c_str (); … Read More

1

Line 29 won't work. getline reads a line, so you should have this (no = sign):

getline (cin, line);

If you then want to convert line into an integer, use atoi or strtol. These work on C-style strings, not C++ style strings, so convert line using c_str ();

getline (cin, line);
next = atoi (line.c_str ());
0

If you then want to convert line into an integer, use atoi or strtol. These work on C-style strings, not C++ style strings, so convert line using c_str ();

Help with Code Tags
C++ Syntax (Toggle Plain Text)
getline (cin, line);
next = atoi (line.c_str ());

Disappointed to see you suggest a non-standard function.

In C++ you should use sstream for conversion from string to numbers.

#include<sstream>
//more stuff if needed
int main()
{
     stringstream ss;
     string strNum = "12345.56";
     float num= 0.0f;
     ss << strNum;   //input the string into the stream
     ss >> num;  //output the result into num = 12345.56f;
    // maybe error checking if needed.
   return 0;
}

Edited by firstPerson: n/a

0

Disappointed to see you suggest a non-standard function.

In C++ you should use sstream for conversion from string to numbers.

Or just read an integer from the cin stream in the first place.

0

Or just read an integer from the cin stream in the first place.

Then that would mean more probable problems.

0

Then that would mean more probable problems.

Not so. Extracting an integer from an strstream works exactly the same as extracting from cin, an ifstream, or any other stream.

0

Disappointed to see you suggest a non-standard function.

atoi is standard. itoa isn't. Someone told me strtol is better than atoi, but I can't remember the reasons given and I don't know if atoi is in fact to be avoided. Generally I use strtol. Never had a problem with either, which doesn't mean there aren't drawbacks. But atoi is standard.

0

atoi is standard. itoa isn't. Someone told me strtol is better than atoi, but I can't remember the reasons given and I don't know if atoi is in fact to be avoided. Generally I use strtol. Never had a problem with either, which doesn't mean there aren't drawbacks. But atoi is standard.

Oh, your right. I was thinking itoa for some reason. Anyways, here
is the reason to use strtol, Link. Its because of error checking.

Edited by firstPerson: n/a

This article has been dead for over six months. Start a new discussion instead.
Be sure to adhere to our posting rules.