Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 通过函数传递数组_C++_Arrays - Fatal编程技术网

C++ 通过函数传递数组

C++ 通过函数传递数组,c++,arrays,C++,Arrays,我正在为一个简单的计算器编写一个代码,它将执行几个十进制转换(目前我关心的是1)。基本上,在用户在菜单(switch语句)中进行选择之后,我希望有一个单独的函数来执行实际转换(数学)和一个为解决方案保留的函数(输出)。我需要帮助将在mathoption1中创建的二进制数组传递到outputoption1。代码现在没有编译,我有4个错误与一些函数的参数相关。显然,我迷路了,需要一些指导 #include <iostream> using namespace std; void di

我正在为一个简单的计算器编写一个代码,它将执行几个十进制转换(目前我关心的是1)。基本上,在用户在菜单(switch语句)中进行选择之后,我希望有一个单独的函数来执行实际转换(数学)和一个为解决方案保留的函数(输出)。我需要帮助将在mathoption1中创建的二进制数组传递到outputoption1。代码现在没有编译,我有4个错误与一些函数的参数相关。显然,我迷路了,需要一些指导

#include <iostream>

using namespace std;

void display();
void menu(int &option, int decimal, int binaryarray[]);
void outputoption1(int decimal, int binaryarray[]);
void mathoption1(int &decimal);


int main()
{
    int option;
    int decimal;
    int binaryarray[32];

    display();
    menu(option, decimal, binaryarray);

    return 0;
}

void display()
{
    cout << "Industrial Engineering Decimal Conversion v 1.0\n" << endl;
    cout << "Created by: asdf adsfadf\n"
         << "\t    adsfa adsfad\n" << endl;
    cout << "On the next screen, you will choose which operation you want to perform\n" << endl;

    system("PAUSE");
    cout << "\n" << endl;
}

void menu(int &option, int decimal, int binaryarray[])
{
   cout << "Welcome to the IE Decimal Conversion Program!\n\n"
        << "To choose a conversion, enter a number from the menu below\n\n"
        << "1) Decimal to Binary\n"
        << "2) Quit the program\n" << endl;

   cin >> option;

   switch (option)
   {
   case 1:
       mathoption1(decimal);
       outputoption1(decimal, binaryarray[]);
       break;
   case 2:
       break;
   default:
       cout << "ERROR: Please make a valid selection" << endl;
       menu(option, decimal, binaryarray);
   }
}

void mathoption1(int &decimal)
{
    cout << "Please input the decimal you want to convert to binary/n/n";
    cin >> decimal;

    int x = 0;
    int binaryarray[32];
    while (decimal != 0)
    {
        binaryarray[x] = decimal % 2;
        x++;
        decimal = decimal / 2;
    }

}

void outputoption1(int decimal, int binaryarray[])
{
    int x = 0;
    cout << "Your original decimal value of " << decimal << " is equivalent to the following binary value:\n";
    for (int y = x - 1; y >= 0; y--)
    {
        cout << binaryarray[y];
    }
}
#包括
使用名称空间std;
void display();
无效菜单(int&选项、int十进制、int二进制数组[]);
void outputoption1(整数十进制,整数二进制数组[]);
void mathoption1(整数和小数);
int main()
{
int选项;
整数十进制;
int二进制数组[32];
显示();
菜单(选项、十进制、二进制数组);
返回0;
}
无效显示()
{

cout函数菜单的原型与实际函数标题不同。您应该更新标题,使其与原型相同。您还必须在函数mathoption1标题的小数点之前添加&

        outputoption1(decimal, binaryarray[]);
删除[]:仅传入变量

菜单不应该有参数,因为这些东西都不是main需要告诉菜单(甚至不知道)的。它们可以是菜单的本地内容

让菜单调用自己是很不传统的。最好使用循环

您没有得到任何输出,因为您在
outputoption1
中的x是0,但您需要在
mathoption1
中生成的x。另外,因为mathoption1有两个版本的binaryArray:可以共享的参数和不能共享的局部变量

因此,本质上你有参数问题。问问你自己每个函数:这个函数需要知道什么才能完成它的工作?并把它放在()之间。它向调用程序提供了什么值(如果有的话)?并把它作为返回类型,或放在()之间

mathoption1作为convertToBinary更好。转换为二进制的函数需要知道什么?十进制数。它提供调用函数什么?二进制数组和二进制位数。(请注意,这不会要求用户输入十进制数,而是从参数列表中获取。这比您现在拥有的更好,原因有两个:它更通用(如果您有一个非交互式程序,它会工作),而且函数“convertToBinary”比函数“askUserForANumberAndConvertToBinary”更连贯

如果输出选项1是“printBinary”,而不是“printOriginalDecimalValueAndBinary”,则输出选项1也会更简单。因此:如果是printBinary,它需要知道什么?告诉它


因此,我认为您应该考虑组织和参数。保持连贯性,然后回到编译问题上来。

更新:我找到了一种方法来完成我需要做的事情,而不必通过函数传递数组。

主要问题是您没有将
二进制数组
传递到
mathoption1,但我还想解决很多其他问题

使用
bool
s表示二进制
  • 数组中的每个元素不需要完整的
    int
    。二进制是一个布尔值,因此您可以使用
    bool
    (类似布尔代数)。考虑它更自然
数组的硬编码大小是不允许的
  • 你不应该硬编码数组的大小。通常你会在C++11中使用宏(
    #define size
    )或
    constexpr
使用函数描述程序的流程
  • 您应该尝试为读者有意义的函数命名。
    mathoption1
    ,虽然可读,但不能告诉我它在做什么。
    convert\u to\u binary
    或类似的东西在保持可读性的同时更容易理解
避免使用命名空间std;
  • 这是坏习惯,不要这样做
其他注释
  • 请注意,此转换算法只转换正数。您需要处理负数(一和二的恭维)
  • 请注意,此转换仅适用于高达4294967295的用户输入。因为您的数组大小为32,您只能将地址设置为232-1。如果有人输入4294967296,您会遇到麻烦。我将让您了解如何处理该问题。您可能需要进行更多的错误检查-这很好。我在下面的代码中没有做多少工作来了解我把它作为一种锻炼给你
您将在下面找到新代码

#include <iostream>

#define SIZE 32

char display_menu() {
  char choice;
  std::cout << "1. Convert decimal to binary." << std::endl;
  std::cout << "2. Choice 2." << std::endl;
  std::cout << "Q. Quit." << std::endl;
  std::cin >> choice;
  return choice;
}

bool is_not_valid(char choice) {
  choice = toupper(choice);
  return choice != '1' && choice != '2' && choice != 'Q';
}

int get_decimal_from_user() {
  int num;
  std::cout << "Please enter a decimal to be converted to binary: " << std::endl;
  std::cin >> num;
  return num;
}

void compute_binary(int num, bool* binary) {
  for(size_t i = 0; i < SIZE; ++i) {
    binary[SIZE-i-1] = num%2;
    num/=2;
  }
}

void output_binary(int num, bool* binary) {
  std::cout << num << " can be represented in binary as ";
  for(size_t i = 0; i < SIZE; ++i) {
    std::cout << binary[i];
  }
  std::cout << std::endl;
}

void convert() {
  int num = get_decimal_from_user();    
  bool binary[SIZE];
  compute_binary(num, binary);
  output_binary(num, binary);
}

void choice_2() {
  std::cout << "Choice 2" << std::endl;
}

void do_choice(char choice) {
  switch(toupper(choice)) {
    case '1': convert(); break;
    case '2': choice_2(); break;
    case 'Q': return;
    default: break;
  }
} 

int main() {
  char choice;

  do {
    do {
      choice = display_menu();
    } while(is_not_valid(choice));

    do_choice(choice);  
  } while(toupper(choice) != 'Q');

  std::cout << "Goodbye!" << std::endl;
  return 0;
}
#包括
#定义大小32
字符显示菜单(){
字符选择;

这是我早些时候想到的

#include <iostream>

using namespace std;

void display();
void menu(int &option, int &decimal);
void outputoption1(int decimal);
void mathoption1(int decimal, int binaryarray[]);
void restartmenu(char &option2, int option, int decimal);

int decimal;
int x;
char option2;


int main()
{
    int option;


    display();
    menu(option, decimal);
    restartmenu(option2, option, decimal);

    return 0;
}

void display()
{
    cout << "Industrial Engineering Decimal Conversion v 1.0\n" << endl;
    cout << "Created by: asdf adsfadf\n"
         << "\t    adsfa adsfad\n" << endl;
    cout << "On the next screen, you will choose which operation you want to perform\n" << endl;

    system("PAUSE");
    cout << "\n" << endl;
}

void menu(int &option, int &decimal)
{
   cout << "Welcome to the IE Decimal Conversion Program!\n\n"
        << "To choose a conversion, enter a number from the menu below\n\n"
        << "1) Decimal to Binary\n"
        << "2) Quit the program\n" << endl;

   cin >> option;

   switch (option)
   {
   case 1:
       cout << "Please input the decimal you want to convert to binary\n\n";
       cin >> decimal;
       outputoption1(decimal);
       break;
   case 2:
       break;
   default:
       cout << "ERROR: Please make a valid selection" << endl;
       menu(option, decimal);
   }
}

void mathoption1(int &decimal)
{
    int binaryarray[32];
    int x = 0;
    int number = decimal;

    while (number != 0)
    {
        binaryarray[x] = number % 2;
        x++;
        number = number / 2;
    }
    for (int y = x - 1; y >= 0; y--)
    {
        cout << binaryarray[y];
    }

}

void outputoption1(int decimal)
{
    cout << "Your original decimal value of " << decimal << " is equivalent to the following binary value:\n";
    mathoption1(decimal);
}

void restartmenu(char &option2, int option, int decimal)
{
    cout << "Restart Program? Y/N" << endl;
    cin >> option2;

    if (option2 == 'y')
    {
        menu(option, decimal);
    }

    if (option2 == 'n')
    {
        exit(0);
    }

    else
    {
        cout << "ERROR: Make Valid Selection" << endl;
        restartmenu(option2, option, decimal);
    }
}
#包括
使用名称空间std;
void display();
无效菜单(整数和选项、整数和小数);
无效输出选项1(整数小数);
void mathoption1(整数十进制,整数二进制数组[]);
无效重新启动菜单(字符和选项2,整数选项,整数小数);
整数十进制;
int x;
字符选项2;
int main()
{
int选项;
显示();
菜单(选项,十进制);
重新启动菜单(选项2,选项,十进制);
返回0;
}
无效显示()
{

你也可以发布错误消息。是的,我刚刚发布了错误消息。我已经更新了我的评论,你还应该注意,当你调用outputoption1时,你的代码不会打印任何内容,因为x是零,你要将x-1分配给y,这意味着你要将-1分配给它,当程序进入循环时,它将退出第一轮由于条件为false,解决方案是将x传递给函数outputoption1或打印整个数组,但在创建数组时应将其清除。此外,还应传递数组binaryarray的引用,而不是在函数mathoption1中创建它,否则数组上的所有更改都将在