Jul 11, 2013 at 11:29pm UTC
i have 2 tasks that i need to do and the first task is done. i have the user enter a number and it opens a txt file. now on the next task i need to have that txt file open and then have it read a line from it. i have tried using getline but it doesnt read the name on the first line.
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
//task 0 constants
const int SCREEN_WIDTH = 80;
const string NAME = "Ryan Dempsey" ;
const string COURSE = "CST 113 - Exercise 5" ;
//task 1
const double BONUS = 10.00;
int main(void )
{
//task 0
int centeredWidth;
int fileName;
//task 1
string customerName;
double totalAmountSpent;
double bonusBucks;
ifstream inFile;
ofstream Ex5Out;
centeredWidth = (SCREEN_WIDTH + NAME.length())/2;
Ex5Out.open("Ex5Out.txt" );
cout << setfill ('*' ) << setw(SCREEN_WIDTH+1) << ' ' << setfill (' ' ) << endl;
cout << setw(centeredWidth) << NAME << endl;
cout << setw(centeredWidth+4) << COURSE << endl;
cout << "Select the input file:" << endl;
cout << "1. Ex5-1.txt" << endl;
cout << "2. Ex5-2.txt" << endl;
cout << "3. Ex5-3.txt" << endl;
cout << endl << endl;
cout << "Enter the input file choice:" ;
cin >> fileName;
cout << endl << endl;
if (fileName == 1)
{
inFile.open("Ex5-1.txt" );
cout << "the file Ex5-1.txt was successfully opened" << endl;
}
else if (fileName == 2)
{
inFile.open("Ex5-2.txt" );
cout << "the file Ex5-2.txt was successfully opened" << endl;
}
else
{
inFile.open("Ex5-3.txt" );
cout << "the file Ex5-3.txt was successfully opened" << endl;
}
cout << setfill ('*' ) << setw(SCREEN_WIDTH+1) << ' ' << setfill (' ' ) << endl;
Ex5Out << setfill ('*' ) << setw(SCREEN_WIDTH+1) << ' ' << setfill (' ' ) << endl;
Ex5Out << setw(centeredWidth) << NAME << endl;
Ex5Out << setw(centeredWidth+4) << COURSE << endl;
Ex5Out << setfill ('*' ) << setw(SCREEN_WIDTH+1) << ' ' << setfill (' ' ) << endl;
//task 1
getline(inFile,customerName);
inFile >> totalAmountSpent;
cout << fixed << setprecision (2);
bonusBucks = 5.00;
if (totalAmountSpent > 500)
{
bonusBucks += BONUS;
}
cout << fixed << setprecision (2);
cout << setfill ('*' ) << setw(SCREEN_WIDTH+1) << ' ' << setfill (' ' ) << endl;
cout << "Task 1 - Total Spent and Bonus" << endl;
cout << "Name:" << customerName << endl;
cout << "Total Amount Spent:" << totalAmountSpent << endl;
cout << "Bonus:" << setw(17) << bonusBucks << endl;
cout << setfill ('*' ) << setw(SCREEN_WIDTH+1) << ' ' << setfill (' ' ) << endl;
inFile.close();
Ex5Out.close();
return 0;
}
Last edited on Jul 11, 2013 at 11:30pm UTC
Jul 12, 2013 at 4:49pm UTC
how many lines does your .txt have?
what is the output?
Jul 12, 2013 at 5:25pm UTC
i fixed it by the way i copied n pasted the text file. when i first did it i just added an existing file and found the txt file. when i just added a new file i added a new txt and copied n pasted the info and it worked.
Jul 12, 2013 at 5:27pm UTC
i have the first two lines from the text for task one.
now i need to third line which has two numbers like , 55 67.
how do i read the third line? do i use getline again or do i use ignore.