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

时间:2015-03-27 13:09:46

标签: c++ input numbers

好的,所以,在我的last question我的程序中找到了一组数字的标准偏差。今天,我的导师告诉我,它需要从用户那里获得多个号码。我不知道如何做到这一点。关于如何做到这一点的任何建议?代码被接受。拜托,谢谢。

#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;
}

3 个答案:

答案 0 :(得分:2)

快速谷歌搜索可以完成这项工作...... 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];
}

答案 1 :(得分:1)

你去吧:

    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];
    }

答案 2 :(得分:0)

这是矢量的一个例子。它需要用户输入,直到用户输入-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;
}