Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works

ambiguous overload for operator>>

  • Please log in to reply



    New Member

  • Member
  • Pip
  • 1 posts
We have been asked to create a histogram in c++. The user inputs a sequence of numbers between 1 and 9 and the program is supposed to count the frequency of each number in the sequence and display the results as a histogram that looks like this:

** *
** **

my code so far is below:

#include <iostream>
using namespace std;

int main ()

int number[10] = {0}; /* define a 1D array */
int i = 0; /* set i to zero so they can be used in the loop*/
int num;
int biggest = 0;

cout << "Enter a sequence of one or more positive integer numbers terminating";
cout << " with a (-1) to indicate the end of your sequence. ";
cin >> num;
for (i=0; i<10; i++)
cin >> number[num - 1]++;

biggest = number[i];

for (i = 0; i < 10; i++)
for (int y = biggest; y>= 0; y--)
if (number[i] >= y)
cout << "*";
cout << " ";
}while (num > 0); /* -1 is the sentinel */

return 0;

but the following error occurs:

histogram.cpp:21: error: ambiguous overload for ‘operator>>’ in ‘std::cin >> (number[(num + -0x00000000000000001)]++)’
/usr/include/c++/4.4/istream:119: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>& (*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>] <near match>
/usr/include/c++/4.4/istream:123: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>] <near match>
/usr/include/c++/4.4/istream:130: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>] <near match>
/usr/include/c++/4.4/istream:238: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>] <near match>

I am very confused at this error! any help please??
  • 0





  • Member
  • PipPipPip
  • 170 posts
The problem is this line:
cin >> number[num - 1]++;

Think about that line... it really doesn't make any sense the way you've written it. It says something like, "Read a value into increment the (num-1)'th element of number." I can't even make that into a proper-grammar sentence...

The problem is you've attempted to read data from cin and store it into an rvalue, which doesn't make any sense (the >> operator stores data in lvalues - in fact, rvalues cannot store anything, by definition).

Think of it this way. Let's say that in one particular iteration of your loop, the user entered a 5. Furthermore suppose that number[4] (which counts the number of 5's that have occurred so far) currently had a value of 0. In that case, this line would evaluate the following way:
cin >> number[num-1]++
cin >> number[5-1]++
cin >> number[4]++
cin >> 0++
cin >> 1
Now hopefully you can see that it doesn't make sense to data read data from cin and store it in 1, because 1 is not a variable - it's a value.

So what you want to do is this:
cin >> num;

That should fix your compiler error, but I will tell you that there are some logical problems to expect with your program as well:
1. The user must press enter to record each entry. So to get three 1's and two 2's, they wouldn't be able to type "11221<enter>," but instead would have to type "1<enter>1<enter>..." etc
2. Because of what I just said, it's possible for the user to enter numbers outside the range 1-9, besides just 0. This includes negative numbers and numbers greater than or equal to 10. Since you want to process only single-digit entries, this should be impossible. The only invalid numeric entry should be 0, since you for some reason decided to exclude it.
3. If the user includes non-numeric, non-whitespace characters (i.e. alphabetic characters, special characters), your program is going to have some problems dealing with this.

These problems are best addressed by reading characters instead of integers, then converting the character to an integer value. Since chars are already integral types this is very easy: to convert the character '2' to the integer value 2, simply subtract '0', for example:
char a;
cin >> a;
cout << "As a character: " << a << " -- and as an int: " << (int)(a-'0') << endl;

So, I would encourage you to try and fix your own code, rather than use mine, but I wrote a simple program that, I think, does what you want. The only thing is I did not understand how to format the output of your program, so I just made it output the results in text format. You can change that yourself, of course :)
#include <iostream>
using namespace std;

int main ()
	// Create an array to store the number of occurrences of integers 1-9
	int* countArray = new int[9];

	// Make sure all values in the array are initialized to 0
	for (int x=0; x<9; ++x)
		countArray[x] = 0;

	// Get the input and count the number of occurrences of each integer
	cout << "Enter a string of integers in the range [1,9], then press enter:\n";
	char input;
	while (true)
		input = getchar();						// Read a char from STDIN
		if (input == 10)						// If it's a newline, then break out of the loop
		else if (input < '1' || input > '9')	// If it's a non-numeric, or if it's 0, then ignore it
		else									// Otherwise increment the appropriate element of countArray

	// Display the results
	cout << "\n\n";
	for (int x=0; x<9; ++x)
		cout << "No. of Occurrences of " << x+1 << ": " << countArray[x] << endl;

	delete[] countArray;

	return 0;
My version will ignore non-numeric characters, process single digits only, and requires that the user press enter only once, when they are finished with their input.

Cheers, hope this helps :unsure:

Edited by W-Unit, 06 June 2011 - 10:28 AM.

  • 0

Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP