In my search function, I'm trying to use the user input and print out the number associated with it. /* This is literall

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

In my search function, I'm trying to use the user input and print out the number associated with it. /* This is literall

Post by answerhappygod »

In my search function, I'm trying to use the user input and print out the number associated with it. /* This is literally my code and the question is with my code, what does "Code irrelevant" even mean?? *//
This is my code:
#include <iostream>#include <fstream>#include <vector>#include <string>
using namespace std;
// A struct State that stores the state names and total birthstruct State {string names;int birth;};
// Function that reads in the file and stores the data into a vectorvoid read_in( vector<State>& states, int& births){ifstream myfile;myfile.open("Student5.txt");// Check if the file is valid and opensif (!myfile.is_open()) {cout << "File did not open." << endl;exit(1);}string name, line;string name_C, birth_C;//Store the column headers as string variables.getline(myfile,name_C,',');getline(myfile,birth_C,'\n');// while it is not the end of the filewhile (!myfile.eof()) { getline (myfile, name, ','); // read in the names seperated by commas myfile >> births; // read in the birth as line State s = {name, births}; states.push_back(s); //store the info in a state vector //states.clear(); } myfile.close();}
//This function checks for the state with the lowest birthsint state_births(vector<State>& states, int& births, int& low) {
for (int i = 1; i < states.size(); i++) { if (states.at(i).birth < states.at(low).birth) { low = i; } //state s.clear();}return 0;}
// this function returns the total birth of all vowel statesvoid vowel_states(vector<State>& states, int& births, int& total) {char let;for (int i = 0; i < states.size(); i++) { let = states.at(i).names.at(0);// if any states that start w/ a vowel, add the births if (let == 'A' || let == 'E' || let == 'I'|| let == 'O' || let == 'U') { total = total + states.at(i).birth; } }states.clear();}
// This function searchs for the birth in a specific statevoid search(string& filename, string& input, int& value) {ifstream myfile(filename); if (myfile.is_open()) { while (myfile >> input >> value) { cout << value << endl; }} }
int main() { vector<State>states; int births, total,value, low=1; string filename = "Student5.txt"; int userInput = 0; string input; read_in(states,births); cout << "Hello, would you like to know the State with the lowest births or would you like to know the total birth of states starting with a vowel?" << endl;cout << "Enter '1' to know the lowest birth." << endl;cout << "Enter '2' to know the total birth." << endl;cout << "Enter '3' to know specific" << endl;cout << "What would you choose?: ";cin >> userInput;if(userInput == 1) {state_births(states, births,low);cout << states.at(low).names << " was the State with the least amount of births." << endl;cout<< "Total brith count was: " << states.at(low).birth << endl;} else if (userInput == 2) {vowel_states(states, births, total);cout << "Total state births for states that start with a vowel: " << total << endl;} else if(userInput == 3) { cin >> input; search(filename, input, value); cout << value << endl;}else {cout << "Invalid entry. Exiting the program.";} states.clear();return 0;}
This is the txt file :
Name,BirthsAlabama,57761Alaska,10086Arizona,80723Arkansas,37018California,454920Colorado,62885Connecticut,34725Delaware,10621District of Columbia,9212Florida,221542Georgia,126172Hawaii,16972Idaho,21403Illinois,144815Indiana,81646Iowa,37785Kansas,36261Kentucky,53922Louisiana,59615Maine,12311Maryland,71080Massachusetts,69109Michigan,110032
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply