C++ Doesn't calculate decimals.

Hello anyone im a beginner to C++, i tried to make my own program which would calculate Pi. It only shows 3 instead of the decimals. If anyone could analyze and tell me what i've done wrong, please do.

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

int main(int nNumberofArgs, char* pszArgs[])
{


int twentytwo = 22;
int seven = 7;
int result = twentytwo / seven;
cout << "Pi is:";
cout << result << endl;

//wait until user is ready before terminating program
// to allow the user to see program results
system ("PAUSE");
return 0;
}

As i said im completely new to C++ so bare with me.
Read this http://www.cplusplus.com/doc/tutorial/variables/
Specifically, about "floating-point" types. ints are just that: integers.
You need to use a different type to save a non-integer.


Also note that (int / int) division is going to be truncated to a whole number, so I would make either your twentytwo variable or your seven variable be a floating point type as well, such as double.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
    double twentytwo = 22;    //as Ganado said the numbers should be double not integers
    double seven = 7;
    double result = twentytwo / seven;    //also if you saved the number in an int you will get an integer (3)
    cout << "Pi is:";
    cout << result << endl;

//wait until user is ready before terminating program
// to allow the user to see program results
    system ("PAUSE");
    return 0;
}


also note that even the following code produce the same problem (which may lead to difficult-to-diagnose bugs)
double pi = 22 / 7;
this code could be corrected by 3 ways:
1-the first is the previous code (assigning the numbers (at least one) to other variables of type double

2-the second using type casting: double pi = ((double) 22) / 7; //C compatible style
or double pi = double(22) / 7; //C++ only style

note that the following code won't work: double pi = double(22 / 7);

however the following will work -however don't use it because it may be confusing-
double pi = (double) 22 / 7;

3-the third is:double pi = 22.0/7 //note the .0 which makes 22 a double



one last remark don't use system("PAUSE");
see why here: http://www.cplusplus.com/forum/beginner/1988/
Hey, dude thank you so much, i've got another dilemma. So i want to create a program which basically finds out how old you are when you type in your birthyear. So it needs to go something like this


yearNumber= 2014
What is your birthyear?
If user types in X then - X with yearNumber.

This is what i wrote so far

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

int main(int nNumberofArgs, char* pszArgs[])
{


int yearNumber = 2014;
cout << "Write your birthyear";




int age;
age = yearNumber - ?????? (I don't know what to do)
cout << "This is your age:";
cout << answer << endl;

system ("PAUSE");
return 0;
}

Basically i need help writing a line like this. (Age = yearNumber (2014) - The input from the user)
Last edited on
first you need to put the user input in a variable
1
2
int birthYear;
cin >> birthYear;

then it is very simple: age = yearNumber - birthYear;

one remark cout << answer <<endl; is wrong it should be age not answer

another remark please from now on write your code in code tags:
you could click on the first button under format
or write it yourself inside [ code] tags: [ code]your code[ /code]
note that I wrote a space between the [ and code so that the site don't transform it (you should omit it)
Okay, i will definitely start using the code tags. Apperantly i'm double posting, even though on my pc i only posted it once? So lets say i want it to say how many years, and months, or even days. How would i go about this? I am not telling you to write the whole code for me, you can refer me to other links and such.
Topic archived. No new replies allowed.