how to output to a .txt file

Hie, Here is my code. I am having trouble placing the ofstream in the following location where i want to display the message on a text file "one of the tickets is a winner this week" and "not one of the tickets is a winner this week."

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
#include<iostream>
#include<vector>
#include <fstream>

using namespace std;
void ticketSort(vector<int>&);
bool search(vector<int>,int);

int main()
{
ofstream output;
vector<int> lotteryList;

lotteryList.push_back(13579);
lotteryList.push_back(26791);
lotteryList.push_back(26792);
lotteryList.push_back(33445);
lotteryList.push_back(55555);
lotteryList.push_back(62483);
lotteryList.push_back(77777);
lotteryList.push_back(79422);
lotteryList.push_back(85647);
lotteryList.push_back(93121);


int winningNumber;

cout<<"Enter this week's winning number ";
cin>>winningNumber;


ticketSort(lotteryList);
output.open("C:\\CS140\\Spring2013\\lotto.txt");
if(search(lotteryList,winningNumber)) 
	cout<<"One of the tickets is a winner this week."<<endl;
else 
	cout<<"Not one of the tickets is a winner this week."<<endl;

output.close();
system("pause");
return 0;
}

void ticketSort(vector<int>& ticket)
{

int size=ticket.size();
int startScan,minIndex,tempTicket,minTicket;
for(startScan=0;startScan<(size-1);startScan++)
{
minIndex=startScan;
minTicket=ticket[startScan];
for(int index = startScan+1;index<size;index++)
{
if(minTicket>ticket[index])
{
minTicket=ticket[index];
minIndex=index;
}
ticket[minIndex]=ticket[startScan];
ticket[startScan]=minTicket;
}
}

}

bool search(vector<int> list,int winNumber)
{
bool found=false;
int first=0,
last=list.size()-1,
middle;
while(!found&&first<=last)
{
middle=(first+last)/2;
if(list[middle]==winNumber) found=true;
else if(list[middle]>winNumber) last=middle-1;
else first=middle+1;
}
return found;
}



The codes are on line 2,11,33,39
Can someone tell me which line and what to fix ?
Last edited on
Instead of using cout, you just need to use your file stream.
Topic archived. No new replies allowed.