how to make a simple c++ calculator

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

WiTH

Jr.Coder
Full Member
Nobleman
May 24, 2012
58
458
1
hello guys, this is my first post here and my first program...
i like c++, but its hard for first language, so i stopped for some time and i´m currently learning c#

C++:
#include "stdafx.h" // only needed for microsoft compiler
#include <iostream>

using namespace std;


int main()
{
	long double w,i,t; // declaring the first and second operator, and result
	char h; // declaring the operator choice and at the same time, the choice of using the calculator again

	cout << "Simple Calculator\n" << "Write the first number:" << endl; // simple text output
	cin >> w; // waiting for input
	cout << "Now write the second number:\n";
	cin >> i;
	cout << "And the Operator:\n";
	cin >> h;

	if (h == '+') // comparing the opperator
		t = w + i;
	if (h == '-')
		t = w - i;
	if (h == '/')
		t = w / i;
	if (h == '*')
		t = w * i;

	cout << "\nThe result is: " << t << endl << "If you want to use the calculator again press \"y\"...\n"; // show the result
	cin >> h;

	if (h == 'y')
	{
		system ("cls"); // clear console screen
		return main(); // restart calculator
	}
	else
		return 0; // quit calculator
}
this is very simple, but can help some beginners and i would like some feedback :)

 
  • Like
Reactions: Fegga

Fleep

Founder
Meme Tier VIP
May 20, 2012
572
11,023
6
Nice and simple, but good program for a beginner ;)

Fleep
 

Flyjunkie

Newbie
Full Member
Nobleman
May 22, 2012
45
373
0
Nice and easy way to get people into a little bit of coding there WiTH def liking it!
 

AxDSan

Newbie
Full Member
Nobleman
May 24, 2012
58
703
1
Another neat idea, is to make a switch:case menu with options you want :D Addition, Substraction etc, etc. :D
 

Flyjunkie

Newbie
Full Member
Nobleman
May 22, 2012
45
373
0
Another neat idea, is to make a switch:case menu with options you want Addition, Substraction etc, etc.
cases ftw! May also be an idea to get them in the habit of checking if the input is valid or not using a FOR and the 'isdigit' function...in this numeric case.
 

WiTH

Jr.Coder
Full Member
Nobleman
May 24, 2012
58
458
1
thanks fleep and flyjunkie
the switch case sounds a good idea, but im more used to if statements :p
 
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods League of Legends Accounts