Welcome, guest! Please login or register.

    * Shoutbox

    RefreshHistory
    • hexedss1: Anyone would like to help on client side got a variable declartion Error
      August 15, 2018, 09:06:30 PM
    • SOUPSOUP: just get a vpn
      August 13, 2018, 06:44:28 PM
    • jme: _^_ will answer all of your javascript questions
      August 12, 2018, 07:11:00 PM
    • doomsale: and where is boxman?
      August 10, 2018, 03:16:59 AM
    • doomsale: Where can i play the Moparscape MMORPG with the Metallica instrumentals?
      August 10, 2018, 03:16:47 AM
    • doomsale: no longer clicking on doors and dummies at east bank
      August 10, 2018, 03:16:07 AM
    • doomsale: were all grown up now huh?
      August 10, 2018, 03:15:58 AM
    • _^_: hey everyone look at my graphs [link]
      August 06, 2018, 01:52:16 PM
    • braedyn: Someone pl0x help me out with PI Lottery!!
      August 03, 2018, 02:12:25 AM
    • Travas: p3n1s
      July 26, 2018, 09:46:33 PM
    • Krackkid: You up :eggplant:
      July 23, 2018, 06:18:17 PM
    • DeathsChaos9: no
      July 23, 2018, 03:53:31 PM
    • Emulous: Come check out Emulous: [link]
      July 22, 2018, 03:37:19 PM
    • Emulous:[link]
      July 22, 2018, 03:37:00 PM
    • Travas: ur mom
      July 21, 2018, 06:04:50 PM
    • YNRDAB: Watcha big ballers up too?
      July 20, 2018, 11:01:06 AM
    • dark250: i need help for coding client
      July 19, 2018, 08:27:37 AM
    • Travas: yes
      July 14, 2018, 02:19:44 PM
    • drubrkletern: lmao racist and sexist much?
      July 14, 2018, 01:11:52 PM
    • Cole1497: yeah i got you lil boat give me a second
      July 13, 2018, 03:19:35 AM

    Author Topic: C++ returning values that end with (.00)  (Read 1024 times)

    0 Members and 1 Guest are viewing this topic.

    OfflineXellium

    • Member
    • ***
    • Posts: 124
    • Thanks: +0/-0
      • View Profile
    C++ returning values that end with (.00)
    « on: January 25, 2016, 08:49:27 AM »
    So I'm doing this homework assignment and I'm a bit stuck. The code is executable but the teacher is picky and wants the first value to return as (.00). I've scanned through my book looking for some type of operator. I was sure that any value */+- double would return a double value. It does for the next two but I can't get my first return to return in decimal places.

    Code: C++
    1. /*
    2. Program Name:   Assignment1.cpp
    3. Chapter:                Chapter 2 Assignment 1
    4. Programmer:             Zach Marshall
    5. Date:                   1/15/2016
    6. Description:    A program designed to take the input of a radius.
    7. Then with the input of the radius, it will display circumfrence, area, and diameter.
    8. */
    9.  
    10. #include <iostream>
    11. #include <cmath>
    12. usingnamespace std;
    13.  
    14. int main(){
    15.         /*Start of Data types*/
    16.  
    17.         double radius, diameter, circumference, area;
    18.  
    19.         //start constants
    20.         constdouble pi =3.14159;
    21.  
    22.         /*End of Data types*/
    23.  
    24.         /*Start of Input/Output statements*/
    25.         cout<<"Enter the radius of the circle (in inches): ";
    26.         cin>> radius;//The user inputs how large the radius is
    27.  
    28.         /*Secondary Datatypes used here because you can't declare radius as a variable without first defining it with the input*/
    29.         diameter =2.00* radius;//Equation for finding the diameter of a circle
    30.         circumference =2* pi * radius;// Equation for finding the circumference of a circle
    31.         area = pi *pow(radius, 2.0);//Equation for finding the area of a circle (uses power function) (radius*radius)
    32.  
    33.         cout<<"The Diameter of a circle with a radius of "<< radius <<" is: "<< diameter <<" inches."<< endl;//output of the diameter
    34.         cout<<"The Circumference of a circle with a radius of "<< radius <<" is: "<< circumference <<" inches."<< endl;//output of the circumference
    35.         cout<<"The Area of a circle with a radius of "<< radius <<" is: "<< area <<" inches."<< endl;//output of the area
    36.         /*End of Input/Output Statements*/
    37.         return0;
    38. }

    The output if the radius is 10 =

    (click to show/hide)

    I have tried entering it with the decimal places and without but no change as seen here.

    Project Project, coming underway.
    Runescape Gambling

    OfflineBowser jr

    • Member
    • ****
    • Posts: 6,001
    • Thanks: +0/-0
      • View Profile
    Runescape Gambling

    OfflineXellium

    • Member
    • ***
    • Posts: 124
    • Thanks: +0/-0
      • View Profile
    Re: C++ returning values that end with (.00)
    « Reply #2 on: January 25, 2016, 10:03:11 AM »
    Code: [Select]
    std::cout << "The Diameter of a circle with a radius of " << radius << " is: " << std::setprecision(4) << diameter << " inches." << endl; //output of the diameter
    still returning a value of just 10 and not 10.00

    It did however modify all my other values to a precision of 4 so do I need to place the statement
    Code: [Select]
    std::setprecision(4) in another line?
    « Last Edit: January 25, 2016, 10:07:40 AM by Xellium »
    Project Project, coming underway.
    Runescape Gambling

    Offlinet4

    • Member
    • ****
    • *
    • *
    • *
    • Posts: 6,798
    • Thanks: +0/-0
      • View Profile
    Re: C++ returning values that end with (.00)
    « Reply #3 on: January 26, 2016, 07:42:31 AM »
    printf should do this:

    Code: C
    1. #include <stdio.h>
    2.  
    3. int main(void)
    4. {
    5.         printf("%.2f\n", 10.00D);
    6.         return0;
    7. }
    8.  

    Offlinejustaguy

    • Member
    • ****
    • *
    • Posts: 706
    • Thanks: +0/-0
      • View Profile
    Re: C++ returning values that end with (.00)
    « Reply #4 on: January 26, 2016, 11:09:17 AM »
    Make sure you include iomanip. Before you print out your values do this:
    Code: C++
    1. cout.precision(4);
    2. // You can also use this statement within your cout statements.
    3. cout<< fixed;
    Now your output will have fixed floating-point notation.

    See: http://www.cplusplus.com/reference/ios/fixed/
    RIP

     

    Copyright © 2017 MoparScape. All rights reserved.
    Powered by SMFPacks SEO Pro Mod |
    SimplePortal 2.3.5 © 2008-2012, SimplePortal