简体   繁体   中英

Invalid operator type of std::istream?

So I'm trying to create a C++ program that reads in a list of numbers(where the user enters a list of 5 numbers separated by spaces) and prints out the reversed list. so far, this is what I have:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#pragma warning(disable: 4996)

using namespace std;

int* get_Number() { 
int* p = new int[32];
cout << "Please enter a list of 5 numbers, separated by spaces" << endl;
cin >> p;
return p;
};


int* reverseArray(int* numArray)
{

}

My problem is that I keep on getting this error:

Error: no operator ">>" matches these operands. Operand types are: std::istream >> int * 

at the cin >> p line.

What am I doing wrong? I'm new to C++, and any help would be greatly appreciated, thank you!!

How about this?

#include <iostream>

int main(int argc, char* argv[])
{
    int nums[5];

    std::cout << "Please enter a list of 5 numbers, separated by spaces" << std::endl;
    for (int i = 0; i < 5; ++i)
        std::cin >> nums[i];

    for (int i = 0; i < 5; ++i)
        std::cout << nums[i];
    return 0;
}

Did you mean cin >> p[i] , where i is an index missing in your code?

Currently you're reading to a pointer, but you intended to read into your array, right?

Try this

int* get_Number() { 
    int* p = new int[32];
    for (int i = 0; i < 5; i++)
    {
       cout << "Please enter a number" << endl;
       cin >> p[i];
    }
    return p;
};

You're better off using getline

string line;
cin.getline(line);

It will do nice stuff for you like resizing it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM