通过函数传递数组

时间:2015-12-06 02:10:41

标签: c++ arrays

我正在为一个简单的计算器编写代码,该计算器将执行几个十进制转换(我现在关注的是1)。基本上在用户在菜单中进行选择(switch语句)之后,我想要一个单独的函数来执行实际的转换(math)和一个为解决方案(输出)保留的函数。我需要帮助将mathoption1中创建的binaryarray传递给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];
    }
}

非常感谢任何帮助/意见/建议。

不确定如何剪切和粘贴错误,但它们列在下面: 语法错误:&#39;]&#39; @第47行

预期表达式@第47行

编辑:代码已更新以更正MENU参数。

5 个答案:

答案 0 :(得分:1)

功能菜单的原型与实际功能标题不同,您应该更新标题,使其变得像原型一样,您必须在十进制之前添加&amp; 功能 mathoption1 标题

答案 1 :(得分:1)

        outputoption1(decimal, binaryarray[]);

删除[]&#39; s:仅传入变量。

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

有菜单调用本身是非常规的。最好使用循环。

您未获得任何输出,因为outputoption1中的x为0,但您需要在mathoption1中生成的x。另外因为mathoption1有两个版本的binaryArray:可以共享的参数和本地变量,它们可以是&。

所以基本上你有参数问题。问问自己每个功能:这个功能在完成工作时需要知道什么?把它放在()之间。它为调用程序提供了什么值(如果有的话)?并将其作为返回类型或在()之间。

mathoption1会比convertToBinary更好。转换为二进制的函数需要知道什么?十进制数。它会为调用函数提供什么?二进制数组AND二进制数字的数量。 (请注意,这不会向用户询问十进制数字,但会从参数列表中获取。这比现在的情况更好,原因有二:它更通用(如果它更通用)你有一个非交互式程序),并且比函数&#34; askUserForANumberAndConvertToBinary更具连贯性和#34; convertToBinary&#34;&#34;

outputoption1也可以更简单,如果它是&#34; printBinary,&#34;而不是&#34; printOriginalDecimalValueAndBinary。&#34;所以:如果是printBinary,它还需要知道什么?告诉它。

所以我认为你应该考虑组织和参数。得到那个连贯的,然后再回到编译问题。

答案 2 :(得分:1)

主要问题是您没有将binaryarray传递给mathoption1,但我还有很多其他问题需要解决。

使用bool来表示二进制

  • 您不需要为数组中的每个元素提供整个int。二进制是一个布尔值,因此您可以使用bool(如布尔代数)。想一想就更自然了。

数组的硬编码大小是禁止

  • 您不应该硬编码数组的大小。通常,您将在C ++ 11中使用宏(#define SIZE)或constexpr

使用函数描述程序流程

  • 您应该尝试为读者提供有意义的功能。 mathoption1虽然可读,但并不能告诉我它在做什么。在保持可读性的同时,convert_to_binary或类似内容更容易理解。

避免using namespace std;

其他说明

  • 请注意,此转换算法仅转换正数。你想要处理否定(一些和两个恭维)。
  • 请注意,此转换仅适用于 4294967295 之前的用户输入。因为您的数组大小为32,所以只能解决2 32 -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;
}

答案 3 :(得分:1)

@erip - 这是我之前提出的。

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

答案 4 :(得分:0)

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

相关问题