Struggling with coding for a assignment question

Write a program that will create userids for email addresses of a company. Names of employees will be contained in a file. Your task is to read the file character by character. Employee names are separated by semicolons. The process of creating a userid is as follows:

The first 8 characters, excluding spaces and non-alphabetical characters becomes the userid. Your program will therefore ignore all spaces and non-alphabetical characters from the input file. If the employee name excluding spaces and non-alphabetical characters consists of less than 8 characters, the userid will also contain less than 8 characters. (e.g. JG Smit will give a userid of jgsmit and GM Bezuidenhout will give a userid of gmbezuid.

You will however not work with the full surname. Your program will read character by character, and form the userid as you go along, adding each new character of the id to the output file. Call the output file userid.dat. As soon as you read a semicolon, the previous userid is complete and you can then write the semicolon to the output file to separate the userids. You therefore have to plan the logic properly, so that you don’t have to go back to the output file to delete characters that should not be there.

Create an input file called employee.dat with the following data:

SS van der Merwe;PJ Ferreira;HW du Plessis;DF Kodisang;AA
Papoudopolous;G Mhlanga;TRF Schoeman;LJ Marais Le Roux;CG
Roux;B Nicholaidis;TT Tshabalala;RV Mississipi;

Allow the user to specify the name of the input file.

That is my question, the part where i struggle and have no idea what coding to use as i cant see that this has been covered yet in our syllabus, is where you have to read the name and surname between the semicolon from the input file
Last edited on
Can you give an example of how the input file is structured?
i have to create the input file with the extension .dat with the following data inside

SS van der Merwe;PJ Ferreira;HW du Plessis;DF Kodisang;AA
Papoudopolous;G Mhlanga;TRF Schoeman;LJ Marais Le Roux;CG
Roux;B Nicholaidis;TT Tshabalala;RV Mississipi;
i updated the first post, that is the entire question as is on my assignment
You could use std::getline(). There is a version of getline that accepts a delimiter character, which in your case, would be ';'.

This seemed to work for me.
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
// Example program
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
#include <sstream>
int main()
{
        
    std::istringstream iss{"George Washington;John Doe;J K Rowling;Test This Name;"};
    std::string user_id{}, name{};
    unsigned char_count{};
    std::vector<std::string> id_vector, name_vector;
    while(std::getline(iss, name, ';'))
    {

        for(char i : name)
        {
            if(isspace(i)) continue;
            ++char_count;
            user_id.push_back(i);
            if(char_count == 8) break;
        }
        id_vector.push_back(user_id);
        name_vector.push_back(name);
        std::string{}.swap(user_id);
        char_count = 0;
    }

for(auto&& i : name_vector) std::cout << i << std::endl;
for(auto&& i : id_vector) std::cout << i << std::endl;

}


Now, you have to figure out how to modify it to accept filestreams and make the characters lowercase
i shall try, in all honesty i have never heard of sstream, but i guess now is a good time as any lol
also i want to know is there anyway i can link an input string such as

char InputFileName

cout << "Enter the name of the file you want to open";

cin >> InputFileName

The InputFileName regardless of what is entered by the user needs to be linked to a input file i created called employee.dat so that the program understands whenever InputFileName is referred to in the program that it will know to use the employee.dat
You don't need to use sstream. I only used sstream to give you an example since I can't post actual files here. You would need to replace sstream with a fstream object and filename.
Topic archived. No new replies allowed.