-1

I am writing a code for a simple calculator that will perform several decimal conversions (I am currently concerned with 1 as of right now). Essentially after the user makes the selection in the menu (switch statement) I want to have a separate function to perform the actual conversion (math) and a function that is reserved for the solution (output). I need help with passing the binaryarray created in mathoption1 to outputoption1. The code now does not compile, I have 4 errors associated with the parameters of some of the functions. Obviously, I am lost and could use some guidance.

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

Any help/input/advice would be greatly appreciated.

Not sure how to cut and paste errors, but they are listed below: syntax error: ']' @ line 47

expected an expression @ line 47

EDIT: Code Updated to correct MENU parameters.

kingpin
  • 85
  • 1
  • 14

5 Answers5

1

The prototype for function menu differs from the real function header you should update the header to become like the prototype also you must add & before decimal in funtion mathoption1 header

abdallahesam
  • 122
  • 6
  • I have updated my comment, also you should note that your code wont print anything when you call **outputoption1** because x is zero and your going to assign x - 1 to y thats mean you are assigning -1 to it and when the program come to the loop its going to exit form the first round because the condition is false, the solution is to pass x to function **outputoption1** or printing the whole array but you should clear it when you create it. – abdallahesam Dec 06 '15 at 03:03
  • Also you should pass a refernce for array **binaryarray** instead of creating it in function **mathoption1** or all of the changes on the array will Go down the drain – abdallahesam Dec 06 '15 at 03:09
1
        outputoption1(decimal, binaryarray[]);

Remove the []'s: pass in only the variable.

menu shouldn't have parameters, because none of those things are things that main needs to tell menu (or even know about). They can be local to menu.

It's unconventional to have menu call itself. Better to use a loop.

You're not getting any output because the x you have in outputoption1 is 0, but you needed the x that you generated in mathoption1. Also because mathoption1 has two version of binaryArray: the parameter, which can be shared, and the local variable, which can't.

So essentially you're having parameter problems. Ask yourself for each function: what precisely does this function need to know to do its job? and put that between the ()'s. What values, if any, does it provide to the calling program? and put that either as a return type, or between the ()'s.

mathoption1 would be better as convertToBinary. What would a function to convert to binary need to know? A decimal number. What would it provide the calling function? A binary array AND the number of binary digits. (Note that this wouldn't ask the user for a decimal number, but would get it from a parameter list. This is better than what you have now for two reasons: it's more versatile (it would work if you had a non-interactive program), and it's more coherent to have a function "convertToBinary" than a function "askUserForANumberAndConvertToBinary."

outputoption1 also can be simpler if it is "printBinary," rather than "printOriginalDecimalValueAndBinary." So: if it were printBinary, what would it need to know? Tell it.

So I think you should be thinking about organization, and parameters. Get that coherent and then come back to the problem of compilation.

Topological Sort
  • 2,733
  • 2
  • 27
  • 54
1

The main problem is that you're not passing binaryarray into mathoption1, but there are a lot of other issues I'd like to address.

Use bools to represent binary

  • You don't need an entire int for each element in the array. Binary is a boolean value, so you can just use bool (like boolean algebra). It's much more natural to think about it.

Hardcoded sizes of arrays are a no-no

  • You should not hard code the size of arrays. Typically you'll use macros (#define SIZE) or constexpr in C++11

Use functions to describe the flow of your program

  • You should try to give names to functions that make sense to a reader. mathoption1, while readable, doesn't tell me what it's doing. convert_to_binary or something similar is much more understandable while maintaining readability.

Avoid using namespace std;

Other notes

  • Note that this conversion algorithm will only convert positive numbers. You'll want to deal with negatives (ones and twos compliment).
  • Note that this conversion will only work with user inputs up to 4294967295. Because your array is sized at 32, you can only address to 232-1. If someone enters 4294967296, you'll have a bad time. I'll let you figure out how you want to handle that. You may want to do more error checking - that's great. I didn't do much in the code below to leave for you as an exercise.

You'll find the new code below.

#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;
}
Community
  • 1
  • 1
erip
  • 16,374
  • 11
  • 66
  • 121
1

@erip - This is what I had come up with earlier.

#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);
    }
}
kingpin
  • 85
  • 1
  • 14
  • I still think you should look at my answer. – erip Dec 06 '15 at 19:26
  • Yes, I saw it. You brought up several good points that I did not consider when I was creating my program. I will be using the answer you posted as reference for future similar problems. – kingpin Dec 06 '15 at 19:30
  • You may get it cobbled together somehow. But at this level, it's best to learn how and when to use functions and parameters. Even if you get it working, if it's unclear, un-modular, or hard to reuse, that's not the best kind of practice. Your original version didn't work yet, but it did separate conversion to binary from printing binary, which was the best way to go -- and erip's version shows how to fix it without sacrificing that modularity. – Topological Sort Dec 06 '15 at 22:12
  • Out of curiosity, why would separating the calculation and the output be better than the answer I posted. I originally tried to separate the two because at the time I was unsure how to go about solving the issue. – kingpin Dec 06 '15 at 22:19
  • Modularity, clarity, reusability. A function that converts to binary is more reusable than one that converts to binary and prints the binary number, because you may want to get a binary number sometimes and _not_ print it. It's clearer, because "convert to binary" and "print binary number" are simple concepts, but "convert to binary and print binary number" is less so. And it's more modular by definition. You have to be good at specifying parameters, but that's useful anyway. ...do you have a professor? Put the question to him too: that's a more important takeaway than just a bug fix. – Topological Sort Dec 07 '15 at 03:47
0

UPDATE: I found a way to do what I needed to do without having to pass the array through the function.

kingpin
  • 85
  • 1
  • 14