Reading characters from a file

Hi I am really struggling with this question. I have tried everything but I'm failing:

Write a program that will create user ids 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 user id is as follows:

The first 8 characters, excluding spaces and non-alphabetical characters becomes the user id. 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 user id as you go along, adding each new character of the id to the output file. Call the output file userid.dat. 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 employee.dat with the following data:

SS van der Merwe;PJ Ferreira;HW du Plessis;DF Kodisang;
Show the "everything". If you don't, then we cannot point out what was your mistake.
Hello thamsa,

In case you are not familiar with this:


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



This may help you get started:

Write a program that will create user ids for email addresses of a company.

Names of employees will be contained in a file.

2. Your task is to read the file character by character.
   Employee names are separated by semicolons.

The process of creating a user id is as follows:

3. The first 8 characters, excluding spaces and non-alphabetical characters becomes the user id.

   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.

4. Your program will read character by character, and form the user id 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.

1. 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 employee.dat with the following data:

SS van der Merwe;PJ Ferreira;HW du Plessis;DF Kodisang;


Work on each step one at a time until it is working the way that you want. Compile and test often. I would start by reading each character and when you find a ";" print a new line and continue with the next name. Once you get that far you can add to the code to check the character read to see if it fits the requirements.

Post the code that you are having problems with. Or just to show what you have done to get any suggestions.

Andy
Hello thamsa,

Given the instructions:

You will however not work with the full surname.


And given this is what is between the ";"s:

SS van der Merwe
PJ Ferreira
HW du Plessis
DF Kodisang


 Press Enter to continue:


What part of these names is to be considered the surname, if any?

To me it looks like the "userid" will be made up using the first two letters and the rest is the surname. Am I understanding this correctly?

Andy
No. The first 8 characters, excluding spaces and non-alphabetical characters becomes the user id.
Hello thamsa,

Thank you. That is what I thought.

I have part of the code working, but now is the time to post what youhave done so far and see what does not work.

Andy
Pseudocode:
open an input stream
open an output stream

set currentLength to 0

while you can read a char from input:
   if the char is a semicolon:
      write a semicolon to output
      set currentLength to 0;
   else if it's a letter and currentLength is less than 8:
      write the lowercase letter to output
      increment current length
Hello thamsa,

I managed to get a working program that creates the file:

SSvander@someshere.com

PJFerrei@someshere.com

HWduPles@someshere.com

DFKodisa@someshere.com


The "@someshere.com" and blank line are optional. The "@someshere.com" part I thought looked like a good idea. I was thinking that the blank line might be useful later, but you can leave it out if you want.

The psedocode that lastchance has provided is very good. After reading it I shortened my code.

If you are still having problems post your code so we can see what you have done.

If you are finished and have what you need edit your OP an put a green check on it so everyone will know that you are done.

Andy
Topic archived. No new replies allowed.