Intro to C++, NEED HELP!!!

This is a discussion on Intro to C++, NEED HELP!!! within the C++ Programming forums, part of the General Programming Boards category; I have to write a program to calculate the diameter, the circumference, and the area of a circle with a ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    73

    Intro to C++, NEED HELP!!!

    I have to write a program to calculate the diameter, the circumference, and the area of a circle with a radius of 6.75. Please tell me what I am doing wrong...I am going on 5 hours, and I am lost:-( Any help would be greatly appreciated.

    //************************************************
    //Calculations of a circle program
    //This program finds the diameter, circumference
    //and the area of a circle with a preset radius.
    //************************************************

    #include <iostream>
    #include <iomanip>
    #include <cmath>

    using namespace std;

    float radius ;
    float pi ;

    int main()

    {

    radius = 6.75; //Size of radius
    pi = 3.14159; //Size of PI


    cout << "The radius of a circle is : "<< radius << endl;


    diameter = 2*radius;
    circumference = diameter * pi;
    area = pi * radius * radius;

    cout << "The diameter is " << diameter << endl;
    cout << "The circumference is " << circumference << endl;
    cout << "The area is " << area << endl;

    return 0;
    }
    Last edited by romeoz; 06-10-2003 at 09:20 PM.

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    float radius
    float pi

    forgot the ; ?

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    diameter
    circumference
    area

    u dont declare those

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    73
    that was my bad...I put them in..what do u mean I dont declare them...how do I do that? I thought if I put = something then that is declaring them...I think I am lost.. Thanks

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    To declare a variable, you need to give it a type - i.e.:

    double diameter = ...;
    float x;
    ... etc ...
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    try something like


    Code:
     
    
    const double RADIUS = 6.75;
    const double PI = 3.14159;
    
    double diameter, circumference, area;
    
    etc.
    you need to declare variables before you use them. radius and PI would make better constants since they are not likely to change.
    Mr. C: Author and Instructor

  7. #7
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    or you can do this...
    Code:
    double diameter = 2*radius;
    double circumference = diameter * pi;
    double area = pi * radius * radius;

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    73
    I put the following:
    Const doble RADIUS = 6.75;
    const doble PI = 3.14159;

    and

    double diameter, double circumference, double area and sure enough it worked. Thanks for the help.

    I read Chapter 3 over again today and I dont know why I needed to add double in front of those to make it work...could someone explain that to me, I would really appreciate it alot.

    Bryan

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    C++ is a strongly typed language. It makes sure everything has a type, type promotion rules are followed closely (i.e. the result of int+double=double), and no type is used in an improper way.

    As a result of all of this, it needs to know the type of a variable when you create it.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Registered User
    Join Date
    Jun 2003
    Posts
    73
    I know you are probably repaeting yourself, but what does "double" do for the variable? thanks

    Bryan

  11. #11
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    'double' is a data type. It lets the compiler know that that variable will be storing a value to be represented in a specific way. 'double' can hold floating-point numbers (ones with decimals), as can 'float' s (though a float is smaller).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  12. #12
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    double is a type of variable that can store decimals.... and larger/smaller numbers that float can (not sure exactly how many)

  13. #13
    Registered User
    Join Date
    Jun 2003
    Posts
    73
    so anytime I want to store decimals I should put const double in front it the variable...I think I am getting it. I am taking this onlline at UMUC and its very hard doing this online because there is not much help. I appreciate all you guys that gave me help, I hope you can continue to help me throughout the semester:-)

    Bryan

  14. #14
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Actually, the 'const' modifier just means that its a constant, which cannot be changed in the program. If you want to manipulate the decimal, just use 'double'.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  15. #15
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    yeah, the const means that the variable cannot be changed anytime in the program after you declare it. For example:
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
         int a=5;
         const int b=3;   //Declares a constant integer
         cout << a << endl;
         cout << n << endl;
         a=7;                 //no problem, this one isnt const
         b=4;                //Will get an error for trying to change the  value of a constant
         cout << a << endl;
         cout << b << endl; //erase the const infront of int b and it will compile to see that both a and b can change :D
         return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Intro to embedded programming
    By ohanna in forum C Programming
    Replies: 6
    Last Post: 03-13-2008, 03:16 PM
  2. New to C#? Intro Lesson
    By oval in forum C# Programming
    Replies: 0
    Last Post: 04-07-2006, 04:57 PM
  3. OnExit() and Intro button
    By artelo in forum Windows Programming
    Replies: 0
    Last Post: 09-24-2003, 02:13 PM
  4. Beginer looking for an intro to graphics in C++...
    By scooby13q in forum C++ Programming
    Replies: 17
    Last Post: 06-07-2002, 05:19 PM
  5. VC++ Intro edition.
    By Doble04 in forum C++ Programming
    Replies: 4
    Last Post: 10-10-2001, 05:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21