[-]
Shout:
Click Refresh to load shouts.

Post Reply 
 
Thread Rating:
  • 3 Votes - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculator
04-29-2008, 04:53 AM (This post was last modified: 05-04-2008 02:49 PM by Martijn. Edit Reason: )
Post: #1
Calculator
Hello, thanks for reading.
I am currently new to programming, and I am interested in C++.
I finished a calculator a few days back, and jesper convinced me to post it here so you guys could comment it, and tell me what to improve.

Don't be to harsh on me please! Smile

A screenshot.
[Image: rekenmachiners9.png]
(The idea is, you need to pick your option for what you want to do, and then say the first number you want to for example multiply and the second number it will give the outcome, and repeat the menu (I don't know why it does that, can't seem to fix it)

The code itself (It's called source I believe)

Code:
// Created by Martijn for learning purpose.

#include <cstdlib>
#include <iostream>
#include <windows.h>

using namespace std;

main()
{
   SetConsoleTitle(TEXT("Calculator - Made by Martijn"));
   double num;
   double num2;
   char choice;
   char answer;
   for (;;){
    do {
   cout<<"Choose your option by entering the number.\n";
   cout<<"Use 'q' to quit.\n\n";
  
   // The Menu
   menu:
        cout<<"\n";
   cout<<"1 - Add (+)\n";
   cout<<"2 - Subtract (-)\n";
   cout<<"3 - Multiply (x)\n";
   cout<<"4 - Divide (:)\n";
   cout<<"\n";
   cin>>choice;
   } while ( choice < '1' || choice > '4' && choice != 'q');
   if (choice == 'q') break;
   switch (choice) {
        
         // Add
          case '1':
               cout<<"Enter a number: ";
               cin>>num;
               cout<<"Enter a number to add: ";
               cin>>num2;
               cout<<"Easy! That's: ";
               cout<<num + num2;
               cout<<"\n\n\n";
               cout<<"Calculate again? (Y/N): ";
               cin>> answer;
               if ( answer == 'y') {goto menu;}
               if ( answer == 'n' | answer == 'q') return 0;
               cout<<"\n\n";

           // Subtract
               case '2':
                    cout<<"Enter a number: ";
                    cin>>num;
                    cout<<"Enter a number subtract: ";
                    cin>>num2;
                    cout<<"Ha, I can do that: ";
                    cout<<num - num2;
                    cout<<"\n\n\n";
               cout<<"Calculate again? (Y/N): ";
               cin>> answer;
               if ( answer == 'y') {goto menu;}
               if ( answer == 'n' | answer == 'q') return 0;
               cout<<"\n\n";
                
                //Multiply
               case '3':
                    cout<<"Enter a number: ";
                    cin>>num;
                    cout<<"Enter a number to multiply: ";
                    cin>>num2;
                    cout<<"Easy, that's: ";
                    cout<<num * num2;
                    cout<<"\n\n\n";
               cout<<"Calculate again? (Y/N): ";
               cin>> answer;
               if ( answer == 'y') {goto menu;}
               if ( answer == 'n' | answer == 'q') return 0;
               cout<<"\n\n";
              
               // Divide
               case '4':
                    cout<<"Enter a number: ";
                    cin>>num;
                    cout<<"Enter another number: ";
                    cin>>num2;
                    cout<<"The answer is: ";
                    cout<<num / num2;
                    cout<<"\n\n\n";
               cout<<"Calculate again? (Y/N): ";
               cin>> answer;
               if ( answer == 'y') {goto menu;}
               if ( answer == 'n' | answer == 'q') return 0;
               cout<<"\n\n";

                    
          default:
                   cout<<"That's not an option!\n\n\n";
                  
               }

}
return 0;
}
Features:
- English Version
- (Y/N) function after calculating
- Custom title (not C://~ anymore)

Todo:

- Think of a cool name!
- GUI
-

Known Bugs:

-
-

Thanks to ufo for the correct translation.






Thanks for reading, and I'd would be great if you would tell me what you think about it, maybe a suggestion.

The programmer's national anthem is 'AAAAAAAARRRRGHHHHH!!'.
Find all posts by this user
Quote this message in a reply
04-29-2008, 05:12 AM
Post: #2
Calculator
Code:
int main()
{
   double num;
   double num2;
   char choice;
   [color=red]for (;;){[/color]
(...)
Code:
[color=red]}[/color]
return 0;
}

This causes the calculator to loop all the time.
For(;; ) will basically run until smth tells the program to stop.
If you cut those out it should work once and then close Smile

Quote:GOD is a DJ
Find all posts by this user
Quote this message in a reply
04-29-2008, 05:16 AM
Post: #3
Calculator
Suggestion - Make GUI for it, then also make it in english : D

[Image: image.php?type=sigpic&amp;userid=761...1230799621]
[Image: buttonug.png]
[Image: amini]
Find all posts by this user
Quote this message in a reply
04-29-2008, 05:28 AM
Post: #4
Calculator
That ending thing.. You should create something like that
Code:
bool Closing = false;
Do {
/*
Do the calculation stuff
...
....
....
*/
//User has pressed quit-button
Closing = true
While (!Closing)

So change your for( ; ; ) to something like that Tongue

TibiaAPI Developer
Find all posts by this user
Quote this message in a reply
04-29-2008, 05:28 AM (This post was last modified: 04-29-2008 05:42 AM by Martijn. Edit Reason: )
Post: #5
Calculator
Thanks for the responses both!
Really appreciate it.

Ufo, when I simply remove the lines you mentioned, it gives a error.
'break statement not within loop or switch' in line 22.

Code:
if (choice == 'q') break;

I thought that one was for when you press Q it closes. but when I removed it, it worked, and so did the "Q" function, what is that line for then?

Also, It doesn't repeat the menu any more, but closes the program before you can even read the outcome, but I'll play around with that. (that system pause or something could work.)

Jesper Smile
A gui is a graphical user interface, right?
Never done it before, but I'll try to find some good tutorials on it.
Thanks!

(And yes, I'll do a english version.)
Also, if this is in the wrong section, I am terribly sorry.

Edit: Another responce!
Thanks OsQu!
I'll play around with it, and will update my post, thanks!

Edit2:
Haha, I can't seem to let that script work, it gives me only bugs ^^.
You guys really give me a big smile IRL.

The programmer's national anthem is 'AAAAAAAARRRRGHHHHH!!'.
Find all posts by this user
Quote this message in a reply
04-29-2008, 05:29 AM
Post: #6
Calculator
I think OsQu or someone can move it to Open Source Big Grin

[Image: image.php?type=sigpic&amp;userid=761...1230799621]
[Image: buttonug.png]
[Image: amini]
Find all posts by this user
Quote this message in a reply
04-29-2008, 05:34 AM
Post: #7
Calculator
Code:
if (choice == 'q')
Meh... overlooked this one Tongue
It's meant to quit the for(;; ) loop.
Note it's case sensitive...

Quote:GOD is a DJ
Find all posts by this user
Quote this message in a reply
04-29-2008, 05:47 AM
Post: #8
Calculator
I've managed to stop to let it repeat the menu with using, 'system ("pause");
But the thing is, it says "Press a key to continue" but the "q" function doesn't work then. So you first press 'q' and it continues, it opens the menu again, and then you need to press 'q' again to quit it.

Can I use something like a 'if else' statement?

Code:
system ("pause");
else {
(choice == 'q') break;
}

The programmer's national anthem is 'AAAAAAAARRRRGHHHHH!!'.
Find all posts by this user
Quote this message in a reply
04-29-2008, 06:20 AM
Post: #9
Calculator
I don't think so...
Try moving the `if (choice == 'q')` to the beggining of FOR loop.
So it would look smth like this
Code:
for(;;) {
    if (choice == 'q') break;

Quote:GOD is a DJ
Find all posts by this user
Quote this message in a reply
04-29-2008, 07:07 AM (This post was last modified: 05-01-2008 08:21 AM by Martijn. Edit Reason: )
Post: #10
Calculator
ufo Wrote:I don't think so...
Try moving the `if (choice == 'q')` to the beggining of FOR loop.
So it would look smth like this
Code:
for(;;) {
    if (choice == 'q') break;

That didn't work, still the same.

I've translated it in english now, and looking for some good GUI tutorials atm, but can't find any.

Edit:

Added a (Y/N) function, works great.
Added a custom title, found that script somewhere ^^.
Also a new image.

The programmer's national anthem is 'AAAAAAAARRRRGHHHHH!!'.
Find all posts by this user
Quote this message in a reply
Post Reply 



Contact UsTProgrammingReturn to TopReturn to ContentLite (Archive) ModeRSS Syndication