-
Hmmmmm someone who has used bloodshed should take over from here lol... I'm not familiar with it in the least. It sounds compiler specific... maybe try making prototypes and then placing all the definitions at the bottom of the code?
Like this:
Code:
#include <iostream>
#include <string>
using namespace std;
void fu();
void bar();
int main()
{
//do stuff
return 0;
}
void fu()
{
//do fu's stuff
}
void bar()
{
//do bar's stuff
}
-
Now it tells me "void '<insert one of the four math functions>()' used prior to declaration". So that didn't work.
-
Lets see your code as it is now....
-
Who knew writing a calculator program could be so tough? :P
Code:
#include <iostream>
#include <string>
using namespace std;
void plus();
void minus();
void multi();
void divi();
void calc() {
string cal;
cout << "Do you want to add, subtract, multiply, or divide?\n";
cin.ignore(80,'\n');
cin >> cal;
if (cal == "add" || "Add") {
plus();
}
else if (cal == "subtract" || "subtract") {
minus();
}
else if (cal == "multiply" || "Multiply") {
multi();
}
else if (cal == "divide" || "Divide") {
divi();
}
}
void again() {
string d;
cin >> d;
if (d == "Y" || "y") {
calc();
}
else {
}
}
int main()
{
string a;
cout << "Do you wish to use the Calculator? Y/N: ";
cin >> a;
if (a == "Y" || "y")
{
calc();
cout << "Would you like to use the calculator again? Y/N: ";
cin >> a;
while (a == "Y" || "y")
{
again();
cout << "Would you like to use the calculator again? Y/N: ";
cin >> a;
}
}
else
{
cin.ignore(80,'\n');
}
return 0;
}
void plus() {
float a;
float b;
float c;
cout << "Input a number: ";
cin >> a;
cout << "Input a second number: ";
cin >> b;
c = a + b;
cout << a << " plus " << b << " equals " << c << ".";
cin.ignore(80,'\n');
cin.get();
}
void minus() {
float a;
float b;
float c;
cout << "Input the number you want to subtract from: ";
cin >> a;
cout << "Input the amout you with to subtract: ";
cin >> b;
c = a - b;
cout << a << " minus " << b << " equals " << c << ".";
cin.ignore(80,'\n');
cin.get();
}
void multi() {
float a;
float b;
float c;
cout << "Input a number: ";
cin >> a;
cout << "Input a second number: ";
cin >> b;
c = a * b;
cout << a << " times " << b << " equals " << c << ".";
cin.ignore(80,'\n');
cin.get();
}
void divi() {
float a;
float b;
float c;
cout << "Input a number: ";
cin >> a;
cout << "Input a second number: ";
cin >> b;
c = a / b;
cout << a << " divided by " << b << " equals " << c << ".";
cin.ignore(80,'\n');
cin.get();
}
-
calc and again need to have prototypes too... not just a couple functions.
-
-
I dont know what to tell you... my code I posted worked on MSVC++ ... and ran just fine except for having to enter y twice in a couple places...
-
I noticed that bug earlier too. =\
EDIT: Fixed that, it had to do with calling cin twice accedentally.
-
Ok, I had the bright idea of assimilating plus() and minus() into multi() and divi(). That is, I now have two functions instead of four: multiplus() and diviminus(). Works just fine now. (will edit later if and when I run up another wall)
-
Ok, I found the next wall. Now, if I input any of my options (add, subtract, multiply, divide), it goes off on this weird loop. First it runs the multiplication part of multiplus(), then the division part of diviminus(), then multiplies again, then divides again, and at the output of the division it prints "I'm sorry, please try again" and "Do you want to add, subtract, multiply, or divide?" all on the same line, looping the entire bug. Also, whether I input Y or N at the very start, it runs the program.
Say nothing about the layout of the code, or what I could do better. I just want to get the bugs fixed first.
Code:
#include <iostream>
#include <string>
using namespace std;
void plus();
void minus();
void multi();
void divi();
void calc();
int main()
{
string a;
cout << "Do you wish to use the Calculator? Y/N: ";
cin >> a;
if (a == "Y" || "Y") {
while (a == "Y" || "y")
{
calc();
cout << "Would you like to use the calculator again? Y/N: ";
cin >> a;
}
}
else if (a == "N" || "n") {
cin.ignore(80,'\n');
return 0;
}
}
void multiplus(string cal) {
float a;
float b;
float c;
if (cal == "multiply" || "Multiply") {
cout << "Input a number: ";
cin >> a;
cout << "Input a second number: ";
cin >> b;
c = a * b;
cout << a << " times " << b << " equals " << c << ".";
cin.ignore(80,'\n');
cin.get();
}
else if (cal == "add" || "Add") {
cout << "Input a number: ";
cin >> a;
cout << "Input a second number: ";
cin >> b;
c = a + b;
cout << a << " plus " << b << " equals " << c << ".";
cin.ignore(80,'\n');
cin.get();
}
else {
}
}
void diviminus(string cal) {
float a;
float b;
float c;
if (cal == "divide" || "Divide") {
cout << "Input a number: ";
cin >> a;
cout << "Input a second number: ";
cin >> b;
c = a / b;
cout << a << " divided by " << b << " equals " << c << ".";
cin.get();
}
else if (cal == "subtract" || "Subtract") {
cout << "Input the number you want to subtract from: ";
cin >> a;
cout << "Input the amout you with to subtract: ";
cin >> b;
c = a - b;
cout << a << " minus " << b << " equals " << c << ".";
cin.ignore(80,'\n');
cin.get();
}
else {
}
}
void calc() {
string cal;
int cas;
cout << "Do you want to add, subtract, multiply, or divide?\n";
cin.ignore(80,'\n');
cin >> cal;
if (cal == "add" || "Add") {
cas = 1;
}
else if (cal == "subtract" || "Subtract") {
cas = 2;
}
else if (cal == "multiply" || "Multiply") {
cas = 3;
}
else if (cal == "divide" || "Divide") {
cas = 4;
}
else {
}
switch (cas) {
case 1:
multiplus(cal);
case 2:
diviminus(cal);
case 3:
multiplus(cal);
case 4:
diviminus(cal);
default:
cout << "I'm sorry, please try again.";
calc();
}
}
-
I see one problem you didn't prototype your new functions.... In fact all the old prototypes are still there even though the functions aren't. I'll toss that code in my compiler and see what needs to be fixed.
-
In you main you are using
Code:
if (a == "Y" || "Y")
this is always true because "Y" is a non zero number when converted to an int which when converted to bool is true( x != 0 is always true) and I believe you wanted "y" there and not "Y".
In fact I think that is your main problem you forgot to put a == or cal == in most if not all of your if statements. That should help you some. When all else fails step through your program with the debugger it is useful.
-
DOH!! :P I completely forgot! That's definitly a downside of prototypes... And I fixed an error in my main() function's "if" arguments, it had "Y" || "Y" instead of "Y" || "y". Wandering through this mess, fixing some minor bugs...
EDIT: Still the same major bug, though. :(
-
You need to make it
Code:
if(a == "Y" || a == "y")
-
I don't really know how to use Dev-C++'s debugger... :( Otherwise I'll fix those if statements. Oops.
The "Y" || "y" worked fine when I had only the plus() function.