c++ voting

How do I make a program to read from a file of 100 people (Age, Residence code - C - Colón, Name, Identity card), calculate how much was the voting population in the province of Colon for the last elections, and print a report (name and identity card)?
try to be more specific with exactly how the input looks and how exactly you want output to look like. It's important how the parts are separated (e.g. space-separated). For example, space-separated Input:

22 C John_Smith 1395175801
28 C Maria_Suarez 2482018014

If it all looks like that, then parts can easily be parsed based on spaces. Of course, it can get much trickier depending how the input looks (might need regexes on getline, etc.).

This is the general setup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>
#include <fstream>
using namespace std;

int main()
{
    int age;
    string code, full_name, id;
    ifstream ifs("myfile.txt");  // Make sure ifs is not null
    while (ifs >> age >> code >> full_name >> id)
    {
        // Append to your data structures...
    }
    ifs.close();
    return 0;
}
Last edited on
Sorry for responding really late, but the output itself has spacing in it like this:


22 C John Smith 1395175801
28 C Maria Suarez 248201801


And I'm kinda new to the file thing. It would be nice for a simple explanation of how it works plz.
Can you give an example of what the input looks like?
Hello DigiLei,

Before you can read a file you first have to know what the file looks like, how it is laid out. Does it look something like your output or is it different?

When I first was learning to deal with input and output files I found this code useful:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
std::string iFileName{ "" };  // <--- Put file name here.

std::ifstream inFile;

inFile.open(iFileName);

if (inFile.is_open())
{
	std::cout << "\n File " << iFileName << " is open" << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(2));  // <--- Needs header files chrono" and "thread".
}
else
{
	std::cout << "\n File " << iFileName << " did not open" << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread".
	exit(1);  // <--- Because there is no need to continue.
}

In the then part of the if statement you can comment out these lines when everything is working and you no longer need them.

When you have gained a better understanding of how everything works this code can be shortened. When that can happen you have learned something.

As dhayden has said without seeing the input file it is hard to advise on how to read it.

Based on the output you have shown I can see the need for five variables, If the input is set up right you may only need one variable for first and last name otherwise two variables will be needed. ick1 has a good example of what you can do except for "full_name" will only pick up the first name and leave the last name for the next variable.

Without more to work with all I can say is that when you open an input file you need to check to make sure it is open. This checking for an open file and good file stream is not as necessary with output files. If the file does not exist it will be created thus it is hard to have an output file stream fail on open, but this is the code I use for output files:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
std::string oFileName{ "" };  // <--- Put file name here.

std::ofstream outFile;

outFile.open(oFileName, std::ios::trunc | std::ios::ate);

if (outFile.is_open())
{
	std::cout << "\n File " << oFileName << " is open" << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(2));  // <--- Needs header files chrono" and "thread".
}
else
{
	std::cout << "\n File " << oFileName << " did not open" << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread".
	exit(1);  // <--- No need to go any farther, but not likely to get here.
}

The biggest difference here is line 5. I use "trunc" to clear the file before writing new information to it. This is useful when first writing and debugging the code so yo only see the most recent output. The "ate" means to append the file and is what works with "trunc". When you are finished with the program you should remove the "std::ios::trunc |" part and change "ate" to "app" so it will always open the file for append.

Post an example of or the whole input file, if small, and some code of what you have tried and we can go from there.

Hope that helps,

Andy
DigiLei wrote:
Sorry for responding really late, but the output itself has spacing in it like this:
22 C John Smith 1395175801
28 C Maria Suarez 248201801


Ok, so very similar to what I initially guessed, but spaces between First and Last names? You have to be very exact there -- could there also be a Middle name (always? sometimes? never?) This could be the difference between simple space parsing and more complicated regex parsing, as I mentioned initially.

If it's just First and Last name, with a space, then just separate out full_name in my example into two variables.

DigiLei wrote:
And I'm kinda new to the file thing. It would be nice for a simple explanation of how it works plz.


Can run it at https://repl.it/repls/PlushEsteemedFolder
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 <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;

struct Person
{
    Person()
    {
    }

    Person(int age, string code, string first_name, string last_name, string id) :
        age(age),
        code(code),
        first_name(first_name),
        last_name(last_name),
        id(id)
    {
    }

    int age;
    string code;
    string first_name;
    string last_name;
    string id;
};

int main()
{
    // Opens an in-file stream object.  You've probably seen streams before: 
    // This *out*puts something, using '<<' special operator:
    //    cout << something;   
    // 
    // This asks user to *in*put, using '>>' to a variable myvar:
    //    cin >> myvar;
    //
    // File streams are very similar.  This particular one, 
    //   ifstream, is only for reading.
#if 0
    string file_name("myfile.txt");
    ifstream ifs(file_name);
    if (!ifs)
    {
        cout << "Could not open file \"" << file_name << "\". Please check path!";
        return -1;
    }
#endif
    
    // For online demonstration purposes, I'm instead using a 
    //   *string* stream. It's really all very similar.  Comment this
    //   out and remove the #if block above to instead use file stream.
    const char* persons_text = R"LITERAL(
22 C John Smith 1395175801
28 C Maria Suarez 248201801
)LITERAL";
    istringstream ifs(persons_text);

    // Variables for the space-separated parts of each line in file.
    // We might need a completely different design if the file is not consistent.
    int age;
    string code, first_name, last_name, id;
    vector<Person> people;
    while (ifs >> age >> code >> first_name >> last_name >> id)
    {
        // Append to your data structures...
        people.emplace_back(age, code, first_name, last_name, id);
    }
#if 0
    ifs.close();
#endif

    // Show people found so far:
    cout << "People found so far:" << endl;
    for (auto& p : people)
        cout << p.age << " " << p.code << " \"" << p.first_name << 
                " " << p.last_name << "\" " << p.id << endl;

    return 0;
}


Last edited on
Topic archived. No new replies allowed.