|
Hello TeamLiqiud! I hate to make a topic over such a trivial topic, but I need help and noone else I know has any idea how to help me. So I'm having difficulty with a program (I'm newb) I've been working on, trying to make better and better (a calculator program). Recently I made an improvement, but after I changed it I'm getting an error I've never had before, it says "expected init- declarator before "using"". I took off the improvements and I still get the error, so I'm thinking maybe it could be a bug with the compiler? I'm not sure. Here's the main page, it's the only one with the error right now, and the problem is line 4: //program that acts as a basic calculator #include <iostream> #include "math.h" using namespace std;
int main() { for (;;) {cout << "hello, this is the calculator!" << endl; cout << "chose your operation with a number: 1 for addition, 2 for subra" << "ction, 3 for multiplication, and 4 for division, and 5 to square. To quit, press any letter." << endl << endl; int s; //s is variable for selection, is an interger cin >> s; if (!cin.good()) { cout << "Your input is invalid and has terminated the program." << endl; break; } if (s == 1) { cout << "choose your first and second number." << endl; int a; int b; cin >> a; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cin >> b; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cout << "your solution is " << add (a, b) << endl << endl; continue; } if (s == 2) { cout << "choose your first and second number." << endl; int a; int b; cin >> a; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cin >> b; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cout << "your solution is " << subtract (a, b) << endl << endl; continue; } if (s == 3) { cout << "choose your first and second number." << endl; int a; int b; cin >> a; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cin >> b; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cout << "your solution is " << multiply (a, b) << endl << endl; continue; } if (s == 4) { cout << "choose your first and second number." << endl; float a; float b; cin >> a; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cin >> b; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cout << "your solution is " << divide (a, b) << endl << endl; continue; } if (s==5) { cout << "Choose the number to be squared." << endl; int a; cin >> a; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cout << "your solution is " << square (a) << endl << endl; continue; } else { cout << "Thats not a valid operation." << endl << endl; continue; }; system("PAUSE"); } system("PAUSE"); return (0); }
I've been coding for about a month, so please don't laugh at me Thanks for any help!
|
What a fun coincidence that the ; ) in the for loop makes a XD
|
Germany2896 Posts
|
I posted your code in VS2008 and compiles fine but i did have to take out the lines where you display the answers as add, divide and what not are not defined and from what i've seen, you didn't seem to define them either. Is there stuff you're not showing us?
What compiler are you using?
|
Is "math.h" a code file that you wrote? I'm guessing so, and my guess is that that file is the source of your errors.
|
On February 06 2010 05:45 b3h47pte wrote: I posted your code in VS2008 and compiles fine but i did have to take out the lines where you display the answers as add, divide and what not are not defined and from what i've seen, you didn't seem to define them either. Is there stuff you're not showing us?
What compiler are you using? I'm using an include file, just playing with headers. Thanks! I had no idea about the code tag, sorry. Should I add the include files or just repost with the functions defined in the same program?
|
Just post the header file.
|
On February 06 2010 05:48 Alphonsse wrote: Is "math.h" a code file that you wrote? I'm guessing so, and my guess is that that file is the source of your errors. .... I'm really bad at making blogs. After seriously an hour and 15 minutes of checking {s and }s, semicolons and shit, I find that I left a semicolon out of my header file.
Sorry for the shitty fail blogs, but it helped.
|
Np, just remember that whenever you see an error on a line of code that immediately follows a #include statement, that generally means the error is in the file that you included.
|
|
Yeah I was going to say, isn't there already a math.h? At least way back when I was learning to code there was...
|
what you could do to improve readability:
- better indentation - instead of if (s == 1), if (s == 2), etc use the switch statement - put the code for your operations into seperate functions and call them from the switch statement
apart from that, good work, keep on going
|
holy crap thats some MOTHAFKING UGLY indenting style I cleaned it up a bit and added some stuff so it'll compile but there's still a lot of redundancy and bad coding style
I don't get any warnings or errors from VS2008 i'll look at gcc later
#include <iostream>
using namespace std;
int add(int a, int b) { return a + b; } int subtract(int a, int b) { return a - b; } int multiply(int a, int b) { return a * b; } float divide(float a, float b) { return a/b; } int square(int a) { return a * a; }
int main() { for (;;) { cout << "Hello, this is the calculator!" << endl; cout << "Choose your operation with a number:" << "1 for addition, 2 for subraction, 3 for multiplication, and 4 for division, and 5 to square." << "To quit, input any letter." << endl << endl; int s; // "s" is variable for selection cin >> s; if (!cin.good()) { cout << "Your input is invalid and the program will be terminated." << endl; break; } if (s == 1) { cout << "choose your first and second number." << endl; int a; int b; cin >> a; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cin >> b; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cout << "your solution is " << add (a, b) << endl << endl; continue; } if (s == 2) { cout << "choose your first and second number." << endl; int a; int b; cin >> a; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cin >> b; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cout << "your solution is " << subtract (a, b) << endl << endl; continue; } if (s == 3) { cout << "choose your first and second number." << endl; int a; int b; cin >> a; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cin >> b; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cout << "your solution is " << multiply (a, b) << endl << endl; continue; } if (s == 4) { cout << "choose your first and second number." << endl; float a; float b; // INPUT A cin >> a; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } // INPUT B cin >> b; if (!cin.good()) { cout << "Your input is invalid." << endl; break; }
if (b == 0) { cout << "YOU CAN'T DIVIDE BY 0." << endl; break; }
cout << "Your solution is " << divide (a, b) << endl << endl; continue; } if (s == 5) { cout << "Choose the number to be squared." << endl; int a; cin >> a; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cout << "your solution is " << square (a) << endl << endl; continue; } else { cout << "Thats not a valid selection." << endl << endl; continue; } system("PAUSE"); } system("PAUSE"); return (0); }
|
I'm not taking an incredibly detailed look, but just eyeballing it, one suggestion (that matters very little) is to throw in a function for
if (!cin.good()) { cout << "Your input is invalid." << endl; break; }
Might as well make that into a function and just call it every time to make things more readable? I haven't programmed in forever though, so maybe that's not a very good idea ._.; Just a thought.
|
+ Show Spoiler +
#include <iostream>
using namespace std;
int add(int a, int b) { return a + b; } int subtract(int a, int b) { return a - b; } int multiply(int a, int b) { return a * b; } float divide(float a, float b) { return a/b; } int square(int a) { return a * a; } char temp;
int main() { for (;;) { cout << "Hello, this is the calculator!" << endl; cout << "Choose your operation with a number:\n" << "1 for addition\n2 for subraction\n3 for multiplication\n4 for division\n5 to square.\n\n" << "To quit, input any letter." << endl << endl; int s; // "s" is variable for selection cin >> s; if (!cin.good()) { cout << "Your input is invalid and the program will be terminated." << endl; break; } if (s == 1) { cout << "Choose your first and second number." << endl; int a; int b; cin >> a; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cin >> b; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cout << "Your solution is " << add (a, b) << endl << endl; continue; } if (s == 2) { cout << "Choose your first and second number." << endl; int a; int b; cin >> a; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cin >> b; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cout << "Your solution is " << subtract (a, b) << endl << endl; continue; } if (s == 3) { cout << "Choose your first and second number." << endl; int a; int b; cin >> a; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cin >> b; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cout << "Your solution is " << multiply (a, b) << endl << endl; continue; } if (s == 4) { cout << "choose your first and second number." << endl; float a; float b; // INPUT A cin >> a; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } // INPUT B cin >> b; if (!cin.good()) { cout << "Your input is invalid." << endl; break; }
if (b == 0) { cout << "YOU CAN'T DIVIDE BY 0." << endl; break; }
cout << "Your solution is " << divide (a, b) << endl << endl; continue; } if (s == 5) { cout << "Choose the number to be squared." << endl; int a; cin >> a; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cout << "Your solution is " << square (a) << endl << endl; continue; } else { cout << "Thats not a valid selection." << endl << endl; continue; } cin >> temp; } cin >> temp; return (0); }
i dunno that much about programming, just made the output look nicer
no errors in dev c++
edit: oooohhhhh
you should tell the user that it crashes if you don't use integers
|
On February 06 2010 06:45 jalstar wrote:+ Show Spoiler +
#include <iostream>
using namespace std;
int add(int a, int b) { return a + b; } int subtract(int a, int b) { return a - b; } int multiply(int a, int b) { return a * b; } float divide(float a, float b) { return a/b; } int square(int a) { return a * a; } char temp;
int main() { for (; { cout << "Hello, this is the calculator!" << endl; cout << "Choose your operation with a number:\n" << "1 for addition\n2 for subraction\n3 for multiplication\n4 for division\n5 to square.\n\n" << "To quit, input any letter." << endl << endl; int s; // "s" is variable for selection cin >> s; if (!cin.good()) { cout << "Your input is invalid and the program will be terminated." << endl; break; } if (s == 1) { cout << "Choose your first and second number." << endl; int a; int b; cin >> a; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cin >> b; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cout << "Your solution is " << add (a, b) << endl << endl; continue; } if (s == 2) { cout << "Choose your first and second number." << endl; int a; int b; cin >> a; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cin >> b; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cout << "Your solution is " << subtract (a, b) << endl << endl; continue; } if (s == 3) { cout << "Choose your first and second number." << endl; int a; int b; cin >> a; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cin >> b; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cout << "Your solution is " << multiply (a, b) << endl << endl; continue; } if (s == 4) { cout << "choose your first and second number." << endl; float a; float b; // INPUT A cin >> a; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } // INPUT B cin >> b; if (!cin.good()) { cout << "Your input is invalid." << endl; break; }
if (b == 0) { cout << "YOU CAN'T DIVIDE BY 0." << endl; break; }
cout << "Your solution is " << divide (a, b) << endl << endl; continue; } if (s == 5) { cout << "Choose the number to be squared." << endl; int a; cin >> a; if (!cin.good()) { cout << "Your input is invalid." << endl; break; } cout << "Your solution is " << square (a) << endl << endl; continue; } else { cout << "Thats not a valid selection." << endl << endl; continue; } cin >> temp; } cin >> temp; return (0); }
i dunno that much about programming, just made the output look nicer no errors in dev c++ edit: oooohhhhh you should tell the user that it crashes if you don't use integers
or just verify the input values to make sure they're integers...
edit.. btw i just looked at your code and its got some serious issues. dont use continue statements ever. just use a big switch statement... like switch(s) case 1: code; break;
etc etc. makes it easier to read and doesn't violate one of the basic programming principles that you should be learning if you're in a class. this also lets you verify input once and make sure everything is good (you'll still need to cover the divide by zero case in the division case but it's better than verifying the same input 6? times)
|
|
|
|