Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 29th, 2005, 2:22 PM   #1
crazykid48x
Programmer
 
crazykid48x's Avatar
 
Join Date: Apr 2005
Posts: 96
Rep Power: 4 crazykid48x is on a distinguished road
Arrow Noob problem

Well im learning c/c++ but I've ran into a slight snag while making an extreamly basic calculator. heres the code.

Quote:
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int multiply(int n1, int n2);
int divide(int n1, int n2);
int add(int n1, int n2);
int subtract(int n1, int n2);

int main()
{
int n1;
int n2;
char sign;

cout<<"insert a number, a sign (+,-,x, or %), and then another number";
cin>>n1;
cin>>sign;
cin>>n2;

switch(sign)
{
case 120:
int multiply(int n1, int n2);
break;
case 43:
int add(int n1, int n2);
break;
case 45:
int subtract(int n1, int n2);
break;
case 37:
int divide(int n1, int n2);
break;
default:
cout<<"please enter +,-,%, or x as signs";
}
system("pause");
return 0;
}
int multiply(int n1, int n2)
{
return n1*n2;
}
int divide(int n1, int n2)
{
return n1/n2;
}
int add(int n1, int n2)
{
return n1+n2;
}
int subtract(int n1, int n2)
{
return n1-n2;
}
the code compiles but it doesnt woek properly. what did i do wrong? thx guys
crazykid48x is offline   Reply With Quote
Old Apr 29th, 2005, 2:41 PM   #2
spydoor
Programmer
 
Join Date: Feb 2005
Posts: 64
Rep Power: 4 spydoor is on a distinguished road
The problem is how you are trying to call the functions.

(int n1, int n2) // do not need to define the types here
int multiply //int is the type of return value; you should assign the result of the function to a variable of type int
(or you could just directly print the result without assigning it to a variable)

example.

int result;
...
case 120:
result = multiply(n1, n2);
cout << result;
break;

or

case 120:
cout << multiply(n1, n2);
break;
spydoor is offline   Reply With Quote
Old Apr 29th, 2005, 4:06 PM   #3
crazykid48x
Programmer
 
crazykid48x's Avatar
 
Join Date: Apr 2005
Posts: 96
Rep Power: 4 crazykid48x is on a distinguished road
oh!!! Ofcourse, I expected it to display without using the cout function. lol! thanks for the help.
crazykid48x is offline   Reply With Quote
Old Apr 29th, 2005, 6:02 PM   #4
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
you might want to use floats or doubles for this as integer division is kind of weird. :-)
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:56 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC