What's the best way to read this file in for use?

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;
		
	}	
}
Last edited on
Did you create your input file? Can it be modified?

Is this actually part of the file?
1
2
3
State Symbol Capital Pop Size Zone Nickname Bordering States 111111
0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999000000
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345


Are the fields separated by a space character or something like a tab character?
That is actually part of the file, and I'm not allowed to edit it. I just read the first 3 lines as junk to get rid of it. Everything is separated by a random number of spaces. he gave us this hint:
Note that rows 1-3 provide headings and column numbers. The other rows are organized as follows:
• State - columns 0 - 14
• State Symbol - columns 15 - 16
• …
• Bordering States - columns 83 - (end of line)
Last edited on
Before you read the file you should think how to store the data.

One option would be a to use a struct or class to hold the data for one state and a vector for all states.

I also would divide the program into functions.
One funtion for the introduction, one for loading the data, one for the actual game.
Are you sure that they are spaces and not the tab character? Or are the fields a fixed width?

Perhaps you could repost your sample file inside code tags to try to preserve the format.

closed account (E0p9LyTq)
You already know the file's formatting. Open the file, read each complete line (getline), discard the first three lines and store the rest individually in a vector<string>. After reading the data close the file.

How you parse the chosen string in the vector to get your clue is up to you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
Louisiana      LA      Baton Rouge    25   31    CST      Pelican State            MS,TX,AR
Maine          ME      Augusta        41   39    EST      Pine Tree State          NH 

Great idea to post them in tags, didn't think of that.
Last edited on
I'm pretty new to this, tried studying up on classes but i don't understand them.
just used this to read in my file completely:
1
2
3
4
5
while (!input.eof())
		{
			getline(input, line);
			cout << line << endl;
		}

Is this a step in the right direction?
Last edited on
Okay it looks like the file is using fixed sized fields. You will need to use this fact to parse each line in the file.

I would first use getline() to read the entire line into a string then use a stringstream, a character buffer, and getline() to retrieve each item from the string.

Something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
...
struct State_information
{
   string state;
   string state_abrev;
};
...
   std::vector<State_information> states(50); 
   std::string line;
   size_t counter = 0;
   while(getline(inputFileStream, line))
   {
      char buffer[200]; // Create a buffer large enough to hold the largest field.
      stringstream sin(line); // Create a stringstream from the line.
      sin.getline(buffer,16); // Get the state name. (note: may be off by 1).
      states[counter].state = buffer;
      sin.getline(buffer, 9);
      states[counter].state_abrev = buffer;
      ...
      counter++;
...

And instead of the "magic numbers" 200, 16, and 9 you could use named variables instead.
Topic archived. No new replies allowed.