data from txt file into array

#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <stdio.h>
#include <ctype.h>
#include <iomanip>
#include <algorithm>
#include <cstdlib>
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, maxBAC = 0, minPenalty = 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;
double bac[50], penalty[50], searchState, state;

// 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;
inFile.open ("stateLaws.txt");

if (inFile.fail( ))
{
cout << "Input file opening failed.\n";
exit(1);
}

while(! inFile.eof())
{
inFile >> state;
inFile >> bac[count];
inFile >> penalty[count];
count++;
}
for (int i = 0; i < 50; i++)
{
if(searchState == bac[i])
{
maxBAC = i+1;
break;
}
if(searchState == penalty[i])
{
minPenalty = i+1;
break;
}
}

******
// 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 << 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;
}



LA .20 45
ME .15 90
MD .15 90
MA .20 90

I am having a problem with my code... I figured I would post the whole code just to make it easier for everyone to understand what I was doing (put a line of asterisks above and below the code I believe is causing the issues) . Fairly simply BAC calculator, and I am trying to get data from a text tile, which you can see a sample of just below the code. I am attempting to store the information in an array which can be displayed with the final calculation. It compiles fine, but when I get to the part where I enter the state, I get nothing back. I would appreciate any suggestions/help anyone can provide.
Please use code tags and properly format your code before posting. If you don't know how to use code tags then reed this article.
http://www.cplusplus.com/articles/z13hAqkS/

Your not inputting the data from your file properly. The variable state is declared as a double, but you are trying to input a string into it. Try declaring your state variable as a string. I don't think this will solve all your problems but it should get you started.
Last edited on
I apologize for the formatting of my code. I changed the variable from double to string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
while(! inFile.eof())
{
	inFile >> state[count];
    inFile >> bac[count];
    inFile >> penalty[count];
    count++;
}
for (int i = 0; i < 50; i++)
{
	if(searchState == state[i])
	{
		maxBAC = i+1;
		break;
	}
		if(searchState == state[i])
		{
		minPenalty = i+1;
	break;
		}
}


As you will see here I changed the variable from maxBAC and maxPenalty to state, but the values I got were not correct.

Your Blood Alcohol Content after drinking 3 beers at 4% over a 5 hour span is: 
   : -0.0205765
The maximum BAC before aggrevated DUI in MA is -858993460
The minimum penalty in MA is -8589993460
You are not legally impared
Press any key to continue...


Do you have any other suggestions for as how to proceed?
searchstate should also be a string. Also the if statement on line 15 will never execute, because of the break on line 13.
Last edited on
I made the changes you suggested, as you can see below
1
2
string bac[50], penalty[50], searchState, state[50];
	string maxBAC, minPenalty;


I modified the code also by eliminating the breaks and one of the if statements, however I am still not getting the correct output.
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
	ifstream inFile;
    inFile.open ("stateLaws.txt");
 
        if (inFile.fail( )) 
        { 
            cout << "Input file opening failed.\n"; 
            exit(1); 
        } 
 
while(! inFile.eof())
{
	inFile >> state[count];
    inFile >> bac[count];
    inFile >> penalty[count];
    count++;
}
for (int i = 0; i < 50; i++)
{
	if(searchState == state[i])
	{
		bac[i] == maxBAC;
		penalty[i] == minPenalty;
	}
}
 

The output is as shown below, where nothing returns rather than -858993460
Your Blood Alcohol Content after drinking 3 beers at 4% over a 5 hour span is: 
   : -0.0205765
The maximum BAC before aggrevated DUI in MA is 
The minimum penalty in MA is 
You are not legally impared
Press any key to continue...


I am unsure where to proceed
Topic archived. No new replies allowed.