So I have the file of states, symbols, capital ect. Here' a snippet:
State Symbol Capital Pop Size Zone Nickname Bordering States 111111
0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999000000
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345
Alabama AL Montgomery 23 30 CST Heart of Dixie FL,GA,MS,TN
Alaska AK Juneau 48 1 AKST Last Frontier none
Arizona AZ Phoenix 15 6 MST Grand Canyon State CA,CO,NM,NV,UT
Arkansas AR Little Rock 32 29 CST Natural State LA,MO,MS,OK,TN,TX
California CA Sacramento 1 3 PST Golden State NV,OR,AZ
Colorado CO Denver 22 8 MST Centennial State KS,NE,NM,OK,UT,WY,AZ
Connecticut CT Hartford 29 48 EST Constitution State MA,NY,RI
Delaware DE Dover 45 49 EST First State MD,NJ,PA
Florida FL Tallahassee 3 22 EST/CST Everglade State GA,AL
Georgia GA Atlanta 8 24 EST Peach State NC,SC,TN,FL,AL
Hawaii HI Honolulu 40 43 HST Aloha State none
Idaho ID Boise 39 14 PST/MST Gem State MT,NV,OR,UT,WA,WY
Illinois IL Springfield 5 25 CST Land of Lincoln IN,KY,MO,WI,IA
Indiana IN Indianapolis 16 38 EST/CST Hoosier State IL,MI,KY,OH
Iowa IA Des Moines 30 26 CST Hawkeye State IL,WI,MO,NE,SD,MN
Kansas KS Topeka 34 15 CST/MST America's Bread Basket MO,NE,OK,CO
Kentucky KY Frankfort 26 37 EST/CST Bluegrass State MO,OH,TN,VA,WV,IL,IN
How can I read this file in so that i can prompt the user with a clue, and allow for them to guess the state or state symbol. My code written so far:
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
|
#include <ctime> // needed for the time( ) function
#include <cstdlib> // needed for the rand( ) and srand( ) functions
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
char rules;
string answer,answer1,state,sym,capital,pop,size,zone,nickname,bordering;
cout << "Welcome to my fourth programming project, name the state game!" << endl;
cout << "Would you like a list of the rules? (Y or N)" << endl;
cin >> rules;
if (rules == 'Y')
{
cout << "You will be given clues about a state and you must try to guess the correct answer." << endl;
cout << "If you guess an answer incorrectly, five points will be subtracted from your score." << endl;
cout << "If you would like another hint, 5 points will be subtracted from your score." << endl;
ifstream input;
input.open("States.txt");
if (input.fail())
{
cout << "Sorry, but the file doesn't exist!" << endl;
cout << "Exiting the program...";
return 0;
}
input.ignore(10000,'\n');
input.ignore(10000,'\n');
input.ignore(10000,'\n');
while (!input.eof())
{
input >> state >> sym >> capital >> pop >> size >> zone >>
nickname >> bordering;
}
int score=100;
const int b=5;
while (score > 0)
{
srand(time(0)); // include this or else rand( ) produces the same number each run
int Clue1 = rand( )%9; // produces a random number from 0 to 8
if (Clue1 == 0)
cout << "The first letter of the state is ___." << endl;
else if (Clue1 == 1)
cout << "The last letter of the state is ___." << endl;
else if (Clue1 == 2)
cout << "The first letter of the state capital is ___." << endl;
else if (Clue1 == 3)
cout << "The last letter of the state capital is ___." << endl;
else if (Clue1 == 4)
cout << "The state's rank in population, where 1 is the largest, is ___." << endl;
else if (Clue1 == 5)
cout << "The state's rank in size (or area), where 1 is the largest, is ___." << endl;
else if (Clue1 == 6)
cout << "The state is in the following time zone(s): ___" << endl;
else if (Clue1 == 7)
cout << "The state's nickname is ___;" << endl;
else if (Clue1 == 8)
cout << "The state is bordered by the following states: ___." << endl;
cin >> answer;
if (answer==answer1)
{
cout << "Great job, your score is: " << score << "!" << endl;
break;
}
else if (answer!=answer1)
score=score-b;
}
}
else if (rules == 'N')
{
cout << "Let's get started. A state has been randomly selected. Here is your first clue: " << endl;
}
}
|