#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
struct list{
double freq;
} ;
string getString(void) {
ifstream SecondFile ("text1.txt");
string test_string;
do
{
while(! SecondFile.eof() ) //this while loop gets the text from the fil\
e line by line
{
getline(SecondFile, test_string); //get the line and put it in the \
fileline
}
}
while(test_string.length() == 0);
return test_string;
SecondFile.close();
}
void Begin () {
string str1;
str1 = getString();
int index;
double number_frequency[26];
int number_count [26];
for(int index = 0; index < 26; index++)
{
number_frequency[index] = 0;
number_count[index] = 0;
}
int number_of_numbers = 0;
int test_string_length = static_cast<int>(str1.length());
for(int index = 0; index < test_string_length; index++)
{
char symbol = str1[index];
symbol = toupper(symbol);
if('A' <= symbol && symbol <= 'Z')
{
int number_index = symbol - static_cast<int>('A');
number_count[number_index]++;
number_of_numbers++;
}
}
if(number_of_numbers > 0)
for(int index = 0; index < 26; index++)
number_frequency[index] = static_cast<double>(number_count[index]) \
/ number_of_numbers;
ofstream out("test1.txt");
if(!out) {
cout << "Cannot open file.\n";
}
for(index = 0; index < 26; index++) {
out << setprecision(2) << fixed;
out << setw(2) << static_cast<char>(index + static_cast<int>('A'));
out<< setw(2) <<" fi: " << setw(2)<< number_count[index];
out<<setw(2) <<" Pi: "<<number_frequency[index] <<endl;
}
out<<"\n";
out<<"N:"<<number_of_numbers<< endl;
out<<"\n";
cout << str1 << endl; //write the fileline onto the screen
out.close();
system("pause");
}
int
main ()
{
cout << "** Jackpot game **\n";
cout << "The goal of this game is to guess a number. You will be ask to typ\
e\n";
cout << "a number (you have 5 guess)\n";
cout << "Jackpot will then tell you if this number is too big of too small \
compared to the secret number to find\n\n";
Begin();
return 0;
}
This post has been edited by JackOfAllTrades: 26 May 2010 - 06:37 AM
Reason for edit:: Added code tags.

New Topic/Question
Reply
MultiQuote










|