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

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2009
Posts: 19
Reputation: metalclunch is an unknown quantity at this point 
Solved Threads: 0
metalclunch metalclunch is offline Offline
Newbie Poster

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

 
0
  #1
Jul 21st, 2009
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
  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
  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.
  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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 61
Reputation: AceofSpades19 is on a distinguished road 
Solved Threads: 10
AceofSpades19's Avatar
AceofSpades19 AceofSpades19 is offline Offline
Junior Poster in Training

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

 
0
  #2
Jul 21st, 2009
its because you are missing the while part of a do while loop
do {

} while (some condition);
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 3
Reputation: ALI INAM has a little shameless behaviour in the past 
Solved Threads: 1
ALI INAM ALI INAM is offline Offline
Newbie Poster

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

 
0
  #3
Jul 21st, 2009
  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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

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

 
0
  #4
Jul 21st, 2009
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
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 19
Reputation: metalclunch is an unknown quantity at this point 
Solved Threads: 0
metalclunch metalclunch is offline Offline
Newbie Poster

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

 
0
  #5
Jul 21st, 2009
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.
  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,
  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.
  1. error C2143: syntax error : missing ';' before '{'

If I follow the error, and put it like this,
  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.
  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.
  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...
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 111
Reputation: jesseb07 is on a distinguished road 
Solved Threads: 15
jesseb07's Avatar
jesseb07 jesseb07 is offline Offline
Junior Poster

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

 
0
  #6
Jul 21st, 2009
the syntax for a do-while loop is this:

  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.
Ps. 121

Makes it easier on everyone: http://www.daniweb.com/forums/thread78223.html

AJAX, PHP, C#, C++, JAVA
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 19
Reputation: metalclunch is an unknown quantity at this point 
Solved Threads: 0
metalclunch metalclunch is offline Offline
Newbie Poster

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

 
0
  #7
Jul 21st, 2009
Well, thanks, mate... This is what I got, and it works..
  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.
  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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

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

 
0
  #8
Jul 21st, 2009
Originally Posted by metalclunch View Post
Although, I have to admit... I don't fully understand why do I need this.
  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.
  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.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 19
Reputation: metalclunch is an unknown quantity at this point 
Solved Threads: 0
metalclunch metalclunch is offline Offline
Newbie Poster

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

 
0
  #9
Jul 21st, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 18
Reputation: 23.12.2012 is an unknown quantity at this point 
Solved Threads: 1
23.12.2012's Avatar
23.12.2012 23.12.2012 is offline Offline
Newbie Poster

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

 
1
  #10
Jul 21st, 2009
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 ?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC