-
September 27th, 2010, 01:47 AM
#1
[RESOLVED] Why won't this program I made work ?
I am trying to make a simple calculator program, it's not finished yet, but when i try to "compile and run" it in Dev C++, it says there is 39 errors! How can there be thirty nine??
So yeah please help me out by reading through this code and telling me what you think could be the problems. The code looks good to me, but I'm only pretty new to C++...
#include <iostream.h>
#include <cmath>
void normalfun(double);
void specialfun(float);
void add(float);
void subtract(float);
void multiply(float);
void divide(float);
/*
Name: Simple Calculator
Copyright: 2010
Author: @@@@@ @@@@@@@@@
Date: 27/09/10 15:18
Description: simple application to perform basic calculations
*/
int main() // main function, calls other functions on request
{
using namespace std;
cout << "Simple Calculator. Written by @@@@@ @@@@@@@@@\n\n";
cout << "\tType 0 for normal functions";
cout << "\n\yType 1 for special functions\n";
double decision;
cin >> decision; // creates the variable for users input
if (decision == 0)
{
normalfun(double decision2)
else specialfun(float)
}
system("pause");
return 0;
}
void normalfun(double decision2)
{
using namespace std;
cout << "\n\tType 1 for addition,";
cout << "\n\tType 2 for subtraction,";
cout << "\n\tType 3 for multiplication,";
cout << "\n\tType 4 for division\n\n";
cin >> decision2;
{
if (decision2 == 1);
add();
if (decision2 == 2);
subtract();
if (decision2 == 3);
multiply();
if (decision2 == 4);
divide();
}
}
void specialfun(float) // special function
{
using namespace std;
cout << "This is special function ";
}
void add(float add1, add2, addtotal) // addition function
{
using namespace std;
cout << "Enter two numbers ";
cout << "\nFirst one: \t";
cin >> add1;
cout << "\nSecond one: \t";
cin >> add2;
addtotal = add1 + add2;
cout << "\n ---------------------------------------------- " << "\n \t" << add1 << " add " << add2 << " equals " << addtotal << " .";
}
void subtract(float sub1, sub2, subtotal) // subtraction function
{
using namespace std;
cout << "Enter two numbers ";
cout << "\nFirst one: \t";
cin >> sub1;
cout << "\nSecond one: \t";
cin >> sub2;
subtotal = sub1 - sub2;
cout << "\n ---------------------------------------------- " << "\n \t" << sub1 << " subtract " << sub2 << " equals " << subtotal << " .";
}
void multiply(float mult1, mult2, multtotal) // multiplication function
{
using namespace std;
cout << "Enter two numbers ";
cout << "\nFirst one: \t";
cin >> mult1;
cout << "\nSecond one: \t";
cin >> mult2;
multtotal = mult1 * mult2;
cout << "\n --------------------------------------------- " << "\n \t" << mult1 << " multiplied by " << mult2 << " equals " << multtotal << " .";
}
void divide(float div1, div2, divtotal) // division function
{
using namespace std;
cout << "Enter two numbers ";
cout << "\nFirst one: \t";
cin >> div1;
cout << "\nSecond one: \t";
cin >> div2;
divtotal = div1 / div2;
cout << "\n ---------------------------------------------- " << "\n \t" << div1 << " divided by " << div2 << " equals " << divtotal << " .";
}
Thanks
Last edited by Brad Jones; August 13th, 2012 at 10:24 AM.
-
September 27th, 2010, 03:32 AM
#2
Re: Why won't this program I made work ?
Originally Posted by dylanm_36
Code:
#include <iostream.h>
In before Paul : iostream.h is not a standard header. You have to include <iostream>.
Also, please post your code between [CODE]code[/CODE] tags.
What errors are you getting? Concentrate only on the very first one. 99% percent of the time, the first errors screws with the compiler, who doesn't understand the rest of the code, and generates more errors.
If you can fix the errors 1 by 1, starting with the first, you should be fine. I bet there isn't more than a handful.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
September 27th, 2010, 07:23 AM
#3
Re: Why won't this program I made work ?
You should try reading the errors. The compiler will tell you what's wrong.
You have two big problems that I see. You're not declaring and calling your functions properly, and you're terminating your if statements with a ;
-
September 27th, 2010, 08:43 AM
#4
Re: Why won't this program I made work ?
Code:
if (decision == 0)
{
normalfun(double decision2)
else specialfun(float)
}
You are also missing your ; here..
Are you able to put "using namespace std;" in a function?.. I have never tried..
-
September 27th, 2010, 08:44 AM
#5
Re: Why won't this program I made work ?
Originally Posted by jnmacd
Code:
if (decision == 0)
{
normalfun(double decision2)
else specialfun(float)
}
You are also missing your ; here..
That's only one of several problems there..
-
September 27th, 2010, 08:53 AM
#6
Re: Why won't this program I made work ?
Originally Posted by dylanm_36
So yeah please help me out by reading through this code and telling me what you think could be the problems. The code looks good to me, but I'm only pretty new to C++...
That's not how to program; Produce a heap of non-working code and then let someone else sort it out.
Instead use stepwise refinement. Start with a small working program. Then add stuff in small increments and make sure each addition works before you continue. You'll have a working program from start to finish.
So start over and proceed as I described.
-
September 27th, 2010, 11:11 AM
#7
Re: Why won't this program I made work ?
Originally Posted by GCDEF
That's only one of several problems there..
Oops! Didn't even look at what he was doing.
So yeah there probably are over 30 errors here....
And nuzzle is right. Start smaller. It seems like you coded all of this THEN tried to compile and was surprised it didn't work!
-
September 28th, 2010, 07:06 AM
#8
Re: Why won't this program I made work ?
To add to above comments:
Your first action should be to make a correct intendation. Then start with main function to make it compile
Code:
int main() // main function, calls other functions on request
{
using namespace std;
cout << "Simple Calculator. Written by @@@@@ @@@@@@@@@\n\n";
cout << "\tType 0 for normal functions";
cout << "\n\yType 1 for special functions\n";
double decision;
cin >> decision; // creates the variable for users input
if (decision == 0)
{
normalfun(double decision2)
else specialfun(float)
}
system("pause");
return 0;
}
The trouble starts with 'if (decision == 0)':
'decision' is of type double. Therefore it should be compared with 0. (though the compiler would correct that for you) and in case of 0 it should work. Generally, a double variable should not be compared with a double literal cause a double might have different internal representations for the same decimal, e. g. 3.0 internally may be 2.99999999999999999999_ or 3.0000000000000000000000_. If you compare the first with 3.0 the comparision will not return equality.
Your solution is easier. You simply turn the 'double decision' to 'int decision' and are done.
After the if statement you open a curly bracket which opens a new if block. But that block isn't closed before the else. Generally, it is
Code:
if (<condition>)
{
<statements>
}
else
{
<statements>
}
So, you need either two blocks with curly brackets or you omit the curly brackets at all what is possible if it is only one statement. I would recommend to always use brackets especially if you were not able to make a correct intendation (as in the code you posted).
Next is that the calls of normalfun and specialfun have wrong syntax. In a call the argument types are not repeated but you pass only the arguments.
Code:
normalfun(decision);
And of course you need to pass variables defined in the calling function and not those which were only prototypes in the called function. So, you have to pass 'decision' and not 'decision2'.
In your case it is unclear why you were passing an argument at all. If you look at normalfun implemention you see that t he argument passed wasn't used but overwritten by a new user input. Same applies for specialfun whcih for any reason has a float argument and actually should have no argument.
Last edited by Brad Jones; August 13th, 2012 at 10:23 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|