我如何让程序接收用户输入?

How would I make the program receive user input?

好的,所以,在我的 上,我的程序正在寻找一组数字的标准差。今天,我的导师告诉我,它需要从用户那里获取多个号码。我不知道该怎么做。关于如何做到这一点的任何建议?代码被接受。请和谢谢。

#include "stdafx.h" //No Flipping Clue
#include <iostream> //Needed For "cout"
#include <iomanip> //Needed To Round Decimal Points
#include <math.h> //Needed For "sqrt()" And "pow()"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
//Used To Round The Decimal Points 2 Places
cout << setiosflags(ios::fixed|ios::showpoint);
cout << setprecision(1);

//Declaring
double Numbers[] = {65, 49, 74, 59, 48}; //Work On Making This A User Input
double Mean = 0, Items = 0, Sum = 0, Deviation = 0;
int Counter;

//Finds The Mean Of The Set Of Numbers
for (Counter = 0; Counter < sizeof(Numbers) / sizeof(double); Counter++)
{
    for (Counter = 0; Counter < sizeof(Numbers) / sizeof(double); Counter++)
    {
        Sum += Numbers[Counter]; //Adds All Numbers In Array Together
    }
    Items = sizeof(Numbers) / sizeof(double); //Gets The Number Of Items In The Array
    Mean = Sum / Items; //Finds The Mean
}

//Finds The Standard Deviation
for (Counter = 0; Counter < sizeof(Numbers) / sizeof(double); Counter++)
{
    Deviation += pow((Numbers[Counter] - Mean), 2) / Items; //Does Math Things...
}
Deviation = sqrt(Deviation); //Square Roots The Final Product
cout << "Deviation = " << Deviation << endl; //Print Out The Standard Deviation

system("pause");
return 0;
}

快速 google 搜索就可以完成任务... C++ Input/Ouput

int number;
cin >> number;

示例:

int nbNumbers;
cout << "How many numbers do you need :" << endl;
cin >> nbNumbers;
double numbers[nbNumbers];

for(int i = 0; i < nbNumbers; ++i)
{
   cout << "Enter a Number :" << endl;
   cin >> numbers[i];
}

好了:

    cout<<"how many numbers you want to enter?";
    cin>>n;
    double Numbers[n];
    cout<<"enter numbers";
    for(int i=0;i<n;i++){
    cin>>Numbers[i];
    }

这里是向量的例子。它接受用户的输入,直到用户输入 -1。

#include <stdio.h>
#include <array>

int main()
{
    int number;
    vector<int> userInput;

     do
     {
        cout << "Enter a number (-1 to quit): ";
        cin >> number;

        userInput.push_back(number);

     } while (number != -1);

    for (vector < int >::iterator it = userInput.begin(); it < userInput.end() - 1; it++)
    {
        cout << endl << *it;
    }

    system("pause");
    return 0;
}