Help on C++ Homework

I've been assigned the following assignment for my C++ class. I'm not sure where to start and some help would be appreciated. Thank you in advance.

a) Write a program that inputs several lines of text from the keyboard and prints an alphabetical listing of each word in the text and how many times it occurred. To do this, keep a pointer to each word in an array of pointers. Use the qsort() function to sort the array of pointers and then count the number of occurrences of each word. Terminate input by signaling end-of-file from the keyboard. Ignore any case differences (e.g., “Cat” and “cat” are the same word). As a minimum requirement, you may assume there will be no punctuation marks and that exactly one space will separate words in the input lines. You may remove this limitation as an extension.

b) Write a second version of this program that will take its input from a sequential file rather than from the keyboard.


This is what I have so far, but it's really not much. I'm not sure where to go from here because I don't completely understand what is meant by "keeping a pointer to each word in an array of pointers". Plus I have errors on lines 16, 34, and 42.

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
#include <iostream>
#include <istream>
#include <string>
#include <locale>
#include <stdlib.h> //qsort
using namespace std;

char lowercase(char input)
{
	char s;
	locale loc;
	int i = 0;

	for (string::size_type j = 0; j < sizeof(input); j++)
	{
		s = tolower(input[j], loc);
		cout << s << endl;
	}

	return s;
}

int main()
{
	string Inputs[100];
	char* input;
	string s;
	locale loc;
	int i = 0;

	cout << "This program will take in an input of several lines of text from the user and print an alphabetical listing of each word in the text, along with how many times the word occured." << endl << endl;
	cout << "Please input a line of text: " << endl;

	lowercase(cin.getline(char(*input), sizeof(input)));
	
	Inputs[i] = s;
	i++;
	

	cout << endl;

	while (input != "end")
	{
		cout << "Please input a line of text (to terminate input, please type \"end\"): " << endl;
		cin >> input;
		cout << "FOLLOWING INPUT" << endl;
		for (string::size_type j = 0; j < sizeof(input); j++)
		{
			s = tolower(input[j], loc);
			cout << s << endl;
			Inputs[i] = s;
			i++;
		}
	}

	cout << Inputs;
	system("pause");
}
Last edited on
1
2
3
4
int main()
{
// insert code here
}

Sadly we are not here to do homework for you. You should at least have a go and we can then help with problems you may encounter. Study, experiment and learn from it, but if we simply do it for you it will be of no benefit.

You need to be more specific where your problem is because what concerns me is the assignment is asking you to use pointers, and pointers are not exactly beginner stuff, yet you said you don't know where to start..?
I know pointers aren't beginner stuff, this is only my second programming class in college, it's only a level 100 class. My professor somehow believes that we're capable of these kinds of problems already, where I've only been programming for about a year now, and only been programming in C++ for a couple of months. I still don't really understand pointers, although I've used them with help in the past, I didn't really understand how they worked. My main concern here is that I'm not sure how to "keep a pointer to each word in an array of pointers".
I'm not sure exactly what your professor is asking, since the assignment is at odds with some basic C++ stuff.

1) How many lines is "several"? Are you to perform the histogram on each line, or on the final collection of lines?

2) Are you disallowed to simply sort the input words? Why the separate list of words? Are you supposed to create a lookup map?

OR, are you supposed to be doing this with C-strings, in which case the strings (words) are managed by pointers anyway -- meaning your array of words is an array of char*?

3) qsort() is a C function. For a C++ class, why not use std::sort() and avoid all the weirdness that qsort() requires?

4) If you are using C++ to input words, the number of spaces separating them is irrelevant.

You need to ask your professor for clarification. (Best results if you ask him for help understanding how to organize your program.)

Good luck!
Unfortunately, I'm required to used the qsort() function rather than std::sort(). Although I agree it'd probably be easier to use std::sort(), but I would lose points for it.

I've written a bit more code (I've edited the code of my original post as well), but I'm having some errors with cin.getline().
Line 16: 'input' is a char, not a string. Hence, you cannot dereference it. (Remember, a string is an array of chars.)

Here's a lowercase function you can use:
http://www.cplusplus.com/faq/sequences/strings/case-conversion/#diy

Line 34: Okay, you really need to read up here:
http://www.cplusplus.com/faq/sequences/strings/c-strings-and-pointers/

Also, you really need to check your documentation. istream::getline(), like most stream methods, return the stream, not the object they modify.

1
2
3
4
char input[ 100 ];

cin.getline( input, 100 );
for (char& c : input) c = tolower( c, loc );


Line 42: The 'input' variable is a pointer to a character, so while your code is syntactically correct, the compiler is actually catching a semantic error for you -- it is very, very, very, very unlikely that your 'input' variable will ever have the address of the literal data "end".


You can save yourself a whole lot of grief by using std::string directly instead of character arrays.

You are also making an error by trying to find the sizeof(input). You can read more about how to actually do it here
http://www.cplusplus.com/faq/sequences/arrays/sizeof-array/
but I recommend you don't waste your time.

If you are using c-strings, find the length of the string with strlen().
If you are using std::string, find the length using s.length().


Alas, I still cannot help you very well about the "keeping a pointer to each word in an array of pointers", as I still don't know what that means either.

I have two suspicions, which I have already mentioned above. You need to go ask your professor for clarification.

Hope this helps.
Topic archived. No new replies allowed.