I'm trying to input a number, and based on that number, the user would have to enter x amount of times.
For example
3 //how many the user wants
192 231 2 3
22192 2 1 23
2831 3 23 1
I tried doing this, but it keeps saying no matching function for getline
int* x = NULL;
int numbers;
cin >> numbers;
x = new int[numbers]
for (int i=0;i<numbers;i++)
{
std::getline(std::cin, numbers)
x[i] = numbers
}
getline的第二个参数的类型为std::string
You definitely do not want to use std::getline
as it doesn't look like you want a string of numbers, but rather numbers themselves.
What you want is to read number by number, so use the same thing you did to read in the numbers
, but do not read it in numbers
again. (Because you are using it in the loop.)
Anyway, an approximation of what you want is this:
int how_many;
std::vector<int> numbers;
std::cin >> how_many;
for (int i = 0; i < how_many; i++){
int temp;
std::cin >> temp;
numbers.push_back(temp);
}
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.