Personal computing discussed

Moderators: renee, SecretSquirrel, just brew it!

 
Crayon Shin Chan
Minister of Gerbil Affairs
Topic Author
Posts: 2313
Joined: Fri Sep 06, 2002 11:14 am
Location: Malaysia
Contact:

no match for operator >> in C++?

Sun Jan 04, 2009 2:23 pm

Hey guys, I'm kinda new to C++, I've been programming in C for some time though, although I'm not really that great at it. I'm using GCC 3.4.2 to compile the following program:
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
    cout << "initializing arrays" << endl ;
    int array1 [60], array2 [60];
//    zeroall(array1, array2);
    cout << "Operand 1:" << endl;
    cin >> array1;
    cout << "Operand 2:" << endl;
    cin >> array2;
    cout << "Operands 1 and 2:" << endl << array1 << endl << array2 << endl;
    return 0;
}

But for some reason, it fails to compile with this message:
9 C:\Users\Shinichi Kudo\Documents\highprec\highprec.cpp no match for 'operator>>' in 'std::cin >> array1' 

I don't understand what I need to do here. I'm pretty sure I referred to the array as an operand in the correct manner (without the square brackets after the name). Any help? This should be a simple problem.
Mothership: FX-8350, 12GB DDR3, M5A99X EVO, MSI GTX 1070 Sea Hawk, Crucial MX500 500GB
Supply ship: [email protected], 12GB DDR3, M4A88TD-V EVO/USB3
Corsair: Thinkpad X230
 
Madman
Minister of Gerbil Affairs
Posts: 2317
Joined: Tue Apr 01, 2003 4:55 am
Location: Latvia

Re: no match for operator >> in C++?

Sun Jan 04, 2009 2:50 pm

cin>>array[insertintegerindexhere];

Or if you are reading a whole array as binary data, try looking for cin.readbuf/read/getline, I don't remember the exact function.
Core 2 Duo E6300, MSI P45 NEO-F, Club 3D GTX 260, 4Gb DDR2-800Mhz, Audigy X-Fi Fatal1ty Champ1on ed., 0.5Tb+1Tb Seagate Barracuda 7200.12, 630W AXP, Samsung SyncMaster BX2450, ViewSonic VP171b
 
Pizzapotamus
Gerbil
Posts: 50
Joined: Tue Aug 28, 2007 11:18 am

Re: no match for operator >> in C++?

Sun Jan 04, 2009 3:00 pm

It's the type on the right side of >> that is causing your problem, if you used cin>> someVariableOfTypeInt things would work but there isn't anything defined for cin>> intArray. To input an array probably you'd want to read values inside of a loop and break on a blank line.
 
Crayon Shin Chan
Minister of Gerbil Affairs
Topic Author
Posts: 2313
Joined: Fri Sep 06, 2002 11:14 am
Location: Malaysia
Contact:

Re: no match for operator >> in C++?

Sun Jan 04, 2009 3:23 pm

That's really screwed up if I have to write a loop just to input some numbers in. Isn't there something like cin.getline(), but for integers? I want each integer (read: single arabic numerical symbol) to take up exactly one space in the array. Kinda like how one alphabet takes up one space in a string.

EDIT: Well, I suppose I could use a char array then. After all, the range is only 0-9.
Last edited by Crayon Shin Chan on Sun Jan 04, 2009 3:31 pm, edited 1 time in total.
Mothership: FX-8350, 12GB DDR3, M5A99X EVO, MSI GTX 1070 Sea Hawk, Crucial MX500 500GB
Supply ship: [email protected], 12GB DDR3, M4A88TD-V EVO/USB3
Corsair: Thinkpad X230
 
Madman
Minister of Gerbil Affairs
Posts: 2317
Joined: Tue Apr 01, 2003 4:55 am
Location: Latvia

Re: no match for operator >> in C++?

Sun Jan 04, 2009 3:30 pm

Nope, we don't, but it's a good thing because storing numbers in ASCII really sucks performance wise, there are also problems with white space terminators (\r\n windows, \r mac (might be mistaken here), \n nix) and so on.

But the C++ allows you to do different weird stuff to read integers, if you don't care for performance there are some nice approaches, I think something like this should work without overflows and memory corruptions. I'm writing this code from memory, might be wrong.

vector<int> boo;

while(true)
{
     int n;
     cin>>n;
     if(!cin.good())
          break;
     boo.push_back(n);
}
Core 2 Duo E6300, MSI P45 NEO-F, Club 3D GTX 260, 4Gb DDR2-800Mhz, Audigy X-Fi Fatal1ty Champ1on ed., 0.5Tb+1Tb Seagate Barracuda 7200.12, 630W AXP, Samsung SyncMaster BX2450, ViewSonic VP171b
 
Crayon Shin Chan
Minister of Gerbil Affairs
Topic Author
Posts: 2313
Joined: Fri Sep 06, 2002 11:14 am
Location: Malaysia
Contact:

Re: no match for operator >> in C++?

Sun Jan 04, 2009 3:40 pm

Hey, I'm not supposed to know about vectors!

Wait, so there's no cin.getline() twin that works for integers, right? And I shouldn't use chars because they suck performance wise.
Mothership: FX-8350, 12GB DDR3, M5A99X EVO, MSI GTX 1070 Sea Hawk, Crucial MX500 500GB
Supply ship: [email protected], 12GB DDR3, M4A88TD-V EVO/USB3
Corsair: Thinkpad X230
 
Madman
Minister of Gerbil Affairs
Posts: 2317
Joined: Tue Apr 01, 2003 4:55 am
Location: Latvia

Re: no match for operator >> in C++?

Sun Jan 04, 2009 3:42 pm

Crayon Shin Chan wrote:
EDIT: Well, I suppose I could use a char array then. After all, the range is only 0-9.


Since the 32 bit CPU operates on DWORDs anyway, there is no much point in that. Printing char to console will most likely cause it to be printed as character somewhere in control range, or you'll have to do a cast.

For small arrays where memory is not an issue int's will do just fine.
Core 2 Duo E6300, MSI P45 NEO-F, Club 3D GTX 260, 4Gb DDR2-800Mhz, Audigy X-Fi Fatal1ty Champ1on ed., 0.5Tb+1Tb Seagate Barracuda 7200.12, 630W AXP, Samsung SyncMaster BX2450, ViewSonic VP171b
 
Madman
Minister of Gerbil Affairs
Posts: 2317
Joined: Tue Apr 01, 2003 4:55 am
Location: Latvia

Re: no match for operator >> in C++?

Sun Jan 04, 2009 3:45 pm

Crayon Shin Chan wrote:
Hey, I'm not supposed to know about vectors!

Wait, so there's no cin.getline() twin that works for integers, right? And I shouldn't use chars because they suck performance wise.

Oh, I didn't paid enough attention, cin is console input, so binary data is out of question. And getline won't help you there as well.

Do you have to read fixed number of numbers, or can it be dynamic?
Core 2 Duo E6300, MSI P45 NEO-F, Club 3D GTX 260, 4Gb DDR2-800Mhz, Audigy X-Fi Fatal1ty Champ1on ed., 0.5Tb+1Tb Seagate Barracuda 7200.12, 630W AXP, Samsung SyncMaster BX2450, ViewSonic VP171b
 
Crayon Shin Chan
Minister of Gerbil Affairs
Topic Author
Posts: 2313
Joined: Fri Sep 06, 2002 11:14 am
Location: Malaysia
Contact:

Re: no match for operator >> in C++?

Sun Jan 04, 2009 3:51 pm

Well, the user will have to input a number... which can at maximum have 60 digits. with a carriage return at the end to signify the end of the number. The idea is then to perform basic arithmetic operations on this number... and since I really don't think even a 64bit computer can handle numbers this large, I'm thinking of implementing the good old vertical addition/subtraction routine you learn in primary school.

The problem is, I would need to pad the number now, so if the numbers have different amounts of digits, I will have to make sure in both numbers, the least significant digit on the right before I add/subtract.
Mothership: FX-8350, 12GB DDR3, M5A99X EVO, MSI GTX 1070 Sea Hawk, Crucial MX500 500GB
Supply ship: [email protected], 12GB DDR3, M4A88TD-V EVO/USB3
Corsair: Thinkpad X230
 
Madman
Minister of Gerbil Affairs
Posts: 2317
Joined: Tue Apr 01, 2003 4:55 am
Location: Latvia

Re: no match for operator >> in C++?

Sun Jan 04, 2009 4:22 pm

Just read it to strings then, then you can start from the end and add those numbers together.

To get the integer value from char, subtract '0' from it, then you can do arithmetic and check for carry conditions.

x86 assembler have nice ASCII adjust after addition opcodes... 8)
Core 2 Duo E6300, MSI P45 NEO-F, Club 3D GTX 260, 4Gb DDR2-800Mhz, Audigy X-Fi Fatal1ty Champ1on ed., 0.5Tb+1Tb Seagate Barracuda 7200.12, 630W AXP, Samsung SyncMaster BX2450, ViewSonic VP171b
 
Crayon Shin Chan
Minister of Gerbil Affairs
Topic Author
Posts: 2313
Joined: Fri Sep 06, 2002 11:14 am
Location: Malaysia
Contact:

Re: no match for operator >> in C++?

Sun Jan 04, 2009 4:32 pm

Sorry, could you please rephrase that? How does reading to strings make lining up those least significant digits any easier than with an array of numbers?

And what do you mean by subtracting zero from the ASCII number of a char? I'm not inputting any characters, just numbers.
Mothership: FX-8350, 12GB DDR3, M5A99X EVO, MSI GTX 1070 Sea Hawk, Crucial MX500 500GB
Supply ship: [email protected], 12GB DDR3, M4A88TD-V EVO/USB3
Corsair: Thinkpad X230
 
Madman
Minister of Gerbil Affairs
Posts: 2317
Joined: Tue Apr 01, 2003 4:55 am
Location: Latvia

Re: no match for operator >> in C++?

Sun Jan 04, 2009 4:55 pm

If you read characters then int value of '0' is 48, '1' is 49 and so on. When you subtract the '0' you get the real integer value. You can operate on it and then add '0' to get back char representation of that number. Unless the number is two digit number, in which case you get mess.

Concerning how it is easier, it depends. All approaches have their problems, I would most likely go with strings.

Strings have length() function, and you can easily start adding digits from number[number.length()-i]. Where i keeps incrementing. Just remember that i cannot be > than number.length().
Core 2 Duo E6300, MSI P45 NEO-F, Club 3D GTX 260, 4Gb DDR2-800Mhz, Audigy X-Fi Fatal1ty Champ1on ed., 0.5Tb+1Tb Seagate Barracuda 7200.12, 630W AXP, Samsung SyncMaster BX2450, ViewSonic VP171b
 
FubbHead
Grand Gerbil Poohbah
Posts: 3482
Joined: Mon Apr 08, 2002 9:04 pm
Location: Borås, Sweden

Re: no match for operator >> in C++?

Sun Jan 04, 2009 5:15 pm

Putting a character (and only a single character) in single quotes you get the value of the character in the ASCII table, ie. if you look at the ASCII table, you see the zero character is represented by the value 48. And since all numbers comes after one another in the ASCII table, it's an easy way to convert the character to integer.

With number this large, perhaps it's safer (and easier?) to let every digit be represented by a single integer in an array, and do the calculations a bit like you'd do on squared paper. It should be fairly easy to convert a string to an integer array. Or you just work on the characters in the string directly, like Madman suggests.
Amiga 1200, 68020@28MHz, 4MB+2MB RAM, Conner 80MB harddrive
 
Flying Fox
Gerbil God
Posts: 25690
Joined: Mon May 24, 2004 2:19 am
Contact:

Re: no match for operator >> in C++?

Sun Jan 04, 2009 10:42 pm

Ok, based on your use of the word "operand", I suppose you are doing some kind of calculator program? You can just use 2 integer variables and be done with it. The iostream library contains the proper overloads for operator>>() with integers.
  int operand1, operand2;
  cout << "Operand 1: ";
  cin >> operand1;
  cout << "Operand 2: ";
  cin >> operand2;
  cout << "Operands 1 and 2:" << endl << operand1 << endl << operand2 << endl;
The Model M is not for the faint of heart. You either like them or hate them.

Gerbils unite! Fold for UnitedGerbilNation, team 2630.

Who is online

Users browsing this forum: No registered users and 1 guest
GZIP: On