-
Major Problem
Hi everyone, I'm having a major problem with a problem that my proffesor gave us to try and solve. Luckily for me it's not graded, but it's the first time I haven't been able to come up with a solution to a problem. I'll list it below, any help would be greatly appreciated.
------------------------------------
Write a program that represents a set as a zero-one integer array. The user of your program will be prompted to type in ome set in the following format followed by a carriage return.
Q = { a, p , k, r }
The name of the set is a single upper-case letter. This is followed by an equal sign and then an opening brace. The set may be an empty set. If the set has elements, they are each input as a single lower case letter. If there are more than two elements, they are separated by commas, according to common usage . The set notation is terminated by a closing brace. When the user types the enter key, the input is finished. There may be zero or more blank spaces insterted anywhere between any two characters of input (demonstrated above).
Anything differing from the above description of a set should be considered an error. In this case the program prints "Error found -- program terminated", Barring errors, the program simply prints out the statement of the set, from information that was placed in the zero-one array. The printing of the set will place one blank space between any two characters, except for commas and the characters they follow (no blanks there). The elements of the set will be printed in alphabetical order (independent of how they were read)and with no gaps. The above example would print: Q={a,k,p,r}
-----------------------------------
Here's where it really sucks people. She gave me a partially completed program from which to work with.
-----------------------------------
To solve this problem, use the "partially completed" program adding in code for each of the different 'cases' within the switch statement.
------------------------------------
Well, there it is. The code she gacve makes no sense to me but I have to use it and only add stuff in the cases. The code is below. Thanks for any help you might be able to give.
-------------------------------------
<code>
#include <iostream.h>
char get_next_char (void);
void print_set(int[], char);
int main() {
int state, bits[26];
bool end_of_set, error_found;
char c, name;
for (int i=0; i<26; i++) bits[i] = 0;
state = 0;
end_of_set = false;
error_found = false;
cout << "Enter a string of the form: A = {x, y, z }" << endl;
cout << "Use an upper case letter for the set name and " << endl;
cout << "single lower case letters for the elements." << endl;
while ((!end_of_set) && (!error_found)) {
switch (state) {
case 0:
break;
case 1:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
} // end of switch
} // end of while
if (error_found)
cout << "Error found--program terminated." << endl;
else print_set(bits, name);
return 0;
} // end main()
char get_next_char() {
char c;
bool end_of_file;
end_of_file = false;
c = ' '; // Set c to a blank space in order to prime the while
while ((c == ' ') && (!end_of_file)) {
if (( c = cin.get()) == EOF)
{
end_of_file = true;
c = '@'; // Use @ to represent EOF
}
} // end of while
return c;
}
void print_set(int bits[], char name) {
bool need_comma;
need_comma = false;
cout << name << " = {";
for (int i = 0; i < 26; i++)
{
if (bits[i] != 0)
{
if (need_comma) cout << ", ";
cout << (char)((int)'a'+i);
need_comma = true;
}
}
cout << '}'<< endl;
}
</code>
-
you know, if you type [C O D E] (without the spaces) and then [/ C O D E] (without spaces) it will have the indents in your text, to make it more easily understanded :D
of course, i cant help you with that, im newb still...
-
This is what he means
Code:
#include <iostream.h>
char get_next_char (void);
void print_set(int[], char);
int main() {
int state, bits[26];
bool end_of_set, error_found;
char c, name;
for (int i=0; i<26; i++) bits[i] = 0;
state = 0;
end_of_set = false;
error_found = false;
cout << "Enter a string of the form: A = {x, y, z }" << endl;
cout << "Use an upper case letter for the set name and " << endl;
cout << "single lower case letters for the elements." << endl;
while ((!end_of_set) && (!error_found)) {
switch (state) {
case 0:
break;
case 1:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
} // end of switch
} // end of while
if (error_found)
cout << "Error found--program terminated." << endl;
else print_set(bits, name);
return 0;
} // end main()
char get_next_char() {
char c;
bool end_of_file;
end_of_file = false;
c = ' '; // Set c to a blank space in order to prime the while
while ((c == ' ') && (!end_of_file)) {
if (( c = cin.get()) == EOF)
{
end_of_file = true;
c = '@'; // Use @ to represent EOF
}
} // end of while
return c;
}
void print_set(int bits[], char name) {
bool need_comma;
need_comma = false;
cout << name << " = {";
for (int i = 0; i < 26; i++)
{
if (bits[i] != 0)
{
if (need_comma) cout << ", ";
cout << (char)((int)'a'+i);
need_comma = true;
}
}
cout << '}'<< endl;
}
-
What do you mean by a zero-one integer array.
main() never calls get_next_char so unless you add it in somewhere the user will never get the opportunity to be heard.
I don't see what EOF has to do with the program at all.
the print function as posted has nothing to do with user input. It will only print out lower case a-z until a 0 is found in bits[], but other than determining how far up the alphabet to go, bits[] has nothing to do with out put per se.
Maybe when these incongruities are cleared up the intent of the switch statement will become more evident, too.
Yuccchhhh.