Help with simple problems

So I'm new to all this and I decided to independently create this program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "../../std_lib_facilities.h"

int main(){

	//Program to calculate the volume of spheres with radius's of 1-100
	const double pi = 3.14159;
	int i;
	double volume;
	for(i = 1; i<101; ++i){
		(4/3)*pi*(i*i*i);
		cin >> volume;
		cout << "Volume of a sphere with radius " << i << " is: " << volume << endl;
	}
	system("pause");
	return 0;
}


And before you ask, the std_lib_facilities.h is the library that I'm asked to use in the book that I'm reading on C++ at the moment.
Two problems:
1) Initially, when I try to run the program, nothing happens. I have to press and enter a character for the program to start. Why is this?
2) Now, I'm not sure if this is because of the type of equation I'm trying to do or whatever but this is the result for every calculation from 1 straight through to 100: -9.2556e+061

Any help would be greatly appreciated.
cin >> volume;

This fetches a value from cin (i.e. from the keyboard). That's why it waits for you to enter something on the keyboard.

Shouldn't you be calculating volume, rather than reading it in from the keyboard?

4/3 is an integer divided by an integer. You'll get another integer. I expect you don't want that. Trying making it 4.0/3.0
Last edited on
I guess you want to output volume of all spheres with radii of 1- 100.
Now....
Your line : (4/3)*pi*(i*i*i); just performs the calculation, doesn't store it anywhere.
You said that when you run your program, nothing happens? That is because it waits for you to input the volume: cin >> volume;. I guess you wanted something like:
1
2
3
4
5
for (int i = 1; i <= 100; i++)
{
     volume = (4.0/3.0) * pi * (i * i * i);
     cout<<"Volume of a sphere with radius "<<i<<" is: "<<volume<<endl;
}

And btw, do you read your book thoroughly, or do you just take an occasional look at it?
Last edited on
Your cin >> volume is the reason you need to put in some value.

The fact that you never assign the variable volume to anything accept the user input is why your output is wrong.

Your program calculates your equation, throws away the answer, asks the user for the answer and then outputs that. I doubt that's what you intended.

Last edited on
And btw, do you read your book thoroughly, or do you just take an occasional look at it?


I assume the book is Stroustrup's PP&P. It really does need to be read carefully, but it's worth the effort.
Thanks for the replies. Yes, I am reading the Programming Principles & Practice.
I thought I understood it all when I was reading it and I have been reading it thoroughly without distractions. Bit of a motivational blow that I still can't get the basics even though I've read quite a lot of it.

I've changed my code to this now and it works:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "../../std_lib_facilities.h"

int main(){

	//Program to calculate the volume of spheres with radii of 1-100
	const double pi = 3.14159;
	int i;

	for(i=1; i<101; ++i){
		cout << "Volume of a sphere with radius " << i << " is: " << (4.0/3.0)*pi*(i*i*i) << endl;
	}

	system("pause");
	return 0;
}


So thank you very much :D No doubt I'll be back here again in the near future.
Topic archived. No new replies allowed.