Question #85859

Read the results of the election into the array from a text file where each line has the city name, district name and the number of votes the three parties received, below:
city1 district1 232000 145000 13450
cityt2 district2 123500 217800 150000
city1 district3 10500 23000 5500
city3 district4 120450 113000 203000
After the array has been read, do the following according to the user’s input:
List: Input the city name, and list all the information about the districts of the city.
District: Input a district’s name and output which party has won the election in that district
City: Input a city’s name and output which party won the election in that city
Quit: End the program
Run
List | District | City | Quit
Enter your choice of action: List
Enter city name: city1
City: city1
District: district1 Party1: 2320 Party2: 1450 Party3: 13450
District: district3 Party1: 105 Party2: 230 Party3: 5500
List | District | City | Quit
Enter your choice of action: Quit

Expert's answer

Answer to Question #85859 - Programming & Computer Science - C++

Question 85859:

Read the results of the election into the array from a text file where each line has the city name, district name and the number of votes the three parties received, below:

city1 district1 232000 145000 13450

city2 district2 123500 217800 150000

city1 district3 10500 23000 5500

city3 district4 120450 113000 203000

After the array has been read, do the following according to the user's input:

List: Input the city name, and list all the information about the districts of the city.

District: Input a district's name and output which party has won the election in that district

City: Input a city's name and output which party won the election in that city

Quit: End the program

Run

List | District | City | Quit

Enter your choice of action: List

Enter city name: city1

City: city1

District: district1 Party1: 2320 Party2: 1450 Party3: 13450

District: district3 Party1: 105 Party2: 230 Party3: 5500

List | District | City | Quit

Enter your choice of action: Quit.

Answer:

#include <iostream>
#include <fstream>
void printWinner(unsigned party1, unsigned party2, unsigned party3) {
    if (party1 > party2 && party1 > party3)
        std::cout << "Winner: Party1\n";
    else if (party2 > party1 && party2 > party3)
        std::cout << "Winner: Party2\n";
    else if (party3 > party1 && party3 > party2)
        std::cout << "Winner: Party3\n";
    else if (party1 == party2 && party1 > party3)
        std::cout << "Winner: Party1 and Party2\n";
    else if (party1 == party3 && party1 > party2)
        std::cout << "Winner: Party1 and Party3\n";
    else if (party2 == party3 && party2 > party1)
        std::cout << "Winner: Party2 and Party3\n";
    else
        std::cout << "Winner: Party1 and Party2 and Party3\n";
}
int main() {
    std::ifstream input("election.txt");
    // input.open("election.txt");
    if (!input) {
        std::cerr << "File was not opened" << std::endl;
        return 1;
    }
    struct Result {
        std::string city;
        std::string district;
        unsigned int party1;
        unsigned party2;
        unsigned party3;
    } results[4];
    int count = 0;
    while (count < 4 && input) {
        input >> results[count].city;
        input >> results[count].district;
        input >> results[count].party1;
        input >> results[count].party2;
        input >> results[count].party3;
        count++;
    }
    if (!input) {
        // if (input.fail()) {
        std::cerr << "File was not loaded" << std::endl;
        return 1;
    }
    input.close();
    bool quit = false;
    std::string action;
    while (!quit) {
        std::cout << "List | District | City | Quit\nEnter your choice of action: ";
        std::cin >> action;
        if (!std::cin) {
            std::cerr << "Input was not processed" << std::endl;
            return 1;
        }
        if (action == "List") {
            std::cout << "Enter city name: ";
            static std::string city;
            std::cin >> city;
            if (!std::cin) {
                std::cerr << "Input was not processed" << std::endl;
                return 1;
            }
            std::cout << "City: " << city << std::endl;
            for (int i = 0; i < count; i++)
                if (results[i].city == city)
                    std::cout << "District: " << results[i].district
                              << " Party1: " << results[i].party1
                              << " Party2: " << results[i].party2
                              << " Party3: " << results[i].party3 << std::endl;
        } else if (action == "District") {
            std::cout << "Enter district name: ";
            static std::string district;
            std::cin >> district;
            if (!std::cin) {
                std::cerr << "Input was not processed" << std::endl;
                return 1;
            }
            std::cout << "District: " << district << std::endl;
            for (int i = 0; i < count; i++)
                if (results[i].district == district) {
                    std::cout << "City: " << results[i].city << " ";
                    printWinner(results[i].party1, results[i].party2,
                               results[i].party3);
                }
        } else if (action == "City") {
            std::cout << "Enter city name: ";
            static std::string city;
            std::cin >> city;
            if (!std::cin) {
                std::cerr << "Input was not processed" << std::endl;
                return 1;
            }
            std::cout << "City: " << city << std::endl;
            for (int i = 0; i < count; i++)
                if (results[i].city == city) {
                    std::cout << "District: " << results[i].district << " ";
                    printWinner(results[i].party1, results[i].party2,
                               results[i].party3);
                }
        } else if (action == "Quit")
            quit = true;
        else
            std::cout << "Incorrect choice" << std::endl;
    }
}


Answer provided by www.AssignmentExpert.com

LATEST TUTORIALS
APPROVED BY CLIENTS