Little help with C++

The_Other_One

VIP Member
Um, I guess programming is fine here... I'm in an intro to C++ and I've run into a slight problem. Our task is to make a simple calculator that will +-*/ two intergers. Simple enough... However, because I was sick recently, I missed the day he talked about type casting -.-

So, my question is, how do I convert two intergers into float? I tried a few things, but I always get 1/2=0. I could probably change x and y to float, but I beleive he wanted them to just be interger values...
 
something like

PHP:
#include<iostream>

using namespace std;

int main()
{
	int x=1;
	int y=2;
	

	cout<<float(x)/float(y)<<endl;


	return 0;
}

Would print out 0.5
 
you could just declare the variables as doubles.

you can either do static_cast<double>(variable)

or just double(variable)
 
Thank you mgoldb2. I do beleive I tried, that, but I was putting the solution in another variable(it was float) but I beleive that was causing the problem. Your little example reminded me I could end it with

cout << x << " / " << y << " = " << float(x)/float(y) << endl;

I beleive he wanted the operation to be shown, so I couldn't just have a solution.

alanuofm - I'm not sure what your talking about right off... This is just an intro class so I don't know much yet. Actually all I know is what I've taught myself in the past few days because I've been out sick!
 
Back
Top