| View previous topic :: View next topic |
| Author |
Message |
Negima I post too much
Reputation: 6
Joined: 22 May 2007 Posts: 2221
|
Posted: Thu Apr 03, 2008 12:09 am Post subject: Multiplication calculator I made |
|
|
Hey guys, this is the first calculator I have made and I wanted to show you guys the source to give me some tips/tell me things to avoid.
I know I shouldn't use system("pause") API but I like how you have to press a key to continue, if there is another way I can do this please tell me.
Also, how can I get it so when you run the app you can choose between multiply/add/subtract/divide?
| Code: | #include <iostream>
int main ()
{
signed short MultiA, MultiB, Product;
std::cout << "Please enter two numbers to multiply\n";
std::cin >> MultiA;
std::cin >> MultiB;
system("pause");
Product = MultiA * MultiB;
std::cout << MultiA << "\n" <<"Multiplyed by: " << MultiB << "\n" <<"Equals: " << Product << "\n";
system("pause");
return 0;
} |
|
|
| Back to top |
|
 |
Pseudo Xero I post too much
Reputation: 0
Joined: 16 Feb 2007 Posts: 2607
|
Posted: Thu Apr 03, 2008 12:11 am Post subject: |
|
|
Just so you know, system("pause") isn't an API.
_________________
| haxory' wrote: | can't VB do anything??
windows is programmed using VB right? correct me if im wrong.
so all things in windows you have like the start menu is a windows form too. |
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Thu Apr 03, 2008 12:24 am Post subject: Re: Multiplication calculator I made |
|
|
| Negima wrote: | Hey guys, this is the first calculator I have made and I wanted to show you guys the source to give me some tips/tell me things to avoid.
I know I shouldn't use system("pause") API but I like how you have to press a key to continue, if there is another way I can do this please tell me.
Also, how can I get it so when you run the app you can choose between multiply/add/subtract/divide?
| Code: | #include <iostream>
int main ()
{
signed short MultiA, MultiB, Product;
std::cout << "Please enter two numbers to multiply\n";
std::cin >> MultiA;
std::cin >> MultiB;
system("pause");
Product = MultiA * MultiB;
std::cout << MultiA << "\n" <<"Multiplyed by: " << MultiB << "\n" <<"Equals: " << Product << "\n";
system("pause");
return 0;
} |
|
1. std::cin.ignore()
std::cin.sync()
2. make a loop that lets the user pick what they want and have it loop until the user specifies they don't want to do anything else.
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Thu Apr 03, 2008 1:51 am Post subject: |
|
|
What if user inputs letters? How about overflows? 0 isn't always EXIT_SUCCESS. std::endl is a nice thing too.
And why to hell you use short ints? -_- Even long long long .. long ints are sometimes too short...
Btw.. Why to pause before showing the result?
| slovach wrote: | 1. std::cin.ignore()
std::cin.sync() | Usually ppl sync() before ignoring to make sure there's nothing in the buffer...
|
|
| Back to top |
|
 |
Zand Master Cheater
Reputation: 0
Joined: 21 Jul 2006 Posts: 424
|
Posted: Thu Apr 03, 2008 2:28 am Post subject: |
|
|
| Ask the user to select numbers to choose between division and multiplication then use a switch statement or if you want the user to type it out, do a stricmp.
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Thu Apr 03, 2008 2:57 am Post subject: |
|
|
| Zand wrote: | | Ask the user to select numbers to choose between division and multiplication then use a switch statement or if you want the user to type it out, do a stricmp. | If you're coding in C++, why to use stricmp?
|
|
| Back to top |
|
 |
92Garfield I'm a spammer
Reputation: 57
Joined: 20 Dec 2007 Posts: 5871 Location: Banana Republic Germany
|
Posted: Thu Apr 03, 2008 4:06 am Post subject: |
|
|
i like your avatar but make it smaller
and remember the cube wont speak to you
_________________
|
|
| Back to top |
|
 |
Negima I post too much
Reputation: 6
Joined: 22 May 2007 Posts: 2221
|
Posted: Thu Apr 03, 2008 5:12 am Post subject: |
|
|
| Jani wrote: | What if user inputs letters? How about overflows? 0 isn't always EXIT_SUCCESS. std::endl is a nice thing too.
And why to hell you use short ints? -_- Even long long long .. long ints are sometimes too short...
Btw.. Why to pause before showing the result?
| slovach wrote: | 1. std::cin.ignore()
std::cin.sync() | Usually ppl sync() before ignoring to make sure there's nothing in the buffer... | What amount of memory would you recommend for these variables? | Quote: | | Ask the user to select numbers to choose between division and multiplication then use a switch statement or if you want the user to type it out, do a stricmp. | How would I do this?
|
|
| Back to top |
|
 |
Zand Master Cheater
Reputation: 0
Joined: 21 Jul 2006 Posts: 424
|
Posted: Thu Apr 03, 2008 7:34 am Post subject: |
|
|
Uh..
| Code: |
#include <iostream>
enum CalcMode
{
MODE_MULTIPLICATION = 1,
MODE_DIVISION = 2,
MODE_ADDITION = 3,
MODE_SUBTRACTION = 4,
};
int main ()
{
signed short NumA, NumB, Product, Mode;
char cMessage[255] = "Please enter two numbers to ";
std::cout << "Please choose calculator mode.\n1. Multiplication\n2. Division\n3. Addition\n4. Subtraction\n\n";
std::cin >> Mode;
std::cout << "\n";
switch ( Mode )
{
case MODE_MULTIPLICATION:
strcat(cMessage, "multiply\n");
break;
case MODE_DIVISION:
strcat(cMessage, "divide\n");
break;
case MODE_ADDITION:
strcat(cMessage, "add\n");
break;
case MODE_SUBTRACTION:
strcat(cMessage, "subtract\n");
break;
default:
std::cout << "No such mode\n\n";
system("pause");
return 0;
break;
}
std::cout << cMessage << "\n";
std::cin >> NumA;
std::cin >> NumB;
std::cout << "\n";
//system("pause");
switch ( Mode )
{
case MODE_MULTIPLICATION:
Product = NumA * NumB;
break;
case MODE_DIVISION:
Product = NumA / NumB;
break;
case MODE_ADDITION:
Product = NumA + NumB;
break;
case MODE_SUBTRACTION:
Product = NumA - NumB;
break;
}
//Product = NumA * NumB;
//std::cout << NumA << "\n" <<"Multiplyed by: " << NumB << "\n" <<"Equals: " << Product << "\n";
std::cout << "Answer is : " << Product << "\n\n";
system("pause");
return 0;
} |
psuedo code. I'm using system pause as it's your code. This can probably be made into much more efficient code.
|
|
| Back to top |
|
 |
DoomsDay Grandmaster Cheater
Reputation: 0
Joined: 06 Jan 2007 Posts: 768 Location: %HomePath%
|
Posted: Thu Apr 03, 2008 9:39 am Post subject: |
|
|
The code looks well, but I think you should use printf() though, it's easier to work with.
P.S. | Code: | @echo off
title Calc
:start
cls
echo ===============
echo Enter argument:
set /p arg=
set /a arg=%arg%
echo
echo Result: %arg%
echo ===============
pause > nul
goto start |
|
|
| Back to top |
|
 |
WafflesFTW Expert Cheater
Reputation: 0
Joined: 21 Mar 2008 Posts: 131
|
Posted: Thu Apr 03, 2008 3:40 pm Post subject: |
|
|
printf is C. I prefer cout.
You should expand your program to make decisions based on user input, like ask them to insert and expression. It can be very helpful to make as it teaches a lot about arrays, etc
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Fri Apr 04, 2008 3:00 am Post subject: |
|
|
| Negima wrote: | | What amount of memory would you recommend for these variables? | Infinite... I mean dynamic :)
|
|
| Back to top |
|
 |
|