0

I'm (probably obviously) very new, and am attempting to build a calculator for my first project. I wanted to test my first concept, but upon compiling I get the 2059 error for the end brace of my InterFace struct as well as the first brace of my int AddUp. These seem like totally random errors. If it helps, the errors are for lines (10,1) and (16,2), although I suspect the 1 and 2 refer to number of like errors recorded? Any help would be appreciated.

1    #include <iostream>
2
3 struct InterFace
4 {
5    char Buttons[4][4]
6    {
7       Buttons[1] = "\u00B1";
8       std::cout << Buttons[1] << std::endl;
9    }
10 };
11
12
13 struct Addition
14 {
15    int AddUp[2]
16    {
17
18    }
19 };



  int main()
  {


  std::cin.get();
  }
  • What are the intended parameters for `char Button`? –  Jun 22 '20 at 03:02
  • 2
    This code contains syntax errors. In a braced initializer list, there can only be expressions separated by commas , which serve as initial values for each element of the thing you are initializing – M.M Jun 22 '20 at 03:07
  • Once I figure out how, I plan to apply all 16 buttons in the array to the window to create a calculator. That first button is supposed to apply the plus/minus sign, and I wanted to make sure I had the right symbol code, and right core concept. – Ben Johnston Jun 22 '20 at 03:09
  • @M.M If I'm understanding right, you mean something like: Buttons[1] = "code or symbol"**,** Am I understanding correctly? – Ben Johnston Jun 22 '20 at 03:20
  • No , for example `char Buttons[4][4] { 'a', 'b', 'c' };` . If you want to have executable assignment statements and so on, those should be placed in a function. It'd be a good idea to learn the language from some learning resource rather than trying to guess syntax as you go – M.M Jun 22 '20 at 03:22
  • I see, thank you. I do have resources incoming, but wanted to get started asap. Thanks for your help. – Ben Johnston Jun 22 '20 at 03:26

2 Answers2

1

You do not have the correct core concepts right, and should probably work through some C++ tutorials or courses before writing a program like this.

A few things:

  1. The ± symbol is a unicode character. char in C++ refers to a single byte, usually an ASCII value if it's referring to text data. So it can't store the unicode +- symbol. Instead, you can store this unicode value in an std::string buttons[4][4]; (although the full answer is much more complicated).
  2. In C++, 'a' refers to the character a, but "a" refers to a const char*. If it wasn't for the unicode issue, you should have used single quotes.
  3. You try to assign to Buttons[1], but buttons is a 2-dimensional array. The element 1 also refers to the second element of the array, which may not be what you intended. Instead you could write Buttons[0][0]='a';
  4. You don't have the concept of a member function/member variable down. The proper way to do this would be to have an initializer function and then call it.

Here is a fixed/working example, but I really recommend going through other tutorials first!

#include <iostream>
#include <string>

struct Interface {
    std::string buttons[4][4];
    void initialize_interface() { 
        buttons[0][0] = std::string("\u00B1");
        std::cout << buttons[0][0] << std::endl;
    }
};

int main() {
    Interface my_interface;
    my_interface.initialize_interface();
    return 0;
}

As M.M. notes in the comments, a more paradigmatic approach would be the following:

#include #include

struct Interface {
    std::string buttons[4][4];
    Interface() { 
        buttons[0][0] = std::string("\u00B1");
        std::cout << buttons[0][0] << std::endl;
    }
};

int main() {
    Interface my_interface;
    return 0;
} 

Interface::Interface is called the constructor, and it runs upon initialization.

David
  • 136
  • 3
  • I think it unfair to assume I haven't taken in any tutorials, as I have watched a fair number of them this week, and understand the concepts of types, classes, structs, private, public, (global) variables, unsigned, and a number of other things. My question demonstrates specifically my lack of understanding of arrays and char in relation to unicode, and I have a lot to learn. Maybe a calculator is reaching too high, but I'm trying to apply what I can. For now I'll stick to basic multiplication and mod formulas while waiting for my book. Thanks for the critique and demo, I'll learn from it. – Ben Johnston Jun 22 '20 at 04:38
  • It's considered good practice to have `initialize_interface` actually be the class constructor. For further reading look up "two-phase initialization" – M.M Jun 22 '20 at 23:42
1

Since I wasn't able to build the calculator as I initially intended, I went a different route. I completed a basic one using switch instead.

#include <iostream>

int main()
{
int r;
int a;
int b;
int result1;
int result2;
int result3;
int result4;
int result5;




std::cout << "Please choose from the available options:" << std::endl << "0. Add" << std::endl << "1. Subtract" << std::endl << "2. Multiply" << std::endl << "3. Divide" << std::endl << "4. Modulo" << std::endl;

std::cin >> r;

switch (r % 5)
{
    
case 0:
    std::cout << "You have chosen to Add, please enter two digits" << std::endl;
    std::cin >> a;
    std::cin >> b;
    result1 = a + b;
    std::cout << "Your sum is " << result1 << std::endl;
    break;
case 1:
    std::cout << "You have chosen to Subtract, please enter two digits" << std::endl;
    std::cin >> a;
    std::cin >> b;
    result2 = a - b;
    std::cout << "Your difference is " << result2 << std::endl;
    break;
case 2:
    std::cout << "You have chosen to Multiply, please enter two digits" << std::endl;
    std::cin >> a;
    std::cin >> b;
    result3 = a * b;
    std::cout << "Your product is " << result3 << std::endl;
    break;
case 3:
    std::cout << "You have chosen to Divide, please enter two digits" << std::endl;
    std::cin >> a;
    std::cin >> b;
    result4 = a / b;
    std::cout << "Your quotient is " << result4 << std::endl;
    break;
case 4:
    std::cout << "You have chosen to perform Modulus, please enter two digits" << std::endl;
    std::cin >> a;
    std::cin >> b;
    result5 = a % b;
    std::cout << "Your answer is " << result5 << std::endl;
    break;
}

std::cin.get();
std::cin.get();

}