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...