Hello, this is Metalcrunch. I've been learning C++ over the past days and I thought I'd make a calculator. So I looked over the existing code on the internet on how to make a calculator, understood it, then made my own from the very scratch.

However, I'm having a few problems with it - Hope you guys can help me out! :D

main_calculator.cpp

//Calculator Script by Sass.
//main_calculator.cpp

//Default include's.
#include <iostream>
#include <windows.h>
using namespace std;

//Custom header file.
#include "header_calculator.h"

//Main()
int main()
{
	//One-time welcome greeting:
	 SetConsoleTitle("Calculator v1.1");
	 cout << "Kalkulaator v1.1 by MetalCrunch." << endl;
	 cout << "---------------------------------------" << endl << endl;

    //Main program:
	 for (;;) {
	  do {
	   cout << "1 - Addition." << endl;
	   cout << "2 - Subtraction." << endl;
	   cout << "3 - Multiplication." << endl;
	   cout << "4 - Division." << endl;
	   cout << "5 - Abimees." << endl;
	   cin >> choice; }

    switch (choice) {
     //Addition.
	 case 1:
	   cout << "Enter the first number: ";
        cin >> num1;
	   cout << "\nEnter the second number: ";
		cin >> num2;
	    result = num1 + num2;
	   cout << "Answer: ";
	   cout << result;
	   break;

     //Subtraction.
	 case 2:
	   cout << "Enter the first number: ";
        cin >> num1;
	   cout << "\nEnter the second number: ";
		cin >> num2;
		result = num1 - num2;
	   cout << "\nAnswer: ";
	   cout << result;
	   break;

     //Multiplication.
	 case 3:
		 cout << "Enter the first number: ";
		  cin >> num1;
		 cout << "\nEnter the second number: ";
		  cin >> num2;
		  result = num1 * num2;
		 cout << "\nAnswer: ";
		 cout << result;
		  break;

     //Division.
	 case 4:
		 cout << "Enter the first number: ";
		  cin >> num1;
	     cout << "\n Enter the second number: ";
		  cin >> num2;
		  result = num1 / num2;
		 cout << "\nAnswer: ";
		 cout << result;
		  break;

     //"Information".
	 case 5:
		 cout << "Calculator v1.1 by Metalcrunch" << endl;
		 cout << "Enter the number 1-5, then answer the questions..." << endl;
		 cout << "The program will solve it all." << endl;
		 cout << "But please don't enter a value over 9 numbers, the program will crash!" << endl;
		 break;

	 default:
	     cout << "test";
	     break; }
	 }
 return 0;
}

header_calculator.h

//Calculator Script
//header_calculator.h

#ifndef HEADER_CALCULATOR_H
#define HEADER_CALCULATOR_H

//Main int's, for calculation.
int num1, num2;
int result;

//The "choice" int.
int choice;

#endif

The build log.

Compiling...
main_calculator.cpp
c:\documents and settings\*********\my documents\visual studio 2008\projects\project1\calculator\calculator\main_calculator.cpp(31) : error C2059: syntax error : 'switch'
c:\documents and settings\*********\my documents\visual studio 2008\projects\project1\calculator\calculator\main_calculator.cpp(31) : error C2143: syntax error : missing ';' before '{'
c:\documents and settings\*********\my documents\visual studio 2008\projects\project1\calculator\calculator\main_calculator.cpp(33) : error C2046: illegal case
c:\documents and settings\*********\my documents\visual studio 2008\projects\project1\calculator\calculator\main_calculator.cpp(44) : error C2046: illegal case
c:\documents and settings\*********\my documents\visual studio 2008\projects\project1\calculator\calculator\main_calculator.cpp(55) : error C2046: illegal case
c:\documents and settings\*********\my documents\visual studio 2008\projects\project1\calculator\calculator\main_calculator.cpp(66) : error C2046: illegal case
c:\documents and settings\*********\my documents\visual studio 2008\projects\project1\calculator\calculator\main_calculator.cpp(77) : error C2046: illegal case
c:\documents and settings\*********\my documents\visual studio 2008\projects\project1\calculator\calculator\main_calculator.cpp(84) : error C2047: illegal default

I can't honestly figure it out ... I'd really, really like some help!
metalcrunch

#include <iostream>
double a = 0;
double b = 0;
double c = 0;
double d = 0;
double add = 0;
double sub = 0;
double mul = 0;
double f = 0;
int main () {
	std::cout << "Enter any one of the following " << std::endl;
	std::cout << "1 for ADDITION " << std::endl;
	std::cout << "2 for SUBTRACTOIN " << std::endl;
	std::cout << "3 for MULTIPLICATOIN " << std::endl;
	std::cout << "4 for DIVISION " << std::endl;
	std::cin >> a;
	std::cout << ' ' << std::endl;
	std::cout << "Enter first number: " <<std::endl;
	std::cin >> b;
	std::cout<< ' ' << std::endl;
	std::cout << "Enter second number: " << std::endl;
	std::cin >> c;
	std::cout<< ' ' << std::endl;
	if ( a == 1) {
		add = b + c;
		std::cout << add <<  std::endl;
	}else if ( a == 2){
		sub = b - c;
		std::cout << sub <<  std::endl;
	}else if (a == 3) {
		mul = b * c;
		std::cout << mul <<  std::endl;
    }else if (a == 4){
		f = b / c;
		std::cout << f <<  std::endl;

	}
	


}

well here is simple solution to your very simple project

Hi, me again. Well, first of all, big thanks to AceOfSpade19 for clearing that up to me.
Second of all, thank you ALI INAM for trying to help me, however next time try putting the [.code=c++][./code] tags.

And third of all, I'm still stuck. Here's how the Main Program part looks like currently.

//Main program:
	 for (;;) {
	  do {
	   cout << "1 - Addition." << endl;
	   cout << "2 - Subtraction." << endl;
	   cout << "3 - Multiplication." << endl;
	   cout << "4 - Division." << endl;
	   cout << "5 - Abimees." << endl;
	   cin >> choice; }

    switch (choice) {
     //Addition.

Now, that was the default Main Code. If I add the "while" command to it like this,

//Main program:
	 for (;;) {
	  do {
	   cout << "1 - Addition." << endl;
	   cout << "2 - Subtraction." << endl;
	   cout << "3 - Multiplication." << endl;
	   cout << "4 - Division." << endl;
	   cout << "5 - Abimees." << endl;
	   cin >> choice; }
	  while (choice < 1 || choice > 5) {
	   break; }

    switch (choice) {
     //Addition.

Then it will get me this error.

error C2143: syntax error : missing ';' before '{'

If I follow the error, and put it like this,

//Main program:
	 for (;;) {
	  do {
	   cout << "1 - Addition." << endl;
	   cout << "2 - Subtraction." << endl;
	   cout << "3 - Multiplication." << endl;
	   cout << "4 - Division." << endl;
	   cout << "5 - Abimees." << endl;
	   cin >> choice; }
	  while (choice < 1 || choice > 5); {
	   break; }

    switch (choice) {
     //Addition.

It will compile "succesfully" but the script is broken, if I enter any number/letter and press Enter it will exit immediately... Right..

I also tried to make it like this.

//Main program:
	 for (;;) {
	  do {
	   cout << "1 - Addition." << endl;
	   cout << "2 - Subtraction." << endl;
	   cout << "3 - Multiplication." << endl;
	   cout << "4 - Division." << endl;
	   cout << "5 - Abimees." << endl;
	   cin >> choice; }
	  while (choice < 1 || choice > 5)
	   break;

    switch (choice) {
     //Addition.

However, that didn't help much, because I get this error.

syntax error : missing ';' before 'break'

Well, I'm stuck... Again.. Would be nice if anyone would told me what to do... Well, would be even better, if I would be told what to put in the "while" statement...

the syntax for a do-while loop is this:

do
{
     //stuff to loop through
}
while(conditions); //note the colon

and maybe consider changing your 'switch' statement to a series of if-else since there's more than 1 or 2 lines of code for each option. I think that's more of a style issue.

Edit: one more thing I noticed

you have your 'for(;;)' but once you run through your do-while loop, you have a break, exiting the for loop. So you only run through the for loop once, maybe look at why you have a break there

Hope that's helpful

Well, thanks, mate... This is what I got, and it works..

//Main program:
	 for (;;) {
	  do {
	   cout << "1 - Addition." << endl;
	   cout << "2 - Subtraction." << endl;
	   cout << "3 - Multiplication." << endl;
	   cout << "4 - Divide." << endl;
	   cout << "5 - Help." << endl;
	   cin >> choice; }
	  while (choice < 1 || choice > 5);

    switch (choice) {
	 default:
	     cout << "If you see me, report the 'default' bug!";

     //Liitmine.

Although, I have to admit... I don't fully understand why do I need this.

while (choice < 1 || choice > 5);

If I change it to like, "choice > 1 || choice < 5" then it stops working. But that seems more logical, doesn't it?

I won't mark it as solved, yet, because I'd like to know why do I need the "while" statement to be like that (shown in the code tags, previously). Is it because if you put a number below 1 and over 5 it will "re-do" the Do function? Seems to function like so...

Thank you all, though.
metalclunch

Although, I have to admit... I don't fully understand why do I need this.

while (choice < 1 || choice > 5);

If I change it to like, "choice > 1 || choice < 5" then it stops working. But that seems more logical, doesn't it?

>Although, I have to admit... I don't fully understand why do I need this.

while (choice < 1 || choice > 5);

Well, this way works because: if the choice entered by the user is a number, lower than one, OR a number higher than five, then the choice is invalid.

>If I change it to like, "choice > 1 || choice < 5" then it stops working. But that seems more logical, doesn't it?
I know what you mean, if you want to have it work in that way, then you have to replace the logical OR by a logical AND, so that it becomes: choice > 0 [B]&&[/B] choice < 5 (I also changed the one to a zero here).
(Because the number has to be higher than zero AND lower than five, it's not enough that just one of these conditions is true, they must be both true).

Thanks, many thanks indeed. I've got my Calculator script fully working now. I also "optimized" the code, by cleaning it up and also making it more readable.

I guess I can Mark this as Solved. +rep to the people who helped me, of course!

choice > 0 && choice < 5 (I also changed the one to a zero here).

Hmm... if the range is 1 - 5 (5 included), shouldn't we use choice > 0 && choice <6 or choice > 0 && choice <= 5 ?

Comments
Good catch :)

Hmm... if the range is 1 - 5 (5 included), shouldn't we use choice > 0 && choice <6 or choice > 0 && choice <= 5 ?

Oops! Yes, I thought there were only 4 options, you are right.
Thanks for the catch :)

EDIT:: I said it that way because the OP asked it in that way.
(So according to that block of code, when I saw it, I automatically assumed that there were only four options :D)

One more thing which is surely going to help you in the future: try spending as much time as possible TESTING your program. You said it works fine, but if you had tried each case, you would have found a problem.

This question has already been answered. Start a new discussion instead.