如何在C ++中正确使用带有用户输入的参数

时间:2017-05-03 11:55:02

标签: c++ arrays function methods parameters

如何正确传递average()中所需的参数/重载;函数在我的主函数里面? 你可以创建一个带有两个参数的函数,但是如何在主函数中传递参数?

#include <iostream>

using namespace std;



float average(int v[], int n) {
    //n = number of elements
    //v = the vector

    int sum = 0;
    int avg = 0;

    for (int i = 0; i <= n; i++) {
        cout << "Enter a number: ";
        cin >> v[i];
        sum += v[i];

    }
    avg = (double)sum / n;

    cout << avg;

    return 0;

}

int main() {
    average();
}

3 个答案:

答案 0 :(得分:1)

要做的第一件事是更改主要方法以获取参数......

int main(int argc, char** argv)

然后

你可以循环argV

for(auto x = 0; x < argc; x++)
{
  std::cout << "argv[" << x << "] = " << argv[x] << std::endl;
}

记住所有这些都是字符串,arg为0是可执行文件的名称......

现在,您可以在运行时循环并从用户那里获取输入...

如果那是你想要的alg。然后做一些事情:

#include <iostream>
using namespace std;

int main() {
    const auto K = 10;
    int array[K];
    std::cout << "Please give then numbers..." << std::endl;
    int sum = 0;

    for (size_t i = 0; i < K; i++)
    {
        std::cin >> array[i];
    }


    for (int i = 0; i < K; i++) {
        sum = sum + array[i];
    }
    double avg = double(sum) / K;
    std::cout << "avg: " << avg << std::endl;

    return 0;
}

答案 1 :(得分:1)

你可以通过允许用户输入他/她想要的数量来进行更多的交互,然后用特殊的数字或字符结束它们(记住角色的转换)例如0.然后你可以计算总和或平均或...所以代码是这样的:

#include <iostream>
using namespace std;

int main() {

    int current = 0;
    int sum = 0;
    int count = 0;

    while(true)
    {
        cout << "Please enter next number(end by entering 0): ";
        cin << current;
        cout << endl;
        sum = sum + current;
        count++;
    }

    double avg = sum / count;
    cout << "sum: " << sum << endl;
    cout << "avg: " << avg << endl;

    return 0;
}

修改 如果你真的想要使用参数和函数,那就是这样:

#include <iostream>
#include <stdlib.h>     /* atoi */
using namespace std;

double avg(int argc, char** argv){

        int i=1;
        double sum = 0;

        while(i<argc){
                try{
                        sum = sum + atoi(argv[i]);
                }
                catch(int ex){
                        cout << "Failed to calculate average" << endl;
                        exit;
                }
                i++;
        }
        sum = sum / (argc-1); /* note that we have one extra argument because of the name of program. */
        return sum;
}

int main(int argc, char** argv) {

    double result = avg(argc, argv);
    cout << "avg: " << result << endl;

    return 0;
}

答案 2 :(得分:0)

也许你想要这个(根据你的上一次编辑):

#include <iostream>

using namespace std;    

double Average(int a[], int size)
{
  int sum = 0;

  for (int i = 0; i < 10; i++) {
    sum = sum + a[i];
  }

  return (double)sum / size;
}

int main() {
  int a[10] = { 1,2,3,4,5,6,7,8,9,10 };

  cout << Average(a, sizeof(a)/sizeof(a[0])) << endl;
  system("pause");

  return 0;
}

sizeof(a)/sizeof(a[0])是一种获取a数组元素数量的方法。

使用vector模板类的更多类似C ++的解决方案:

#include <iostream>
#include <vector>

using namespace std;    

double Average(vector<int> a)
{
  int sum = 0;

  for (auto & v : a)
    sum = sum + v;

  return (double)sum / a.size();
}

int main() {
  vector<int> a = { 1,2,3,4,5,6,7,8,9,10 };

  cout << "Average = " << Average(a) << endl;
  system("pause");

  return 0;
}

使用交互式用户输入:

int main() {
  vector<int> a;

  for (int i = 0; i < 10; i++)
  {
    cout << "Enter value " << i + 1 << endl;
    int value;
    cin >> value;
    a.push_back(value);
  }

  cout << "Average = " << Average(a) << endl;
  system("pause");

  return 0;
}