1

OK... so, i'm trying to learn C++ by using it and coming up with tasks along the way. A task that I made for me is to make a program that acts like a "universal calculator" basically a calculator that can ask "what would you like to do?" and if the user inputs "calculator" it will run a calculator app that I've already made. But I want to make the program so that it uses a header file to store all of my functions. To do so I'm fairly certain that I need to use Classes witch is fine, I just don't know how to use classes. I've imported my calculator program into my universal calculator header file and I think I did it correctly since there isn't any errors in the debug and there isn't any red or green squiggly lines under anything. Same goes for what's inside the universal calculator CPP file.

And with that, my problem lies where I can't compile and run my code. When I compile I get two errors

error C2653: 'calculator': is not a class or namespace name

error C2065: 'CalculatorApp': undeclared identifier

Looking into this it seems that there is some problem that lies in the "#include header file." When I comment "#include " or something like that in the main CPP file, I get a similar problem in the debug:

error C2065: 'cout': undeclared identifier

error C2065: 'cin': undeclared identifier

But do note that when I do this, the actual "cin" and "cout" functions (Commands? idk what its called) do not have a red squiggly under it. Its like the debug missed the memo of "#include Header file" and is reading the code differently.

Universal Calculator V1.0.CPP

// Universal Calculator V1.0.cpp : Defines the entry point for the console application.
//
#include "Universal Calculator.h"
#include "stdafx.h"
#include <string>
#include <iostream>
#include <Windows.h>

using namespace std;

int main()
{
    string task;
    cout << "What would you like to do? ";
    cin >> task;
    if (task == "calculator")
    {
        calculator::CalculatorApp;
    }
    Sleep(3000);
    system("CLS");
    return main();
}

Universal Calculator.h Header file:

#ifndef UNIVERSAL CALCULATOR_h
#define UNIVERSAL CALCULATOR_h

#include "stdafx.h"
#include <string>
#include <iostream>
#include <Windows.h>

using namespace std;

class calculator
{
public:

    float FirstNumber;
    float SecondNumber;
    float answer;

    void Add()
    {
        cout << "What is your first number? ";
        cin >> FirstNumber;

        cout << "What is your second number? ";
        cin >> SecondNumber;

        answer = FirstNumber + SecondNumber;
        cout << "The answer is: " << answer << endl;
    }

    void Subtract()
    {
        cout << "What is your first number? ";
        cin >> FirstNumber;

        cout << "What is your second number? ";
        cin >> SecondNumber;

        answer = FirstNumber - SecondNumber;
        cout << "The answer is: " << answer << endl;
    }

    void Multiply()
    {
        cout << "What is your first number? ";
        cin >> FirstNumber;

        cout << "What is your second number? ";
        cin >> SecondNumber;

        answer = FirstNumber * SecondNumber;
        cout << "The answer is: " << answer << endl;
    }

    void Divide()
    {
        cout << "What is your first number? ";
        cin >> FirstNumber;

        cout << "What is your second number? ";
        cin >> SecondNumber;

        answer = FirstNumber / SecondNumber;
        cout << "The answer is: " << answer << endl;
    }
    void CalculatorApp()
    {

        int Calculator();
        {
            int Operation;
            cout << "Bode's Calculator V2.1" << endl;
            cout << "What is the operation? Add[1], Subtract[2], Multiply[3] or Divide[4]? ";
            cin >> Operation;

            switch (Operation)
            {
            case 1:
                Add();
                break;
            case 2:
                Subtract();
                break;
            case 3:
                Multiply();
                break;
            case 4:
                Divide();
                break;
            }
        }
    }

};
#endif

I know that this post is already really long but in addition: I also changed the "#pragma once" to what it is now thinking that was the problem but that changed no noticeable differences. And one last question: if you have all of your "#includes" inside your header file and you have "#include header file" in your main CPP, then shouldn't you not need to also have the #includes inside the main CPP file?

Thank you for your time to read this really long post. I'm sorry in advance if I missed something stupid when making this...

Community
  • 1
  • 1
  • 3
    Always make `#include "stdafx.h"` be the firsl `#include`. Or probably better when you are learning, don't use precompiled headers at all. –  Aug 21 '18 at 17:33
  • Not quite a duplicate, but your answer can be found here: [Purpose of stdafx.h](https://stackoverflow.com/questions/2976035/purpose-of-stdafx-h) – user4581301 Aug 21 '18 at 17:34
  • 1
    `#ifndef UNIVERSAL CALCULATOR_h` - did you miss an underscore? – Maxim Egorushkin Aug 21 '18 at 17:34
  • If you put code into a header file, when you modify the code, all source files that include the header will need to be recompiled. If the code is in a source file, only the source file needs to be recompiled. – Thomas Matthews Aug 21 '18 at 18:48
  • What is this syntax: `void CaclulatorApp() { int Calculator(); {`? Are you trying for nested functions? – Thomas Matthews Aug 21 '18 at 18:50
  • You can save yourself some code by prompting the User for two values, after you ask for the operation, but before you process the operation in the `switch` statement. – Thomas Matthews Aug 21 '18 at 18:54
  • The `main` function cannot be called recursively, i.e. `main` can't call `main`. – Thomas Matthews Aug 21 '18 at 18:55
  • I suggest you use a loop in your `main` rather than recursion. Proper recursion needs to *unwind* when exiting or returning. Also, recursions occupies space on the stack for each call, such a waste. A simple loop will be more efficient in terms of execution speed and code space. – Thomas Matthews Aug 21 '18 at 18:57
  • You need an instance of the `class calculator` in order to execute `CalculatorApp`. You can avoid the instance requirement by declaring the `CalculatorApp()` as static. – Thomas Matthews Aug 21 '18 at 18:59
  • Remove `#include ` and `#include ` from your header. Nothing in your header file is referencing content in these include files. – Thomas Matthews Aug 21 '18 at 19:02
  • Also, `using namespace std;` is evil in a header file. It brings in the entire `std` namespace into every source file that includes it. Prefix things with `std::` inside your header files (or move the code into a source file). – Thomas Matthews Aug 21 '18 at 19:05
  • C++ is simply very difficult to learn properly using only online sources and guessing, even if you have experience in another language. I highly recommend you get a [good book](https://stackoverflow.com/q/388242/9254539) as you've got a lot to (un)learn. – eesiraed Aug 21 '18 at 23:19

2 Answers2

0

When I compile I get two errors

Solution here is that inside Universal Calculator V1.0.CPP you need to switch order of the first two #include directives, so that stdafx.h will be the first one to be included. See here where it says:

Visual C++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default); it assumes all code in the source up to and including that line is already compiled.

Better yet, read here to make sure you actully need to be using Precompiled Headers.

When I comment "#include " or something like that in the main CPP file, I get a similar problem

 Yes, you need those includes so don't comment them out. Your problem was the wrong order when using stdafx.h noted above.

if you have all of your "#includes" inside your header file and you have "#include header file" in your main CPP, then shouldn't you not need to also have the #includes inside the main CPP file?

Under most circumstances (if you're learning C++ then I'd follow this for now), you should only include in each source file (either .cpp or .h) what's necessary for the code in the file. As in, include declaration for what is being used. It's better to not exclude anything, relying in whats already in other .h files already included, as they may change and you'll stop compiling. Include what you need, only what you need, and don't leave anything out.

Geezer
  • 5,600
  • 18
  • 31
0

Well your first problem is in your header guard's identifier which contains a white space (This is not allowed). Futhermore, identifiers must begin with either a letter or an underscore. Identifiers are case-sensitive; upper- and lowercase letters are distinct.

Your other problem is the #include directive "stdafx.h" which has to be placed before any other directive. Your program is simple, you don't need this header so you may deleted from both files. However, if you are curious about the role of this header I recommend you to read this article.

Next, in order to use the accesible members of your calculator class, it's necessary to instatiate an object of type calculator. For this you better read about classes, I recommend you this site.

Lastly, your design for your calculator class it's inneficient. You need to read about abstraction and encapsulation, these concepts are the basis of Object Oriented Programming. This knowledge will help you to structure your classes neatly.

  • I fixed most all other problems that has been proposed on this forum and it works! But one thing that doesn't make sense is the header guard's white space. Wouldn't you just type whatever the headerfile's name is, in caps, and a underscore instead of a dot? Normally I would use "#pragma once" but i thought this might be the original problem. Please note: I'm not asking because I think your wrong, I'm asking because I'm trying to understand. – Bode Vaughn Aug 22 '18 at 16:38
  • Also about abstraction, I'm aware that within a class you can protect or public certain code but why would you do that? Is it for performance reasons? And the other problem is I don't know what code is suppose to be protected and what code isn't supposed to be protected? Where can i learn this (I've tried looking it up on YouTube but can't find a good answer)? – Bode Vaughn Aug 22 '18 at 16:45
  • About headers, the name you give to variables is known as _identifiers_. In the first paragraph of my answer I mentioned the **rules** for naming _identifiers_, there's nothing to understand in that, it's simply a requirement for the lenguage. – Edgar Hernandez Aug 22 '18 at 17:26
  • About abstraction, you have to take in count that there're 2 posible users of the code you're writing: first, the _user_ of your programm and second, other programmers. Sometimes you'll be creating an **interface** for other programmers to work with, so you would make **public** some things for the users (programmers) and make private the **implementation** which is what makes the **interface** work and it can't be seen by the _user_. Search about this 2 concepts (interface and implementation) to totally grasp what I'm explaining. – Edgar Hernandez Aug 23 '18 at 01:14
  • I recommend you this [site](https://www.learncpp.com/) and the best book for me is **Cpp Primer** by _Stanley Lippman_. – Edgar Hernandez Aug 23 '18 at 01:17