473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

What am I doing wrong with this simple program?

Ahhhh, ive missed so many lessons this year that i have to try and teach myself c++ from a book and its so hard. Usually I just read this forums to pick things up but its doing my head in trying to work it out, so I thought I would sign up and ask some people who know what they doing.

Basically im trying to write a program that reads in the radius of a circle and prints its radius, diameter,circumference and area. It dosent seem that hard but when i input my code....

Expand|Select|Wrap|Line Numbers
  1. //Write a program that reads in radius of a circle and prints the circle's, diameter, circumference and area
  2.  
  3. #include <iostream>
  4. #include <cmath>
  5.  
  6. //declaration of variables
  7. int main ()
  8. {
  9. int radius;
  10. int diameter;
  11. int circumference;
  12. int area;
  13. int pi;
  14.  
  15. //ask user to enter radius of circle
  16. std::cout <<"Enter the radius (integer number only) of the circle\n";
  17. std::cin >> radius;
  18.  
  19. //assign result
  20. diameter = radius * 2;
  21. circumference = pi * diameter;
  22. area = pi * radius * radius;
  23.  
  24. //print results
  25. std::cout << "The Radius of the circle is" << radius << std::endl;
  26. std::cout << "The Diameter of the circle is" << diameter << std::endl;
  27. std::cout << "The Circumference of the circle is" << circumference << std::endl;
  28. std::cout << "The Area of the circle is" << area << std::endl;
  29.  
  30. return 0;
  31.  
  32. }
and plug in my radius number, I get crazy negative values for my diameter, circumference and area. These crazy values are an inherent thing that seem to crop up in a lot of the codes I try to construct so I think there is an error that I systematically put into all my programs through being rubbish at c++. Could anyone point me towards the direction of where I am making this fatal error because I cant for the life of me see it? Usually the error reports help but when I compile I get no errors, so I am completely lost.

An example of the output im getting when i run the program

when the radius is 5, the circumference is -8, and the area is -20.

And im using microsoft visual

Thanks for any help.
Nov 14 '07 #1
4 2348
Im kinda cracking it I think.

I get 8 for my diameter which is spot on.

So it looks like the problem crops up with the circumference and area (so it looks like pi is the problem which makes sense as its the first time Ive used it on a program).

Now I get

When radius = 4
Diameter = 8
Circumference = -1717986972
Area = - 858993 or something like that

Ive read about double and float a wee bit but im not sure if I should have incorparated those for this program as im using pi to be 3.14169, and im not sure how I would do that.
Nov 14 '07 #2
beacon
579 512MB
What are you using for pi? You have it declared as an integer variable, but typically it's a float. That's one thing that stands out to me.
Nov 14 '07 #3
Banfa
9,065 Expert Mod 8TB
You never assign a value to the variable pi.

Also your program uses integers, this will mean poor results since the integer value of pi is 3. You may wish to consider a re-write using double.
Nov 14 '07 #4
Thanks guys.

Ive replaced int pi with - cons float pi = 3.141593

And replaced my int with double ( Ive always used int and so it occur to me to do that for this). Its nailed on now, giving me the correct results to a greater degree of accuracy.

Thanks again.
Nov 14 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
11
by: Alfonso Morra | last post by:
Hi, I am at the end of my tether now - after spending several days trying to figure how to do this. I have finally written a simple "proof of concept" program to test serializing a structure...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
8
by: Connectcase | last post by:
Hi there, and greetings from the Netherlands. Trying to switch over from VB to VB.NET and struggling with something that seemed very simple: ============================ Public Class Form1...
8
by: watkinsdev | last post by:
Hi, I have created a mesh class in visual studio 6.0 c++. I can create a device, render objects and can edit the objects by for instancnce selecting a cluster of vertices and processing the...
10
by: Enkidu | last post by:
Beginner question, sorry! I am using indexers to access an array of StringBuilders in an instance of a class: Getting: value = board1; Setting: board1 = value1 ;
34
by: raphfrk | last post by:
This program should copy one file onto the other. It works if I compile it with gcc to a cygwin program. However, if I compile it with the -mno-cygwin option, it doesn't work (this targets native...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.