Ben Johnston
Ben Johnston

Reputation: 13

Compiler stating error 2059 for struct and int description

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

Upvotes: 0

Views: 94

Answers (2)

David
David

Reputation: 136

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.

Upvotes: 1

Ben Johnston
Ben Johnston

Reputation: 13

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

}

Upvotes: 1

Related Questions