Counting

I have a major problem with my project. This is the explanation of the project.
I don't know where to start, could anyone help me?

1. Complete counts.cpp using the tools provided in common.h as follows:
Read characters from standard input until EOF (the end-of-file mark) is read (see the textbook page 353, and know that cin has most of the same features as input file streams. Do not prompt the user to enter text - just read data as soon as the program starts.
Keep a running count of each different character encountered in the input, and keep count of the total number of characters input.
Note there are a total of 128 ASCII codes, equal to the value of the symbolic constant NUM defined in common.h. Get familiar with the features of common.h - many of them are handy, and you will be required to use some of them in later steps.
Print a neat table that shows each different input code, the corresponding character and its count, and print the total count at the end. Do not print any rows for ASCII codes that are not included in the input text.
You must exactly match the format of our solution's basic results, and so you must use the symbols and functions defined in common.h. Do NOT print anything any other way.
Use prHeader one time at the start.
Use prCountStr for each row that corresponds to a special character (codes 0-32, and 127), and use the symbol strings provided in common.h.
Use prCountChr for each row that corresponds to a printable character (codes 33-126).
Use prTotal one time at the end.
You can test it by typing text manually, in which case you must enter ctrl-D on Linux (or ctrl-Z on Windows) to end the input. Or better, test it by redirecting a file using the Linux redirection operator ('<'). For example, to input this sample text file named sample.txt, in our second sample run, notice that we typed the following:
./counts < sample.txt
Another good input file for testing is allchars.txt which contains one of each ASCII character code. Of course, many of these codes don't show up well when you view it in your web browser, but your program should be able to identify them. Here's the result (of course): allchars-out.txt.
Make a backup copy of counts.cpp before attempting Part 2. A fully working basic version (i.e., just Part 1) will earn half-credit for this project, more points than a broken enhanced version that we cannot test at all.


2. Enhance counts.cpp so that it responds to command line arguments as specified below. Here is an example program showing how command line arguments can be processed: args.cpp.
Let the user enter input filenames and/or an output filename on the command line.
If any input files are requested, then read from those files instead of cin - count all of the characters in all of the input files as one set of input data.
if a command line argument begins with '-o', then the next command line argument is an output filename (-o name). In that case print results to the file instead of cout. Ignore any command line arguments that may follow the output filename.
Signal errors by using the functions defined at the bottom of common.h:
Use badFile if a file (whether input or output) cannot be opened.
Use badOption if an argument begins with '-' but the second character is not 'o'.
Use missing if the '-o' option is not followed by a filename (even if the user types -oname with no space before the name).
Match these enhanced results. Notice the last several runs show error messages and no results.
This is the file we have to write our program based on:

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
// common.h
// DO NOT COPY/PASTE ME INTO counts.cpp, BECAUSE
// I AM ALREADY #included THERE ON LINE 5. JUST BE
// SURE A COPY OF ME IS IN YOUR WORKING DIRECTORY.
//
// Use the constants, symbols and functions herein to
// insure that your printed results exactly match our
// solution's results. Do not print by any other means.
//
// Do not change this file, and especially do not rely on any
// changes to it. You will not turn this file in.

#ifndef COMMON_H
#define COMMON_H
#include <iostream>
#include <iomanip>
#include <cstdlib>

// global constants: number of different characters, and
//                   first and last printable characters
const int NUM = 128;
const int FIRST = '!';
const int LAST = '~';

// symbols for special characters, corresponding to codes 0 through FIRST-1
const char symbols[][4] = {"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK",
    "BEL", "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI", "DLE", "DC1",
    "DC2", "DC3", "DC4", "NAK", "SYN", "ETB", "CAN", "EM", "SUB", "ESC",
    "FS", "GS", "RS", "US", "SPC" };

// symbol for DEL character, code LAST+1 (same as NUM-1)
const char symbolDel[4] = "DEL";

// the following four functions must be used to print results

// use prHeader at the start to print header row (titles)
void prHeader(std::ostream& out) {
    out << "Code\tChar\tCount\n----\t----\t-----\n";
}

// use prCountStr to print count for one of the special symbols
void prCountStr(std::ostream& out, int code, const char str[], int count) {
    out << std::setw(3) << code << '\t' << str << '\t'
        << std::setw(5) << count << std::endl;
}

// use prCountChr to print count for one of the printable characters
void prCountChr(std::ostream& out, int code, const char chr, int count) {
    out << std::setw(3) << code << '\t' << chr << '\t'
        << std::setw(5) << count << std::endl;
}

// use prTotal at the end to print total character count
void prTotal(std::ostream& out, int count) {
    out << "\t\t-----\nTotal\t\t"
        << std::setw(5) << count << std::endl;
}

// use the following three functions for part 2 error messages
// ignore these functions for part 1

// use badFile(name) to exit if a file (name) cannot be opened
void badFile(char name[]) {
    std::cout << "bad file: " << name << std::endl;
    std::exit(1);
}

// use badOption(op) if an invalid option (not '-o') is on command line
void badOption(char op[]) {
    std::cout << "bad option: " << op << std::endl;
    std::exit(2);
}

// use missing() if output filename is missing
void missing() {
    std::cout << "missing output file\n";
    std::exit(3);
}

#endif
Last edited on
And this is how we have to start our main function:

1
2
3
4
5
6
7
8
9
10
11
12
// counts.cpp - counts characters

#include <iostream>
#include "common.h"
using namespace std;

int main(int argc, char *argv[]) {
    

	return 0;
}
I have a major problem with my project. This is the explanation of the project.
I don't know where to start, could anyone help me?

Some tips :
There is always a flow to implement the program. If you are stuck somewhere you can ask us for details.
Since you ask here and multiple people will review your questions, please wait and be patient.
You don't even know how to start yet, I think the question is broad given such a big project. It would be easier for us if you have some specific problem.
I slightly know how to start. I have to call functions, symbols and characters in the file. But the file is already mentioned in the main function. Should I just start calling all those characters, symbols and functions?
How Should I call the functions in the file common.h?
how to use variables?
how to call symbols and characters?
Thanks,
Best is to do it step by step. First step is to create a project and include the common.h
Test if it runs.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
#include "common.h"

using namespace std;

int main()
{
  system("pause");
  return 0;
}


Next step would be to read all the chars. To check that it reads them properly output them to the console.
@Thomas1965

Yeah, it's working. for reading all characters I have to simply call the function of characters? Because usually when we want to read characters of a file, that file is some words or numbers not a file containing functions.
Last edited on
You are supposed to read the characters from the keyboard. The functions in the file are only for the output so you can ignore them for a while. To read the characters from the keyboard you can use istream& get (char& c); See if you get it working
http://www.cplusplus.com/reference/istream/istream/get/
@Thomas1965

This is the first step, is it right?
The next step is to count them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include "common.h"

using namespace std;

int main()
{
	// create an object of type ifstream to read in to the file
	ifstream read("common.h"); 
	char data; 
	while (!read.eof()) {
		input.get(data); 
	}
	
	return 0;
}
Yes counting would be the second step. However the first step is not working. You are supposed to read from the console not from a file.
1. Complete counts.cpp using the tools provided in common.h as follows:
Read characters from standard input until EOF (the end-of-file mark) is read (see the textbook page 353
Ok I got this for the first part. but there is a bug somewhere I don't recognize. Could anyone help 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
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
#include <iostream>
#include <string>
#include "common.h"

using namespace std;

int main(int argc, char *argv[])
{
	int i = 0, i2 = 0, totalCount = 0;
	char character, counter[10000] = { 0 };
	int shownArray[NUM] = { 0 };
	FILE *out;
	out = fopen(int OUTPUT, "w");
	prHeader(out);

	if (out == NULL)
	{
		printf("ERROR opening files. \n");
		return 1;
	}
	else
	{
		printf("SUCCESS opening files. \n");
		while (scanf("%c", &character) != EOF)
		{
			counter[totalCount++] = character;
			printf("%c", character);
		}

		for (i = 0; i<NUM; i++)
		{
			for (i2 = 0; i2 <= totalCount; i2++)
			{
				if (i == counter[i2])
					shownArray[i]++;
			}
		}

		for (i = 0; i<NUM; i++)
		{

			if (shownArray[i]>0)
			{
				if (i <= 32)
				{
					prCountStr(out, i, symbols[i], shownArray[i]);
				}
				else if (i == 127)
				{
					prCountStr(out, i, symbolDel, shownArray[i]);
				}
				else
				{
					char c = i;
					prCountChr(out, i, c, shownArray[i]);
				}
			}
		}
		prTotal(out, totalCount);
	}
	fclose(out);
	return 0;
}
Topic archived. No new replies allowed.