A. Atiyah
A. Atiyah

Reputation: 23

no operator matches these operands c++

my code:
#include <iostream>
#include <string>
using namespace std;

int main()
{
    char str1[1000000];
    char newString[1000][1000]; 
    int i,j,ctr;
       cout <<" \n\n Split string by space into words :"<< endl;
       cout << "---------------------------------------\n";    

    cout << " Input  a string : ";
    cin >> str1 >> sizeof(str1) >> stdin;   

    j=0; ctr=0;
    for(i=0;i<=(strlen(str1));i++)
    {
        // if space or NULL found, assign NULL into newString[ctr]
        if(str1[i]==' '||str1[i]=='\0')
        {
            newString[ctr][j]='\0';
            ctr++;  //for next word
            j=0;    //for next word, init index to 0
        }
        else
        {
            newString[ctr][j]=str1[i];
            j++;
        }
    }
    cout << "\n Strings or words after split by space are :\n";
    for(i=0;i < ctr;i++)
        cout << newString[i];

    return 0;
}

The error statement:

Error 1 error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'unsigned int' (or there is no acceptable conversion) c:\users\ayah atiyeh\documents\visual studio 2012\projects\consoleapplication1\consoleapplication1\source.cpp 14 3 IntelliSense: no operator ">>" matches these operands operand types are: std::basic_istream> >> unsigned int c:\Users\Ayah Atiyeh\Documents\Visual Studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\Source.cpp 14

Upvotes: 0

Views: 3840

Answers (3)

iBug
iBug

Reputation: 37227

cin >> str1 >> sizeof(str1) >> stdin;

In the second part of this input statement, you're trying to read into an rvalue (sizeof(str1) is a compile time constant and is an rvalue). You shouldn't do that. Also reading something to stdin is a dangerous operation as its type is FILE*, which may negatively affect further input operations.

Upvotes: 0

Ed Heal
Ed Heal

Reputation: 59997

Change

char str1[1000000];

to

std::string str1;

And

cin >> str1 >> sizeof(str1) >> stdin; 

to

std::cin >> str1;

BTW - Remove the line using namespace std;

Then the line

 for(i=0;i<=(strlen(str1));i++)

should be

  for(i=0;i<str1.length();i++)

Upvotes: 1

Sahil Soni
Sahil Soni

Reputation: 41

You need to do this:

cout << " Input  a string : ";
cin >> str1;

instead of :

cout << " Input  a string : ";
cin >> str1 >> sizeof(str1) >> stdin;   

The thing is, >> operator is used to direct an input to the variable on the right of it. And you are not giving a variable on the right of second >>, sizeof(str1) is a function and it returns a number. and when the compiler sees a number in place of a variable, it gives you that error.

Upvotes: 2

Related Questions