0
votes
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

3
Why are you not just using std::string? BTW - What do you think the line cin >> str1 >> sizeof(str1) >> stdin; means? - Ed Heal
Where should I use std:: string please? - A. Atiyah
>> sizeof(str1) what? - user2672107
Use std::string for strings. Use std::vector for arrays. - user2672107

3 Answers

2
votes

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.

1
votes

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++)
0
votes
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.