You are here:

C++/Beginner Question

Advertisement


Question
I am Just getting started With C++(From VB) In a simple program I want to Output floating or double result. Here is what I have and Outputs 3.000000 and  should be 3.3333333

//Caluclate and Report c
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])

{
   int a,b;
   float c;
   a = 10;
   b = 3;
   c = a/b;
   
   printf("Answer = %f",c);
   printf("\n");

   system("PAUSE");
   return EXIT_SUCCESS;
}

Thank you for your help!

Answer
Hi,
 Here, use a little bit of typecasting.

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])

{
  int a,b;
  float c;
  a = 10;
  b = 3;
  c = float(a)/float(b);
  cout << c << endl;

  return EXIT_SUCCESS;
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Dharmender Rai

Expertise

I can answer general and system level C/C++ questions.

Experience

I have 5 years of experience in C++.

©2016 About.com. All rights reserved.