C++ :: Getting 2 Decimal Places In Output Text File?

Jul 30, 2014

how I can make my output only have 2 decimal places because I want it to be a monetary value. In my code I used this:

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

which worked well at first because on my command prompt screen the numbers had two decimal places and looked liked this:

Premium for Pol1 is $14101.62
Premium for Pol2 is $14221.16
Premium for Pol3 is $582390.50
Premium for Pol4 is $220384.49

However, I also have the program outputting to a textfile called "output.txt" and when I opened this text file the numbers only had one or no decimal places.

Premium for Pol1 is $14101.6
Premium for Pol2 is $14221.2
Premium for Pol3 is $582391
Premium for Pol4 is $220384

How come the code for two decimal places is working for my output to command prompt but not my output to the text file?

Here is my code.

double ratesmn[86] = {
#include "MaleNonSmoker.txt"
- 1
};

[Code].....

View 2 Replies


ADVERTISEMENT

C++ :: Nested Selection Statements - Format Output Into Two Decimal Places

Apr 19, 2014

event................. red house.............. blue house........... yellow house
Chess ......................12.................... 5....................................8
TableTennis .................. 4.......................... 11.....................17
Basket Ball .................... 6......................... 5......................14
karathe ..........................5........................... 8.........................10

Write a program to input the house name, and the score (as shown in the table) of the sport event obtained by the corresponding house. Then find the total score . You should use nested selection statements.

i) If the user inputs a wrong house name, display the error message "In valid House Name".

ii) Modify the program to handle many data. After calculate and disp ay the total score of the house, the program should display a prompt "Do you need to enter nother house name?".

If the user inputs "y" or "Y", program should ask for the next house name and the scores.
If the user inputs "n" or "N" after entering all three houses, program should terminate by printing the score of the winner.
If the user inputs "n" or "N" and without entering all three houses, program should display an error message "Error in finding the winner" and terminate without printing the ranks.

Note: format the output into two decimal places.

Sample output:
Enter the house name: red house
Enter the score of Chess: 12
Enter the score of Table Tennis:4
Enter the score of Basket Ball:6
Enter the score of karathe 5
Score is: 27 Points
Do you enter another house name? y

Enter the house name: blue house
Enter the score of Chess:5
Enter the score of Table Tennis:ll
Enter the score of Basket Ball:5
Enter the score of karathe 8
Score is: 29 Points
Do you enter another house name? y

Enter the house name: yellow house
Enter the score of Chess:8
Enter the score of Table Tennis: 17
Enter the score of Basket Ball: 14
Enter the score of karathe 10
Score is: 49 Points
Do you enter another house name? n
The winner scored: 49 Points

View 3 Replies View Related

C :: Float Setting Decimal Places

Jun 27, 2013

I am trying to find out when using float in a calculation how to set the number of decimal places. For example my code below

Code:
#include stdio.h>
int main()
{
float x=123.0;

[Code]....

This returns an answer 8487.0000 I would like it not to show all the decimal places. However if the sum has decimal places I would like to select the number of decimal places shown.

View 3 Replies View Related

C :: User Defined Number Of Decimal Places?

Sep 2, 2013

I need the user to be able to input the number of decimal places they wish to have displayed in the output. Everything works fine as is, I just don't know how to allow for the user to input the number of decimal places they want the output to have.

Code:

#include<stdio.h>
#include<math.h>
#define PI 3.141592654
int main(void) {
//Local Declarations
int x; //desired number of decimal places
float radius; //radius of circle
float circumference; //circumference of circle

[Code] .....

View 2 Replies View Related

C++ :: Create A Converter But Results Must Be In 4 Decimal Places

Dec 1, 2013

I have to create a converter but the results must be in 4 decimal places just like, if i Entered 5000mm the result would be 500.0000cm . what should I do ? I used Float function

View 1 Replies View Related

C :: Inventory Program - Entering Amounts With Two Decimal Places

Jun 7, 2013

So my inventory program runs fine as far as I can tell. Except when entering "Amounts" with more that 1-precision . ie

Amount Entered : 25.99 // Problem
Amount Entered : 25.9 // No Problem

When entering say 25.99, the rest of the file is jibberish. but 25.9, the rest of the file is fine

Code:
#include <stdio.h>
typedef struct toolRecord{
int record_num;
char tool_name[16];
int quantity;
double price;

[Code] .....

View 5 Replies View Related

C :: Program To Generate A List Of Powers Of 3 - Decimal Places?

Sep 23, 2014

So my assignment requires us to write a C program to generate a list of powers of 3 where each output line lists three numbers: k 3k 3‐k

It should look like this (just pretend all the numbers are lined up in their respective columns)

1 0 1.0
3 1 0.333333343
9 2 0.111111112
27 3 0.037037037
81 4 0.012345679
243 5 0.004115226
729 6 0.001371742
2187 7 0.000457247
6561 8 0.000152416
19683 9 0.000050805

This is what I keep getting:

1 0 1.000000000
3 1 0.333333343
9 2 0.111111112
27 3 0.037037037
81 4 0.012345679
243 5 0.004115226
729 6 0.001371742
2187 7 0.000457247
6561 8 0.000152416
19683 9 0.000050805

My input:

Code:
#include <stdio.h>
#include <stdlib.h>
void powers(void) {
int i=0;
int n=9;
int x=1;
float h=1;

[Code] .....

Notice how I'm getting 1.000000000 for my first h value in my output whereas I should be getting 1.0. What am I doing wrong?

View 6 Replies View Related

C :: Iteration Bound Determination - Get Result Correct Upto N Decimal Places

Mar 26, 2013

suppose I want to find the value of e ,e^x or sin(x) or any such function in general ,through their infinite series (maclaurin or taylor) how many iteration should I have to make to get the result correct uptu 'n' decimal places?

View 6 Replies View Related

C :: Output Does Not Print On Text File

Jul 19, 2013

In this program I can not print the typed value in the file. The file creation well done but it is an empty file

I press Ctrl +z for EOF

Code:
#include<stdio.h>
int main(void){
FILE *fp;
char ch;
fp=fopen(" Test.txt","w");
printf(" Enter the TEXT

[Code] .....

View 1 Replies View Related

C++ :: Program Won't Output Any Information To Text File

May 6, 2013

my program wont output any information to the text file that I have specified it just creates it but it leaves it blank

#include <iostream>
#include<string>
#include<iomanip>
#include <fstream>
using namespace std;
class Carloan

[code]....

View 2 Replies View Related

C++ :: Output Unusual Format For Text File

Jul 24, 2013

Bit of a problem, I'm trying to output the data from my code into a particular format to a text file. The format is

y0 y1 y2 y3 y4 y5...yn
z0 0.01 0.2 1
z1 0.4 0 etc...
z2
z3
z4
z5
...
zn

Any pointers?

View 1 Replies View Related

C++ :: Writing Output Of While Loop To Text File?

Jul 30, 2014

[URL] when I try to write my output to a file. I am building a life insurance premium calculator with c++. What my calculator does is read in a text file called "policies.txt" containing a number of different policies. For example:

Pol1 M N 20 100000 1 .04 99
Pol2 F S 30 100000 1 .05 99
Pol3 M S 72 750000 1 .03 99
Pol4 F N 45 1000000 1 .05 99

And using this, it creates a single premium. Once the calculator calculates the premium of each policy, I want it to write out the premium amount of each policy to a textfile called "output.txt". The problem is that when I check my "output.txt" file, only the premium for the last policy shows up. It only reads:

Premium for Pol4 is 220384
When I want it to read:

Premium for Pol1 is 14101.6
Premium for Pol2 is 14221.2
Premium for Pol3 is 582391
Premium for Pol4 is 220384

How come only the last policy is showing up? and is there any way to make all four policies appear in my output text file? Here is my code:

double ratesmn[86] = {
#include "MaleNonSmoker.txt"
- 1
};

[Code]....

View 2 Replies View Related

C/C++ :: Read Text File And Display It In Output

Apr 7, 2014

I'm supposed to read a text file and display it in the output. I have it down except my formatting. I just cannot get it to just skip one line rather than multiple. I know why though because my char has a max of 11 and if I put a character lets say mango, it is going to fill in the rest with white spaces. It's supposed to display like this:

Mango
Strawberry
Grapes

But instead I get something like this

Mango

Melon

Grapes

I am using isspace but it's not working.

//source code

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <fstream>
using namespace std;
class CFile

[Code] ....

View 5 Replies View Related

C++ :: How To Output Void Function On A Text File

Feb 12, 2013

So I am writing this code that analyzes a file called "analysis.txt" and prints out in an ouput file (called "report.txt") the results of the analysis, i.e.,(the frequency of all alphabet letters present in the file).

I am having trouble with outputing on the file2.

here is what I have:

Code:

#include<iostream>
#include<fstream>
using namespace std;
ifstream file1;

[Code]....

I tried to lose the "<<" after the file 2, but it's still giving me an error. how to output a void function on a text file?

View 14 Replies View Related

C++ :: Output Numbers As 2 Decimal

Feb 11, 2013

My program needs to be adjusted to send out the numbers as 2 decimal , but nothing work.

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int
number_ofstyle_American,
number_ofstyle_Modern,

[Code] .....

View 1 Replies View Related

C++ :: Input / Output Text File Manipulation Not Working

Mar 24, 2013

Why my output screen for this program does not want to stay open. It only opens for a split of a second and it's gone. The program is supposed to take numbers from a input file and display and save the manipulation in the output file. Here is the program.

Code:
#include<iostream>
#include<fstream>
#include<iomanip>
usingnamespace std;

[Code] ....

View 6 Replies View Related

C++ :: Grade Calculator - Output With Comment To A Text File

Feb 24, 2013

I am a math teacher who is taking a programming class. My district wants to incorporate some programming tasks into the high school math curricula. I am working on a project to calculate a final grade and output this along with a comment to a text file. The following data is the input that I created:

3 300 100 100 100 .15
4 400 100 98 94 92 .45
3 300 98 100 92 .40

The final grade output results in gibberish.

#include <iostream>
#include <fstream> // For file I/O
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;

[Code] ....

View 6 Replies View Related

C++ :: Can't Output Decimal Number From A Function Result

Mar 28, 2013

I have this simple code here:

Code:
#include <iostream>
int multiply (double x, double y) {
double result = x*y;
return (result);

[Code] ....

I get the answer 5.94 (which is what I'm looking for). I can't work out why the first example is not outputting a decimal number. I have set the variables as a double so I just can't see why this is not working for me.

View 1 Replies View Related

C :: Simple Decimal Input To Binary Output

Jun 22, 2013

I am learning c because I want to get back into programming microcontrollers, which I previously did in assembly. I wanted to make something fairly tight in terms of program memory and RAM to show me an output in binary form. When you are troubleshooting a file register or serial interface when you can see the actual bit values (on a small LCD for a micro-controller) and compare it to a datasheet.

Code:
#include <stdio.h>
#include <math.h>
int main() {
int i;
int decimaltoconvert;
int convertingarray[7];
int convertingarray2[7];

[Code] .....

Also, how might I go about putting that into a function that I could call?

View 6 Replies View Related

C/C++ :: Read All Song Titles From A Text File - Randomly Select And Output?

Apr 19, 2015

So far I have managed to sort songs by their string length in main. The void function is there because i learned how to create it, but it's not being used because I dont know how to use it.

Expanding on main, how would I go about selecting a random song from the array?

#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
struct Song {
string song;

[code]....

View 2 Replies View Related

C :: Not Getting Actual Output While Displaying Hex Numbers In Decimal Format

Apr 26, 2013

I need to display 0-15 hex numbers[0X00-0x0F] in decimal value...& I'm getting the output but it's not exactly what it should be,below is my code.. [This not the complete code,but main part where the changes are done]

Actual output i should get is for 1v it should generate 0001,for 2v it should generate 0010 and simultaneously till [15v-1111]... But what i'm getting is exactly different to this for eg for 7v,8v&9v the bits generated are 1101,1011,1011 respectively...

[URL] ....

Code:
sbit V1 = P2^0;
sbit V2 = P2^2;
sbit V3 = P2^4;
sbit V4 = P2^6;
#define DAC_table V1,V2,V3,V4

#include <stdio.h>
#include <string.h>
idata unsigned int ptr2tbl ;

[Code] .....

View 2 Replies View Related

C/C++ :: Incorrect Output From Unsigned Binary To Decimal Program

Feb 2, 2015

This program has to convert an unsigned binary number into a decimal number. No matter what binary number I enter, however, it always outputs that the decimal number is 0.

My code is as follows:

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int main() {
string binarynumber;
cout << "Enter an unsigned binary number up to 32 bits." << endl;

[Code] ....

And my output:

Enter an unsigned binary number up to 32 bits.
00001111
That number in decimal is 0

The output should have shown the binary number in decimal to be 15, and I cannot find my error.

View 6 Replies View Related

C++ :: Accept Signed Decimal Integer As Input And Output In Binary

Jan 29, 2015

Write a C++ application program to accept a signed decimal integer as input and output the equivalent 2s complement version in 16-bit binary. Include a space between every four bits in the output string. The input will only be processed by the application if it falls in the valid range that can be represented in 2s complement format with 16 bits. The range of a decimal number from - to + is -32768 to 32767.

View 3 Replies View Related

C++ :: Input Decimal Number And Output To Be Number Scientific Notation

Apr 26, 2013

I need to write a code in c++ , to input decimal number and the output to be number in Scientific notation with 32 bits .

View 1 Replies View Related

C :: Original Text File / Generate Another File With Text Content In Upper Case

Nov 29, 2014

Code software that, from an original text file, generate another file with the text content in upper case.For exemple:

entrence:

luke
tom
alex

outings:

LUKE
TOM
ALEX

My code so far:

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
}

[code]....

View 2 Replies View Related

C/C++ :: Rotate Array By 2 Places?

Jul 25, 2012

input: array[] = {0,1,2,3,4};
output should be {3,4,0,1,2};

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved