Hello World

Hello everyone

I'm having a problem with 2 of the questions on one of my assignments. If any one can offer assistance it would be greatly appreciated, my main problem is the manipulation of the strings, I don't know what code to write to do work on the files, here are the questions:

Question 3

The quality control team of a sweets factory wants to check if the packets of lollies contain the amount of lollies that they are supposed to contain according to the standards. There are small packets that should contain 50 lollies and large packets that should contain 100 lollies. Every morning a random number of packets are selected, opened and the lollies counted. A file is used to keep record of the number of lollies in each of the randomly selected packets together with an indicator to say whether it is a small or large packet. A different number of packets is selected every day, so that the total records per file is not known. The letter ‘S’ is used for small packets and the letter ‘L’ for large packets. Each line in the file represents the size of one packet and the number of lollies inside that packet.

Use variables of type int to read in the number of lollies, and to calculate all totals, and variables of type double to calculate averages. Write a program that will compute and output the following information:

- Count the number of small packets and the number of large packets.

- Count the number of packets (small and large separately) that contains the required amount of lollies according to the standards

- Compute the average number of lollies per small and per large packet

It is a requirement that the random selection of packets includes small and large packets. The program should assert an error if that is not the case. A value of 0 may also not be captured for the number of lollies – assert an error if this is the case. Prompt the user for the name of the file that contains the data to be processed. Write the output to a file control.txt.

Test you program with the following input file. Call your input file inputdata.dat. Do not count the number of records in the file – your program has to work for any number of records.

S 48

S 50

S 47

L 99

L 100

L 101

L 104

S 45

S 52

L 97

S 48

L 97

S 51

L 100

L 103


Using the above file, the results should be as follows:

Number of small packets: 7

Number of large packets: 8

Number of small packets containing 50 lollies : 1

Number of large packets containing 100 lollies : 2

Average number of lollies per small packet : 48.7143

Average number of lollies per large packet : 100.125



With this problem, I can connect the files, do all the calculations, put in the assert statement/s but I do not know how to manipulate the file. e.g. Count and differentiate between the characters (S, L); count the characters and the values next to them as a whole (S 48) so that the value is added to small packets and not all the packets.




Question 4

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 user id 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 user id of jgsmit and GM Bezuidenhout will give a user id 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.txt.

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 user id’s. 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.txt 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;


This seems to be pure string manipulation, I’m lost in this question. I can connect and save the files and do the easy stuff but when it comes to writing code that skips spaces and only takes the first 8 characters, its all downhill from there. Another problem I can’t work out is triggering the code to take the semicolon as the end to one user ID and the start to another.


I'm not looking for someone to give me the answer to the questions just help with the problems within the questions.



For question one, you can use the charAt(0) method in string class.
just grab each line using getline
then process by a set number of counters and such.

eg.

if string.charAt(0) == 'S'
then small count++, small total = small total + string.substring(1,string.length());

if small total == 50
then correctSmallCount++;

Then do basic math to work out averages.

Ps to convert string to int check stringstream. There's some articles about how to do it in the articles forum.
Last edited on
Thanks Campton, I will pay special attention to it when I go through the article.
charAt is java, string in C uses simply at
so same structure just "at" instead of "charAt"?

What about the rest of the statement?
"then correctSmallCount++;"... etc
Last edited on
Topic archived. No new replies allowed.