A little help for a beginner programmer.

I am in my last weeks of class and I am working on a program that I have all working except one thing, and for the life of me can not figure it out. We are working with <fstream> and sequential access files. I am supposed to extract data from 4 different variables and have it all totalled in a report style document. I am only going to post the area that I am having the problem at. What is happening in the program is it is reading the first line of coding, and skipping over all the other codes. And I need it to address each line of code, so that it removes it from the dat file. Hope that makes some since. This is the very first C++ class I have been in, and have made it up until now. Any help would greatly be appreciated.

//Open the data file
ifstream salesIn;
salesIn.open("ssw-sales.dat", ios::in);
if (salesIn.is_open())
{
//Headers
cout <<"#" <<setw(10)<<"Type"<<setw(10)<<"QTY"<<setw(15)<<"Coupon"<<setw(20)<<"Actual Chg"<<setw(15)<<"Delta"<<endl;
cout <<"*" <<setw(10)<<"****"<<setw(10)<<"***"<<setw(15)<<"******"<<setw(20)<<"**********"<<setw(15)<<"*****"<<endl;

//Prime read: read the first record of the data file
salesIn >> ticketDate
>> ticketType
>> ticketQty
>> coupon
>> ticketCost;


//Begin the central record-processing while loop
while (!salesIn.eof())
{
lineNum = ++lineNum;
cout << lineNum << setw(10)<<getTicType(ticketType, TicType) << setw(10)<<ticketQty<<setw(15)<< coupon <<setw(20)<<ticketCost<<setw(15)<<getdelta(ticketType, dvalue, ticketQty, ticketCost, coupon, adultTic, juniorTic)<<endl;


//This part works fine on the program.
if (ticketType == 'A')
totalAdltTic = totalAdltTic + ticketQty ;
if (ticketType == 'J')
totalJunTic = totalJunTic + ticketQty;
if (ticketType == 'C')
totalTodTic = totalTodTic + ticketQty;
//end if
//This part is the part that reads the first line, and skips over all the others.
if (ticketType == 'A' && coupon == 'N')
adultSales = ticketQty * adultTic;
// end if
if (ticketType == 'A' && coupon == 'Y')
adultCsales = ticketQty * (adultTic - (adultTic * .25));
// end if
if (ticketType == 'J' && coupon == 'N')
juniorSales = ticketQty * juniorTic;
// end if
if (ticketType == 'J' && coupon == 'Y')
juniorCsales = ticketQty * (juniorTic - (juniorTic * .25));
// end if


I thought as the program was running within the while statement it would have evaluated each line independant of each other. I am wrong. I thought about using case select, but I can not use the particular variables needed. Any suggestions.
Please use the [ code ] ...code... [ /code ] tages when you paste code, it's saves indent too, so it's easier to read! :D

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
//Open the data file
ifstream salesIn;
salesIn.open("ssw-sales.dat", ios::in);
if (salesIn.is_open())
{
//Headers
cout <<"#" <<setw(10)<<"Type"<<setw(10)<<"QTY"<<setw(15)<<"Coupon"<<setw(20)<<"Actual Chg"<<setw(15)<<"Delta"<<endl;
cout <<"*" <<setw(10)<<"****"<<setw(10)<<"***"<<setw(15)<<"******"<<setw(20)<<"**********"<<setw(15)<<"*****"<<endl;

//Prime read: read the first record of the data file
salesIn >> ticketDate
>> ticketType
>> ticketQty
>> coupon
>> ticketCost;


//Begin the central record-processing while loop
while (!salesIn.eof())
{
lineNum = ++lineNum;
cout << lineNum << setw(10)<<getTicType(ticketType, TicType) << setw(10)<<ticketQty<<setw(15)<< coupon <<setw(20)<<ticketCost<<setw(15)<<getdelta(ticketType, dvalue, ticketQty, ticketCost, coupon, adultTic, juniorTic)<<endl;


//This part works fine on the program.
if (ticketType == 'A')
totalAdltTic = totalAdltTic + ticketQty ;
if (ticketType == 'J')
totalJunTic = totalJunTic + ticketQty;
if (ticketType == 'C')
totalTodTic = totalTodTic + ticketQty;
//end if
//This part is the part that reads the first line, and skips over all the others.
if (ticketType == 'A' && coupon == 'N')
adultSales = ticketQty * adultTic;
// end if
if (ticketType == 'A' && coupon == 'Y')
adultCsales = ticketQty * (adultTic - (adultTic * .25));
// end if
if (ticketType == 'J' && coupon == 'N')
juniorSales = ticketQty * juniorTic;
// end if
if (ticketType == 'J' && coupon == 'Y')
juniorCsales = ticketQty * (juniorTic - (juniorTic * .25));
// end if  


As to why you program skips all after the if statement, all I can think of, is that seems to keep returning true? Have you made sure this isn't the case?
Actually each of the if's are going to be true. The dat file that I am pulling from is using each of the variables, "A" for adult, "J" for junior, "Y" for if customer has a coupon, and "N" if they don't. Maybe my logic is off, I am still learning. The file is reading in sequential access and and pulling the data from the file until it reaches the end. I assumed that the program would evaluate each line and take the information from the dat file and process it individually, and then store adultsales, adultCsales, juniorsales, and juniorCsales with the information extracted from the dat file.
I'm guessing the following are strings? :
1
2
3
4
5
salesIn >> ticketDate
>> ticketType
>> ticketQty
>> coupon
>> ticketCost;


If so, use the getline() function:
1
2
3
4
5
6
7
getline( salesIn, ticketDate, '\t' );
getline( salesIn, ticketQty, '\t' );
getline( salesIn, coupon, '\t' );
getline( salesIn, ticketCost, '\t' );

//You may need this to go to the next line in the file:
getline( salesIn, tempString, '\n' ) ;


The '\t' is a horizontal tab. For this, your data int the file, on the same line, would have tabbed spaces in between each bit of information.

The '\n' is for a new line.

Here is some more info on getline:
http://msdn.microsoft.com/en-us/library/3ah895zy%28v=vs.80%29.aspx
http://msdn.microsoft.com/en-us/library/2whx1zkx%28v=VS.71%29.aspx
Last edited on
Topic archived. No new replies allowed.