-
Switches
Hey
Im' trying to make a thingy where you can add, subtract, devide and stuff using the switch statements.
In the middle of making it the thing wont compile. Something about 'jump to'.......
i dunno.
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
int main ()
{
cout << "This program can do simple calculations for you. ";
cout << "\nChoose which you would like to do: ";
cout << "\n\nA. add";
cout << "\nB. subtract";
cout << "\nC. multiply";
cout << "\nD. devide";
cout << "\n\n\n Your choice: ";
char choice;
cin >> choice;
switch (choice)
{
case 'a':
case 'A':
int num, num2;
cout << "Ok you chose A";
cout << "\nEnter two numbers to add:";
cout << "\n First: ";
cin >> num;
cout << "\n Second: ";
cin >> num2;
int total = (num + num2);
cout << "\n Your total is: ";
cout << total;
cout << "\n\n";
break;
case 'b':
case 'B':
cout << "Ok you chose B";
cout << "\nEnter two numbers to subtract:";
break;
case 'c':
case 'C':
cout << "Ok you chose C";
cout << "\nEnter two numbers to multiply:";
break;
case 'd':
case 'D':
cout << "Ok you chose D";
cout << "\nEnter two numbers to devide:";
break;
}
system ("pause");
return 0;
}
-
My guess is that the following line causes this:
int total = (num + num2);
When you define variables and objects within a case block, always enclose the entire block with {} or better yet, call a function that does what is necessary. This will make your code look more structured and readable.
Danny Kalev
-
Ok i tried using a function. And the case thing.
but it doesnt work
int add (num, num2);
int sub (num, num2);
int mult (num, num2);
int div (num, num2);
int main ()
{
cout << "This program can do simple calculations for you. ";
cout << "\nChoose which you would like to do: ";
cout << "\n\nA. add";
cout << "\nB. subtract";
cout << "\nC. multiply";
cout << "\nD. devide";
cout << "\n\n\n Your choice: ";
char choice;
cin >> choice;
switch (choice)
{
case 'a':
case 'A':
{
int num, num2;
cout << "Ok you chose A";
cout << "\nEnter two numbers to add:";
cout << "\n First: ";
cin >> num;
cout << "\n Second: ";
cin >> num2;
total = add (num, num2);
cout << "\n Your total is: ";
cout << total;
cout << "\n\n";
break;
}
case 'b':
case 'B':
{
cout << "Ok you chose B";
cout << "\nEnter two numbers to subtract:";
break;
}
case 'c':
case 'C':
{
cout << "Ok you chose C";
cout << "\nEnter two numbers to multiply:";
break;
}
case 'd':
case 'D':
{
cout << "Ok you chose D";
cout << "\nEnter two numbers to devide:";
break;
}
}
system ("pause");
return 0;
}
add (num, num2)
{
total = num + num2;
return total;
}
Last edited by KirBy07; 08-01-2004 at 02:51 AM.
-
where is the definition of total? and what's the exact error message you're getting?
Danny Kalev
-
I get errors like
redefinition
syntax
initializer
ISO C++
i dunno wat it is...
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
int add (num, num2);
int sub (num, num2);
int mult (num, num2);
int div (num, num2);
int total;
int main ()
{
cout << "This program can do simple calculations for you. ";
cout << "\nChoose which you would like to do: ";
cout << "\n\nA. add";
cout << "\nB. subtract";
cout << "\nC. multiply";
cout << "\nD. devide";
cout << "\n\n\n Your choice: ";
char choice;
cin >> choice;
switch (choice)
{
case 'a':
case 'A':
{
int num, num2;
cout << "Ok you chose A";
cout << "\nEnter two numbers to add:";
cout << "\n First: ";
cin >> num;
cout << "\n Second: ";
cin >> num2;
total = add (num, num2);
cout << "\n Your total is: ";
cout << total;
cout << "\n\n";
break;
}
case 'b':
case 'B':
{
cout << "Ok you chose B";
cout << "\nEnter two numbers to subtract:";
cout << "\n First: ";
cin >> num;
cout << "\n Second: ";
cin >> num2;
total = sub (num, num2);
cout << "\n Your total is: ";
cout << total;
cout << "\n\n";
break;
}
case 'c':
case 'C':
{
cout << "Ok you chose C";
cout << "\nEnter two numbers to multiply:";
break;
}
case 'd':
case 'D':
{
cout << "Ok you chose D";
cout << "\nEnter two numbers to devide:";
break;
}
}
system ("pause");
return 0;
}
add (num, num2)
{
total = num + num2;
return total;
}
sub (num, num2)
{
total = num + num2;
}
mult (num, num2)
{
total = num * num2;
}
div (num, num2)
{
total = num / num2;
}
-
The problem lies in the declarations of yoru functions:
int add (num, num2);
int sub (num, num2);
int mult (num, num2);
int div (num, num2);
This style is obsolete. Change it to:
int add (int num, int num2);
int sub (int num, int num2);
and so on...
Danny Kalev
-
ok i did that stuff
am i not calling the functions correctly ?
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
int add (int num, int num2);
int sub (int num, int num2);
int mult (int num, int num2);
int div (int num, int num2);
int main ()
{
cout << "This program can do simple calculations for you. ";
cout << "\nChoose which you would like to do: ";
cout << "\n\nA. add";
cout << "\nB. subtract";
cout << "\nC. multiply";
cout << "\nD. devide";
cout << "\n\n\n Your choice: ";
char choice;
cin >> choice;
switch (choice)
{
case 'a':
case 'A':
{
int num, num2, total;
cout << "Ok you chose A";
cout << "\nEnter two numbers to add:";
cout << "\n First: ";
cin >> num;
cout << "\n Second: ";
cin >> num2;
total = add (num, num2);
cout << "\n Your total is: ";
cout << total;
cout << "\n\n";
break;
}
case 'b':
case 'B':
{
int num, num2, total;
cout << "Ok you chose B";
cout << "\nEnter two numbers to subtract:";
cout << "\n First: ";
cin >> num;
cout << "\n Second: ";
cin >> num2;
total = sub (num, num2);
cout << "\n Your total is: ";
cout << total;
cout << "\n\n";
break;
}
case 'c':
case 'C':
{
int num, num2, total;
cout << "Ok you chose C";
cout << "\nEnter two numbers to multiply:";
cout << "\n First: ";
cin >> num;
cout << "\n Second: ";
cin >> num2;
total = mult (num, num2);
cout << "\n Your total is: ";
cout << total;
cout << "\n\n";
break;
}
case 'd':
case 'D':
{
int num, num2, total;
cout << "Ok you chose D";
cout << "\nEnter two numbers to devide:";
cout << "\n First: ";
cin >> num;
cout << "\n Second: ";
cin >> num2;
total = div (num, num2);
cout << "\n Your total is: ";
cout << total;
cout << "\n\n";
break;
}
}
system ("pause");
return 0;
}
add (num, num2)
{
total = num + num2;
return total;
}
sub (num, num2)
{
total = num + num2;
}
mult (num, num2)
{
total = num * num2;
}
div (num, num2)
{
total = num / num2;
}
-
No, the porblem is that you don't define them correctly:
add (num, num2) //bad
{
total = num + num2;
return total;
}
First, spell out the return type, which is int:
int add(...)
{
...
}
Then, define the parameters as in the declarations:
int add (int num, int num2)
{
total = num + num2;
return total;
}
Repeat these steps for all the functions you're using and then recompile.
Danny Kalev
-
ok the whole thing is redone.....
it should compile but its not, so i'm seriously lost
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
using namespace std;
void add(int, int);
void sub (int, int);
void mult (int, int);
void div (int, int);
void menu ();
int main ()
{
bool keepgoing=true;
int choice;
for (keepgoing=true);
{
cout << menu ()<<endl;
cout << "Your choice is: ";
cin >> choice;
if (choice == 5)||(choice!= 1,2,3,4)
{
cout << "Program Ended";
keepgoing = false;
}
else{
switch (choice)
case '1':
{
int num1, num2;
cout << "Enter two numbers to add";
cout << "First: ";
cin >> num1;
cout << "Second: ";
cin >> num2;
cout >> "\n\n\nYour answer is: "<<add (num1,num2)<<endl;
break;
}
case '2':
{
int num1, num2;
cout << "Enter two numbers to subtract";
cout << "First: ";
cin >> num1;
cout << "Second: ";
cin >> num2;
cout >> "\n\n\nYour answer is: "<<sub (num1,num2)<<endl;
break;
}
case '3':
{
int num1, num2;
cout << "Enter two numbers to multiply";
cout << "First: ";
cin >> num1;
cout << "Second: ";
cin >> num2;
cout >> "\n\n\nYour answer is: "<<mult (num1,num2)<<endl;
break;
}
case '2':
{
int num1, num2;
cout << "Enter two numbers to divide";
cout << "First: ";
cin >> num1;
cout << "Second: ";
cin >> num2;
cout >> "\n\n\nYour answer is: "<<div (num1,num2)<<endl;
break;
}
}
}
}
system ("pause");
return 0;
}
void add (int num1, int num2)
{
int total= num1 + num2;
cout << total << endl;
}
void menu ()
{
cout << " *****************Menu******************** ";
cout << " * * ";
cout << " * Choose what you would like to do: * ";
cout << " * * ";
cout << " * 1 ] Add * ";
cout << " * 2 ] Subtract * ";
cout << " * 3 ] Multiply * ";
cout << " * 4 ] Divide * ";
cout << " * 5 ] Quit * ";
cout << " ***************************************** ";
}
void sub (int num1, int num2)
{
int total= num1 - num2;
cout << total << endl;
}
void mult (int num1, int num2)
{
int total= num1 * num2;
cout << total << endl;
}
void div (int num1, int num2)
{
int total= num1 / num2;
cout << total << endl;
}
-
This is the problem:
for (keepgoing=true)
You probably mean:
while (keepgoing==true)
{
...
}
Danny Kalev
-
Also:
cout >> "\n\n\nYour answer is: "<<mult (num1,num2)<<endl;
THe return type of aqll your functions is void, which is no good in this case: you have to call a function that returns *some* value in a cout expression:
int mult(int n1, int n2);
cout >> "\n\n\nYour answer is: "<<mult (num1,num2)<<endl;
This should work.
Danny Kalev
-
dam....
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
using namespace std;
int add(int num, int num2);
int sub (int num, int num2);
int mult (int num, int num2);
int div (int num, int num2);
void menu ();
int main ()
{
bool keepgoing=true;
int choice;
while (keepgoing==true);
{
cout << menu ()<<endl;
cout << "Your choice is: ";
cin >> choice;
if (choice == 5)||(choice!= 1,2,3,4)
{
cout << "Program Ended";
keepgoing = false;
}
else{
switch (choice)
case '1':
{
int num1, num2;
cout << "Enter two numbers to add";
cout << "First: ";
cin >> num1;
cout << "Second: ";
cin >> num2;
cout << "\n\n\nYour answer is: "<<add (num1,num2)<<endl;
break;
}
case '2':
{
int num1, num2;
cout << "Enter two numbers to subtract";
cout << "First: ";
cin >> num1;
cout << "Second: ";
cin >> num2;
cout << "\n\n\nYour answer is: "<<sub (num1,num2)<<endl;
break;
}
case '3':
{
int num1, num2;
cout << "Enter two numbers to multiply";
cout << "First: ";
cin >> num1;
cout << "Second: ";
cin >> num2;
cout << "\n\n\nYour answer is: "<<mult (num1,num2)<<endl;
break;
}
case '2':
{
int num1, num2;
cout << "Enter two numbers to divide";
cout << "First: ";
cin >> num1;
cout << "Second: ";
cin >> num2;
cout << "\n\n\nYour answer is: "<<div (num1,num2)<<endl;
break;
}
}
}
}
system ("pause");
return 0;
}
int add (int num1, int num2)
{
int total= num1 + num2;
cout << total << endl;
}
void menu ()
{
cout << " *****************Menu******************** ";
cout << " * * ";
cout << " * Choose what you would like to do: * ";
cout << " * * ";
cout << " * 1 ] Add * ";
cout << " * 2 ] Subtract * ";
cout << " * 3 ] Multiply * ";
cout << " * 4 ] Divide * ";
cout << " * 5 ] Quit * ";
cout << " ***************************************** ";
}
int sub (int num1, int num2)
{
int total= num1 - num2;
cout << total << endl;
}
int mult (int num1, int num2)
{
int total= num1 * num2;
cout << total << endl;
}
int div (int num1, int num2)
{
int total= num1 / num2;
cout << total << endl;
}
-
Could you post some error messages? The exact message please?
gorshing
newb
-
Well, one problem is what Danny has already stated ...
The return type of aqll your functions is void, which is no good in this case: you have to call a function that returns *some* value in a cout expression
You are still doing that here ...
Code:
cout << menu ()<<endl;
The error message I received is ...
Code:
test.cpp(20) : error C2679: binary '<<' : no operator found which takes a right-
hand operand of type 'void' (or there is no acceptable conversion)
See how it gives you the exact line number where the error is at?
gorshing
newb
-
wat compiler are u using ?
cuz mine sux it gives me crappy error messages that dont mean anything.
anyways, the menu function i've done that before using void. so it should work.
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
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks