简体   繁体   English

如何在C ++中通过用户输入正确使用参数

[英]How do I properly use parameters with user inputs in C++

How do I properly pass the paramaters / overloads needed in the average(); 如何正确地传递average()中所需的参数/重载; function thats inside my main function? 那是我主要功能内的功能吗? As you can se i created a function that takes two parameters but how do I pass the paramaters in the main? 如您所知,我创建了一个带有两个参数的函数,但是如何在主参数中传递参数呢?

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

1st thing to do is to change the main method to take the parameters... 首先要做的是更改主要方法以获取参数...

int main(int argc, char** argv)

then 然后

you can do a loop over argV 你可以在argV上做一个循环

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

remember all those are strings and arg at 0 is the name of the executable... 记住所有这些都是字符串,0处的arg是可执行文件的名称...

now, you can loop and take the input from the user at run time... 现在,您可以循环并在运行时从用户那里获取输入...

if that is your desired alg. 如果那是您想要的算法。 then do something like: 然后做类似的事情:

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

You can do it more interactive by allowing the user to enter as many numbers as he/she wants and then end them with a special number or character(mind the conversion in case of character) for example 0. then you can calculate sum or average or ... so the code goes like this: 您可以通过允许用户输入任意数量的数字,然后以特殊数字或字符结尾(请注意转换为字符)(例如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;
}

Edit: if you realy want to use arguments and function this is the way: 编辑:如果您确实要使用参数和函数,这是这样的方式:

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

Maybe you want this (as per your last edit): 也许您想要这个(根据您的上一次编辑):

#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]) is a way to get the number of elements of the a array. sizeof(a)/sizeof(a[0])是一种方式来获得的元素的数量a数组。

Or a more C++ like solution using the vector template class: 或使用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;
}

And with interactive user input: 并通过交互式用户输入:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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