Thursday, July 30, 2009

C++ program?

i'm trying to write a program that does the same functions as a calculator. i'm using visual studio to write my code. so far i have this as my code


#include %26lt;iostream%26gt;





#include %26lt;string%26gt;





using namespace std;








int main(int)





{





// delcare variables





int x;





int y;





char function;





double answer;








//collect data





cout %26lt;%26lt; "Please enter a number" %26lt;%26lt; endl;


cin %26gt;%26gt; x;








cout %26lt;%26lt; "Enter a function from the list of (+ - * / %)" %26lt;%26lt; endl;





cin %26gt;%26gt; function;





cout %26lt;%26lt; "Please enter another number" %26lt;%26lt; endl;


cin %26gt;%26gt; y;


/*switch (function)


{


case '*':


x*y;


break;


case '+':


x+y;


break;


case '-':


x-y;


break;


case '/':


x/y;


break;


case '%':


x%y;


break;


default:


cout %26lt;%26lt; "Error please select another fucntion" %26lt;%26lt; endl;


}*/





//display answer


answer = x%26lt;%26lt;' '%26lt;%26lt;y;


cout %26lt;%26lt; " "%26lt;%26lt; ' '%26lt;%26lt; x %26lt;%26lt; ' '%26lt;%26lt; function %26lt;%26lt; ' '%26lt;%26lt; y %26lt;%26lt; " = " %26lt;%26lt; answer %26lt;%26lt;endl;





why do when i type 2-1 = 4?


please help!

C++ program?
Hope you missed the closing bracket of main only in the copy paste..





I am not sure of this part of the pgm


answer = x%26lt;%26lt;' '%26lt;%26lt;y;


cout %26lt;%26lt; " "%26lt;%26lt; ' '%26lt;%26lt; x %26lt;%26lt; ' '%26lt;%26lt; function %26lt;%26lt; ' '%26lt;%26lt; y %26lt;%26lt; " = " %26lt;%26lt; answer %26lt;%26lt;endl;





and y don't you use the like of


answer = x+y;


break;





good...it was my pleasure to be at your service :)





in your switch statement?


They anwer would automatically get coerced into double won't it?





I am no C++ pgmer. C is ma domain :)








ops..you have commented the switch function. i donno bout this magic answer x%26lt;%26lt;' '%26lt;%26lt;y;
Reply:I am guessing you know that the switch is commented out...





Try initializing all your variables i.e. x=0; answer=0;





Also, you aren't assigning the outcome value to anything. x-y? What = x-y? You proabably want answer=x-y





EDIT:


Sorry I tried to help... while I was trying to get your crap code to compile...

liama-song

No comments:

Post a Comment