Ok, I've only just started to learn C++ and so far it has been fairly painless.
For my first programe I want to make a simple calculator. I've posted some code below with what I have learnt so far and would like some advise on anything that's missing or would be usefull before I start.
It will be press 'A' to add, 'S' to subtract, 'D' to divide or 'M' to multiply.
I think you're off to a good start. Here are a couple of recommendations. First, I would add another case statement to check for 'A' in case the user inputs 'a' or 'A'. Second, I wouldn't recommend using the system("pause"); statement. This is operating system specific. I would instead use 2 statements.
cin.ignore();
cin.get();
Finally, I would separate the cout statements for readability. When you learn more about C++, you will want to put each calculator function into a function. In that implementation you would ask the user to enter 2 numbers and a decision string (such as 'A' or 'a' for add, etc), and then write code to pass the numbers to a function.
What Other Code Will I Need?
More Coffee (3) Aug 29, 2012 at 6:10am
Ok, I've only just started to learn C++ and so far it has been fairly painless.
For my first programe I want to make a simple calculator. I've posted some code below with what I have learnt so far and would like some advise on anything that's missing or would be usefull before I start.
It will be press 'A' to add, 'S' to subtract, 'D' to divide or 'M' to multiply.
// test.cpp :
//
#include <iostream>
#include <stdio.h>
#include <tchar.h>
#include <cmath>
using namespace std;
int main()
{
cout << "input 'a' to check or anything else to test" << endl; // Tells The User What Todo.
char action; // start
cin >> action;
switch(action)
{
case 'A':
case 'a' :
cout << "check check check" << endl ;
break; // break leaves
default : cout << "test test test" << endl ;
break;
} // end
cout << "hello" << endl << endl; // Says Hello!
cin.ignore();
cin.get();
cout << "Lets Add Two Numbers Together!" << endl << endl;
int number1;
cout << "Please Enter A Number And Press Enter: ";
cin >> number1; cout << endl; // Asks for number1.
cout << "You Entered " << number1 << "." << endl << endl; // Recalls number1.
cout << "Please Enter Another And Press Enter: ";
int number2; cin >> number2;
cout << endl; // /*Asks for number2.
cout << number1 << " + " << number2 << " = " << number1+number2 << endl; /*Adds number1 and number2**/
cout << endl;
cin.ignore();
cin.get();
return(0);
Space out your code a bit more. You have int declarations on lines 31 and 34 that would be best shown on new lines, along with the succeeding cin statements.
You might also want to add some user input validation. When you ask for the numbers, I could type 'bananas' and your code would go bananas.
How about encompassing the whole thing in a loop so that you can perform more calculations?
Finally, and I don't care if anyone pipes in saying that this is unnecessary, but drop the system call for pausing your program. In fact, my above suggestion of looping your program will solve this anyhow, you shouldn't need it at all.