Alright, so the assignment which im sure you guys have seen around alot is to do a powerball type program, 1-55 for the first 5 numbers then the 6th 1-42.

I set up the program, i get my random numbers. but the teacher wants you to assign a single variable and not use global variables.

Im not too sure whats going wrong but i outFiled and inFiled my code and i get a result each time but it DOES NOT save. im so baffled becuase my friend wrote his and it saves perfectly fine.

here is what ive written so far .

any help is appreciated thank you!

#include <iostream>
#include <ctime>
#include <cstdlib> 
#include <fstream>
using namespace std;

int main()
{
	ofstream outFile;

	outFile.open("powerball2.txt");

	outFile <<"** Powerball **"<<endl<<endl;
	
	srand(int(time(0)));

	outFile << 1+rand()%55 <<endl;
	outFile << 1+rand()%55 <<endl;
	outFile << 1+rand()%55 <<endl;
	outFile << 1+rand()%55 <<endl;
	outFile << 1+rand()%55 <<endl;
	outFile << 1+rand()%42 <<endl<<endl<<endl;
outFile.close();

ifstream inFile;
	
	inFile.open("powerball2.txt");
	inFile >> 1+rand()%55 <<endl;
	inFile >> 1+rand()%55 <<endl;
	inFile >> 1+rand()%55 <<endl;
	inFile >> 1+rand()%55 <<endl;
	inFile >> 1+rand()%55 <<endl;
	inFile >> 1+rand()%42 <<endl;

	inFile.close();


	system("pause");
	return 0;
}

Recommended Answers

All 3 Replies

lines 28-33 are wrong. You can not read a file into a const value, but use variables

int arry[6];
for(int i = 0; i < 6; i++)
   inFile >> arry[i];

Is anymore of it wrong? i replaced 28-33 with what you said, but its still doing the same thing. not saving a text document but still out putting random numbers like its supposed to.

Does windows 7 have something to do with this ? im totally lost. shouldnt it be saving something regardless?

hah yay i figured it out, i guess it helps to read the book a bit. i ended up with.

#include <iostream>
#include <ctime>
#include <cstdlib> 
#include <fstream>
using namespace std;

int main()
{
	ofstream outFile;

	const int SIZE = 81;
	char name[SIZE];

	outFile.open("powerball2.txt");

	outFile <<"** Powerball **"<<endl<<endl;
	
	srand(int(time(0)));

	outFile << 1+rand()%55 <<endl;
	outFile << 1+rand()%55 <<endl;
	outFile << 1+rand()%55 <<endl;
	outFile << 1+rand()%55 <<endl;
	outFile << 1+rand()%55 <<endl;
	outFile << 1+rand()%42 <<endl<<endl<<endl;
outFile.close();

ifstream inFile;
	
	inFile.open("powerball2.txt");

	inFile >> name;
	cout << name;

	inFile >> name;
	cout << name;

	inFile >> name;
	cout << name<<endl;

	inFile >> name;
	cout << name <<endl;

	inFile >> name;
	cout << name <<endl;

	inFile >> name;
	cout << name <<endl;

	inFile >> name;
	cout << name <<endl;

	inFile >> name;
	cout << name <<endl;

	inFile >> name;
	cout << name <<endl;

	inFile.close();


	system("pause");
	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.