convert string to double
Nov 11, 2014 at 5:08pm Nov 11, 2014 at 5:08pm UTC
Hi guys. I have been looking everywhere for an answer and couldn't find one. I am a beginner in c++ and I have an assignment. The assignment asks to read a set of face values from a txt file, and depending on the face value, it will print out "doubled" ,"tripled", etc. I first defined a string face value and then i used getline to read the individual values. So far i can echo the face values just fine. My question is, how can i make a certain face value output certain things.
Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
void main()
{
string sfacevalue, BonusAction; //define variables
double dfacevalue = 0;
ifstream infile; //open data file
infile.open("C++ Data File.txt" );
if (!infile) //if data file doesnt exist, print "No Data File"
cout << "No data file!" << endl;
cout << "Face value" << endl;
while (infile) // gets all the lines in that data file
{
getline(infile, sfacevalue); // saves the line in sfacevalue
cout << sfacevalue << endl; // prints facevalue
}
sfacevalue = dfacevalue; //converts from string to double
if (dfacevalue <= 0.35) //calculations
BonusAction = "tripled" ;
else if (dfacevalue > 0.35 || dfacevalue < 0.51)
BonusAction = "doubled" ;
else if (dfacevalue >= 0.51 || dfacevalue < 1.00)
BonusAction = "value increased" ;
while (dfacevalue)
{
cout << dfacevalue;
}
system("pause" );
}
And here is how the output should look:
Face value Bonus action Actual value
0.49 doubled $0.98
0.28 tripled $0.84
0.74 value increased $1.00
0.34 tripled $1.02
0.50 doubled $1.00
Nov 11, 2014 at 5:37pm Nov 11, 2014 at 5:37pm UTC
1 2 3 4 5 6
ifstream infile;
infile.open("C++ Data File.txt" );
double d;
infile >> d;
cout << 2*d;
Topic archived. No new replies allowed.