string stream search

I am working on a project which is a fairly simply BAC calculator. I am attempting to grab data from a text file, which contains state abbreviation, maximum BAC before aggravated, and minimum license suspension. The text file looks like


AL .15 90
MA .20 90
ect
ect


I am trying to search for the state abbreviation based upon user entry, and then pull the abbreviation as well as the 2 numbers next to it.

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>  
#include <fstream>
#include <string> 
#include <cmath>
#include <stdio.h>
#include <ctype.h>
#include <iomanip>
#include <algorithm>
#include <cstdlib>
#include <sstream>
using namespace std; 

 float MALE_RATE = 0.68, FEMALE_RATE = 0.55, LEGAL_LIMIT = 0.08;  
	//percent of body weight which holds alcohol
	

int main ()
{

	char gender;
	int weight, count = 0;
	std::string alcoholType;
	std::string alcoholType1 = "beer";
	std::string alcoholType2 = "wine";
	std::string alcoholType3 = "liquor";
	float genderRate, drinks, percent, BAC, time, alcoholWeight, metabolism, weightGender, alcoholPercent, alcoholTotal;

	string maxBAC, minPenalty, searchState, line;
	
	// gather user input
	cout << "Enter the following " << endl << endl;
	cout << "Gender (M) or (F): "; 
	cin >> gender;
	cout << "\n" "Weight in pounds: ";
	cin >> weight;
	cout << "\n" "What type of alcohol have you been drinking (Beer, Wine, Liquor): ";
	cin >> alcoholType;
	cout << "\n" "You typed " << alcoholType << endl;
	cout << "\n" "Number of drinks: ";
	cin >> drinks;
	cout << "\n" "Percentage of alcohol consumed: ";
	cin >> percent;
	cout << "\n" "Hours spent drinking: ";
	cin >> time;
	cout << "\n" "State (ex: AL, MA, NY): ";
	cin >> searchState; 

	if (alcoholType == alcoholType1)
	{
		alcoholPercent = (drinks * 12) * (percent / 100);
	}
	else if (alcoholType == alcoholType2)
	{
		alcoholPercent = (drinks * 5) * (percent / 100);
	}
	else if (alcoholType == alcoholType3)
	{
		alcoholPercent = (drinks * 1.5) * (percent / 100);
	}
	else
	{
		cout << "Invalid response, enter 'beer,' 'wine,' or 'liquor.'" << endl;
	}

	if (gender == 'M' || gender == 'm')
	{
	genderRate = MALE_RATE; 
	}
	else if(gender == 'F' || gender == 'f') 
	{
	genderRate = FEMALE_RATE;
	}
	else 
	{
	cout << "Invalid entry, please enter 'M' or 'F'" << endl;
	}
 

	alcoholWeight = alcoholPercent * 5.14; 

	weightGender = weight * genderRate; 

	metabolism = .015 * time; 

	alcoholTotal = alcoholWeight / weightGender; 

	BAC = alcoholTotal - metabolism; 


	ifstream inFile("stateLaws.txt");
	stringstream ss; 

   getline(inFile, line);
   ss << line;
   ss >> searchState >> maxBAC >> minPenalty;
   ss.clear();
	

	// the program can now output the result
	cout << endl << endl << endl; // skip some spaces
	cout << "Your Blood Alcohol Content after drinking " << drinks <<" "
		 << alcoholType << " at " << percent << "% " << "over a "
		 << time << " hour span is: " << endl << endl << "  : "
		 << BAC << " BAC." <<endl <<endl;
	cout << "The maximum BAC before aggrevated DUI in " << searchState << " is " << maxBAC << endl;
	cout << "The minimum penalty in " << searchState << " is " << minPenalty << " day suspension of license" << endl;


	if (BAC > LEGAL_LIMIT) 
		cout << "You are legally impaired and should not drive" << endl;
	else
		cout << "You are not legally impared" << endl;

	return 0;
}

//function defintion 


The code I am struggling with is the getline and stringstream which is near the bottom. Any suggestions as to what I am doing wrong would be greatly appreciated.

EDIT:
The output I am getting is


The maximum BAC before aggrevated DUI in AL is .15
The minimum penalty in AL is 90 day suspension of license


I get that for every state I type. I know it is reading the first line, but something in my code does not allow it to read the other lines. Please help!!
Last edited on
You're only reading the first line of the file. The getline needs to be in a loop.

But you don't need getline() or the stringstream. Try this:

1
2
3
4
5
6
ifstream inFile("statelaws.txt");
std::string state;
while(inFile){
    inFile >> state >> maxBAC >> minPenalty;
    if(state==searchState) break;
}


Here is a good tutorial: http://www.learncpp.com/cpp-tutorial/136-basic-file-io/
Last edited on
Topic archived. No new replies allowed.