943,929 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 919
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 21st, 2009
0

C++ - Need help solving a problem with my Calculator Script.

Expand Post »
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!

main_calculator.cpp
c++ Syntax (Toggle Plain Text)
  1. //Calculator Script by Sass.
  2. //main_calculator.cpp
  3.  
  4. //Default include's.
  5. #include <iostream>
  6. #include <windows.h>
  7. using namespace std;
  8.  
  9. //Custom header file.
  10. #include "header_calculator.h"
  11.  
  12. //Main()
  13. int main()
  14. {
  15. //One-time welcome greeting:
  16. SetConsoleTitle("Calculator v1.1");
  17. cout << "Kalkulaator v1.1 by MetalCrunch." << endl;
  18. cout << "---------------------------------------" << endl << endl;
  19.  
  20. //Main program:
  21. for (;;) {
  22. do {
  23. cout << "1 - Addition." << endl;
  24. cout << "2 - Subtraction." << endl;
  25. cout << "3 - Multiplication." << endl;
  26. cout << "4 - Division." << endl;
  27. cout << "5 - Abimees." << endl;
  28. cin >> choice; }
  29.  
  30. switch (choice) {
  31. //Addition.
  32. case 1:
  33. cout << "Enter the first number: ";
  34. cin >> num1;
  35. cout << "\nEnter the second number: ";
  36. cin >> num2;
  37. result = num1 + num2;
  38. cout << "Answer: ";
  39. cout << result;
  40. break;
  41.  
  42. //Subtraction.
  43. case 2:
  44. cout << "Enter the first number: ";
  45. cin >> num1;
  46. cout << "\nEnter the second number: ";
  47. cin >> num2;
  48. result = num1 - num2;
  49. cout << "\nAnswer: ";
  50. cout << result;
  51. break;
  52.  
  53. //Multiplication.
  54. case 3:
  55. cout << "Enter the first number: ";
  56. cin >> num1;
  57. cout << "\nEnter the second number: ";
  58. cin >> num2;
  59. result = num1 * num2;
  60. cout << "\nAnswer: ";
  61. cout << result;
  62. break;
  63.  
  64. //Division.
  65. case 4:
  66. cout << "Enter the first number: ";
  67. cin >> num1;
  68. cout << "\n Enter the second number: ";
  69. cin >> num2;
  70. result = num1 / num2;
  71. cout << "\nAnswer: ";
  72. cout << result;
  73. break;
  74.  
  75. //"Information".
  76. case 5:
  77. cout << "Calculator v1.1 by Metalcrunch" << endl;
  78. cout << "Enter the number 1-5, then answer the questions..." << endl;
  79. cout << "The program will solve it all." << endl;
  80. cout << "But please don't enter a value over 9 numbers, the program will crash!" << endl;
  81. break;
  82.  
  83. default:
  84. cout << "test";
  85. break; }
  86. }
  87. return 0;
  88. }

header_calculator.h
c++ Syntax (Toggle Plain Text)
  1. //Calculator Script
  2. //header_calculator.h
  3.  
  4. #ifndef HEADER_CALCULATOR_H
  5. #define HEADER_CALCULATOR_H
  6.  
  7. //Main int's, for calculation.
  8. int num1, num2;
  9. int result;
  10.  
  11. //The "choice" int.
  12. int choice;
  13.  
  14. #endif

The build log.
C++ Syntax (Toggle Plain Text)
  1. Compiling...
  2. main_calculator.cpp
  3. c:\documents and settings\*********\my documents\visual studio 2008\projects\project1\calculator\calculator\main_calculator.cpp(31) : error C2059: syntax error : 'switch'
  4. c:\documents and settings\*********\my documents\visual studio 2008\projects\project1\calculator\calculator\main_calculator.cpp(31) : error C2143: syntax error : missing ';' before '{'
  5. c:\documents and settings\*********\my documents\visual studio 2008\projects\project1\calculator\calculator\main_calculator.cpp(33) : error C2046: illegal case
  6. c:\documents and settings\*********\my documents\visual studio 2008\projects\project1\calculator\calculator\main_calculator.cpp(44) : error C2046: illegal case
  7. c:\documents and settings\*********\my documents\visual studio 2008\projects\project1\calculator\calculator\main_calculator.cpp(55) : error C2046: illegal case
  8. c:\documents and settings\*********\my documents\visual studio 2008\projects\project1\calculator\calculator\main_calculator.cpp(66) : error C2046: illegal case
  9. c:\documents and settings\*********\my documents\visual studio 2008\projects\project1\calculator\calculator\main_calculator.cpp(77) : error C2046: illegal case
  10. 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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
metalclunch is offline Offline
21 posts
since Jul 2009
Jul 21st, 2009
0

Re: C++ - Need help solving a problem with my Calculator Script.

its because you are missing the while part of a do while loop
do {

} while (some condition);
Reputation Points: 61
Solved Threads: 10
Junior Poster in Training
AceofSpades19 is offline Offline
61 posts
since Jun 2008
Jul 21st, 2009
0

Re: C++ - Need help solving a problem with my Calculator Script.

C++ Syntax (Toggle Plain Text)
  1.  
  2. #include <iostream>
  3. double a = 0;
  4. double b = 0;
  5. double c = 0;
  6. double d = 0;
  7. double add = 0;
  8. double sub = 0;
  9. double mul = 0;
  10. double f = 0;
  11. int main () {
  12. std::cout << "Enter any one of the following " << std::endl;
  13. std::cout << "1 for ADDITION " << std::endl;
  14. std::cout << "2 for SUBTRACTOIN " << std::endl;
  15. std::cout << "3 for MULTIPLICATOIN " << std::endl;
  16. std::cout << "4 for DIVISION " << std::endl;
  17. std::cin >> a;
  18. std::cout << ' ' << std::endl;
  19. std::cout << "Enter first number: " <<std::endl;
  20. std::cin >> b;
  21. std::cout<< ' ' << std::endl;
  22. std::cout << "Enter second number: " << std::endl;
  23. std::cin >> c;
  24. std::cout<< ' ' << std::endl;
  25. if ( a == 1) {
  26. add = b + c;
  27. std::cout << add << std::endl;
  28. }else if ( a == 2){
  29. sub = b - c;
  30. std::cout << sub << std::endl;
  31. }else if (a == 3) {
  32. mul = b * c;
  33. std::cout << mul << std::endl;
  34. }else if (a == 4){
  35. f = b / c;
  36. std::cout << f << std::endl;
  37.  
  38. }
  39.  
  40.  
  41.  
  42. }
well here is simple solution to your very simple project
Last edited by Ancient Dragon; Jul 21st, 2009 at 10:47 am. Reason: correct code tags
Reputation Points: 2
Solved Threads: 1
Newbie Poster
ALI INAM is offline Offline
3 posts
since Jul 2009
Jul 21st, 2009
0

Re: C++ - Need help solving a problem with my Calculator Script.

ALI INAM, you should urgently stop posting such rubbish solutions, BTW: why making every variable global?
Ever heard of local variables?

Do you really think the OP will learn from such solutions?
By just giving code to the OP, you won't help him, remember that.
It's against Daniweb's homework policy.

Links for (I hope) a permanent reminder:
http://www.daniweb.com/forums/announcement118-2.html
http://www.daniweb.com/forums/announcement118-3.html
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jul 21st, 2009
0

Re: C++ - Need help solving a problem with my Calculator Script.

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.
c++ Syntax (Toggle Plain Text)
  1. //Main program:
  2. for (;;) {
  3. do {
  4. cout << "1 - Addition." << endl;
  5. cout << "2 - Subtraction." << endl;
  6. cout << "3 - Multiplication." << endl;
  7. cout << "4 - Division." << endl;
  8. cout << "5 - Abimees." << endl;
  9. cin >> choice; }
  10.  
  11. switch (choice) {
  12. //Addition.

Now, that was the default Main Code. If I add the "while" command to it like this,
c++ Syntax (Toggle Plain Text)
  1. //Main program:
  2. for (;;) {
  3. do {
  4. cout << "1 - Addition." << endl;
  5. cout << "2 - Subtraction." << endl;
  6. cout << "3 - Multiplication." << endl;
  7. cout << "4 - Division." << endl;
  8. cout << "5 - Abimees." << endl;
  9. cin >> choice; }
  10. while (choice < 1 || choice > 5) {
  11. break; }
  12.  
  13. switch (choice) {
  14. //Addition.
Then it will get me this error.
C++ Syntax (Toggle Plain Text)
  1. error C2143: syntax error : missing ';' before '{'

If I follow the error, and put it like this,
c++ Syntax (Toggle Plain Text)
  1. //Main program:
  2. for (;;) {
  3. do {
  4. cout << "1 - Addition." << endl;
  5. cout << "2 - Subtraction." << endl;
  6. cout << "3 - Multiplication." << endl;
  7. cout << "4 - Division." << endl;
  8. cout << "5 - Abimees." << endl;
  9. cin >> choice; }
  10. while (choice < 1 || choice > 5); {
  11. break; }
  12.  
  13. switch (choice) {
  14. //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.
c++ Syntax (Toggle Plain Text)
  1. //Main program:
  2. for (;;) {
  3. do {
  4. cout << "1 - Addition." << endl;
  5. cout << "2 - Subtraction." << endl;
  6. cout << "3 - Multiplication." << endl;
  7. cout << "4 - Division." << endl;
  8. cout << "5 - Abimees." << endl;
  9. cin >> choice; }
  10. while (choice < 1 || choice > 5)
  11. break;
  12.  
  13. switch (choice) {
  14. //Addition.
However, that didn't help much, because I get this error.
C++ Syntax (Toggle Plain Text)
  1. 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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
metalclunch is offline Offline
21 posts
since Jul 2009
Jul 21st, 2009
0

Re: C++ - Need help solving a problem with my Calculator Script.

the syntax for a do-while loop is this:

c++ Syntax (Toggle Plain Text)
  1. do
  2. {
  3. //stuff to loop through
  4. }
  5. 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
Last edited by jesseb07; Jul 21st, 2009 at 9:03 am.
Reputation Points: 76
Solved Threads: 15
Junior Poster
jesseb07 is offline Offline
111 posts
since Dec 2006
Jul 21st, 2009
0

Re: C++ - Need help solving a problem with my Calculator Script.

Well, thanks, mate... This is what I got, and it works..
c++ Syntax (Toggle Plain Text)
  1. //Main program:
  2. for (;;) {
  3. do {
  4. cout << "1 - Addition." << endl;
  5. cout << "2 - Subtraction." << endl;
  6. cout << "3 - Multiplication." << endl;
  7. cout << "4 - Divide." << endl;
  8. cout << "5 - Help." << endl;
  9. cin >> choice; }
  10. while (choice < 1 || choice > 5);
  11.  
  12. switch (choice) {
  13. default:
  14. cout << "If you see me, report the 'default' bug!";
  15.  
  16. //Liitmine.

Although, I have to admit... I don't fully understand why do I need this.
c++ Syntax (Toggle Plain Text)
  1. 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
Last edited by metalclunch; Jul 21st, 2009 at 9:52 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
metalclunch is offline Offline
21 posts
since Jul 2009
Jul 21st, 2009
0

Re: C++ - Need help solving a problem with my Calculator Script.

Although, I have to admit... I don't fully understand why do I need this.
c++ Syntax (Toggle Plain Text)
  1. 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.
c++ Syntax (Toggle Plain Text)
  1. 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 && 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).
Last edited by tux4life; Jul 21st, 2009 at 9:59 am.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jul 21st, 2009
0

Re: C++ - Need help solving a problem with my Calculator Script.

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
metalclunch is offline Offline
21 posts
since Jul 2009
Jul 21st, 2009
1

Re: C++ - Need help solving a problem with my Calculator Script.

Quote ...
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 ?
Reputation Points: 32
Solved Threads: 1
Newbie Poster
23.12.2012 is offline Offline
18 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: AfxBeginThread Memory Leak?
Next Thread in C++ Forum Timeline: I really don't know how to make programs....please Solve this for me





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC