augh C++ drives me to drink

buzzard34

Gawd
Joined
Feb 14, 2004
Messages
619
So here is my code:
[font=verdana,arial,helvetica]code:[/font]

#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;

const int MAX_WORD_LENGTH = 20;

//Function Prototypes



void Description();
//Description of program


void InitWord(char w[],int len);
//Cells 0..len -1 are initialized to space character

void GetWord (char w[], int& len);
//User is prompted to enter a word with less than MAX_WORD_LENGTH
//letters. The user is then prompted to enter the word.

void ShowWord (char w[], int len);
//Precondition: w is an array which contains a word of
//len letters stored in cells 0..len-1.
//Postcondition: the word is displayed on the screen.
// w and len are unchanged.

void ShowReversedWord (char w[], int len);
//Precondition: w is an array which contains a word of
//len letters stored in cells 0..len-1.
//Postcondition: the word is displayed backwards on the screen.
//w and len are unchanged

bool WordsAreSame (char w1[], int len1, char w2[], int len2);
//Precondition: w1 is an array which contains a word of
//len1 letters stored in cells 0..len1-1.
// w2 is an array which contains a word of
//len2 letters stored in cells 0..len2-1.
//Postcondition: Returns true if the words are the same, false otherwise
//Case is ignored in that Hello and hello will be regarded as equal.
// w1, w2, len1, and len2 are unchanged.
//Uses <cctype>

bool IsPalindrome (char w[], int len);
//Precondition: w is an array which contains a word of
//len letters stored in cells 0..len-1.
//Postcondition: Returns true if the word is a palindrome, false otherwise
//Case is ignored in that AbcBa will be regarded as a palindrome.
// w, len are unchanged.
//Uses <cctype>

int NumberVowels (char w[], int len);
//Precondition: w is an array which contains a word of
//len letters stored in cells 0..len-1.
//Postcondition: Returns the number of letters that are in the set
//{A,a,E,e,I,i,O,o,U,u}
//w and len are unchanged.

void DisplayWordCharm (char w[], int len);
//Precondition: w is an array which contains a word of
//len letters stored in cells 0..len-1.
//Postcondition: The word is displayed on the screen as an ancient
//word charm as described on the assignment.
//Uses <iomanip> and <cctype>


//The main program


int main()
{


char word1[MAX_WORD_LENGTH];
int word1Length;
char word2[MAX_WORD_LENGTH];
int word2Length = MAX_WORD_LENGTH;

Description();

cout << "First word:\n";
InitWord(word1,MAX_WORD_LENGTH);
GetWord(word1,word1Length);
cout << "\n\nSecond word\n";
InitWord(word2,MAX_WORD_LENGTH);
GetWord(word2,word2Length);


//Test GetWord and ShowWord
cout <<"Your first word(between the bars): ";
cout << '|';
ShowWord(word1,word1Length);
cout << '|'<< endl;
cout << "Your second word:";
cout << '|';
ShowWord(word2,word2Length);
cout << '|'<< endl;

//Test ShowReversedWord
cout << "Your first word spelled backwards is ";
ShowReversedWord(word1,word1Length);
cout << endl;

//Test WordsAreSame
if (WordsAreSame(word1,word1Length,word2,word2Length))
cout << "Your words were the same.\n";
else
cout << "Your words are different.\n";


//Test NumberVowels
cout << "The number of vowels in your second word is ";
cout << NumberVowels(word2,word2Length);
cout << ".\n";

//Test IsPalindrome
if (IsPalindrome(word1,word1Length))
cout << "Your first word is a palindrome.\n";
else
cout << "Your first word is not a palindrome.\n";

//Test WordCharm
cout << "Your second word displayed as a word charm:\n\n";
DisplayWordCharm(word2,word2Length);


return 0;
}

//Function Implementations


//Description of program

void Description()
{
cout << "Greetings to the fun with words program.\n";
cout << "You will be prompted to enter two words with ";
cout << MAX_WORD_LENGTH << " \nletters or less. The program";
cout << " will then test your \nfunction implementations.\n";
}

void InitWord(char w[],int len)
//Cells 0..len -1 are initialized to space character
{}

void GetWord (char w[], int& len)
//User is prompted to enter a word with less than MAX_WORD_LENGTH
//letters. The user is then prompted to enter the word.
{ cout << "Please enter the number of letters in your word.\n";
cout << "Make sure the number given doesn't exceed "
<< MAX_WORD_LENGTH << " characters in length: ";
cin >> len;
cout << "Now enter your word.\n";

for (int i=0; i<len; i++)
cin >> w;
}

void ShowWord (char w[], int len)
//Precondition: w is an array which contains a word of
//len letters stored in cells 0..len-1.
//Postcondition: the word is displayed on the screen.
// w and len are unchanged.
{
for (int i = len; i<0; i++)
cout << w;
}

void ShowReversedWord (char w[], int len)
//Precondition: w is an array which contains a word of
//len letters stored in cells 0..len-1.
//Postcondition: the word is displayed backwards on the screen.
//w and len are unchanged
{
for(int i = len-1; i>0; i--)
cout << w;
}

bool WordsAreSame (char w1[], int len1, char w2[], int len2)
//Precondition: w1 is an array which contains a word of
//len1 letters stored in cells 0..len1-1.
// w2 is an array which contains a word of
//len2 letters stored in cells 0..len2-1.
//Postcondition: Returns true if the words are the same, false otherwise
//Case is ignored in that Hello and hello will be regarded as equal.
// w1, w2, len1, and len2 are unchanged.
//Uses <cctype>
{
if((len1==len2)&&(w1==w2));
else
cout << "Words are different.\n";
}

bool IsPalindrome (char w[], int len)
//Precondition: w is an array which contains a word of
//len letters stored in cells 0..len-1.
//Postcondition: Returns true if the word is a palindrome, false otherwise
//Case is ignored in that AbcBa will be regarded as a palindrome.
// w, len are unchanged.
//Uses <cctype>
{int
if((c1==c5)&&(c2==c4))
return true
if (toupper(c1)=toupper(c5));

}

int NumberVowels (char w[], int len)
//Precondition: w is an array which contains a word of
//len letters stored in cells 0..len-1.
//Postcondition: Returns the number of letters that are in the set
//{A,a,E,e,I,i,O,o,U,u}
//w and len are unchanged.
{const int A = 1;
const int a = 1;
const int E = 1;
const int e = 1;
const int I = 1;
const int i = 1;
const int O = 1;
const int o = 1;
const int U = 1;
const int u = 1;

if(w==A,a,E,e,I,i,O,o,U,u);
int count=0;
for(int i=0; i<len; i++;);
{{if(w%5==0)} return count};
else
cout << "No vowels.\n"

}

void DisplayWordCharm (char w[], int len)
//Precondition: w is an array which contains a word of
//len letters stored in cells 0..len-1.
//Postcondition: The word is displayed on the screen as an ancient
//word charm as described on the assignment.
//Uses <iomanip> and <cctype>
{
int setw = 42;
}
[font=verdana, arial, helvetica]

and this is whats its supposed to put out, if we used the word goober for example:
[/font]
[font=verdana,arial,helvetica]code:[/font]

Greetings to the fun with words program.
You will be prompted to enter two words with 20
letters or less. The program will then test your
function implementations.
First word:
How many letters are in your word? 6
Enter your word:goober


Second word
How many letters are in your word? 6
Enter your word:goober
Your first word(between the bars): |goober|
Your second word:|goober|
Your first word spelled backwards is reboog
Your words were the same.
The number of vowels in your second word is 3.
Your first word is not a palindrome.
Your second word displayed as a word charm:

G
O O
O O O
B B B B
E E E E E
R R R R R R
[font=verdana, arial, helvetica]
I give up. What did I do wrong? I know it involves functions, damn those arguments...:(:mad:
[/font]
 
It's really tough to look at your code. Can you repost using the CODE tags, so your spacing and indentation turns out right? Don't forget to disable smilies, too.

I'll have a look at what you've posted; until my contacts fall out, anyway.

Before my contacts fell out, I noticed that NumberVowels is pretty screwed-up.

Code:
int NumberVowels (char w[], int len)
//Precondition: w is an array which contains a word of
//len letters stored in cells 0..len-1.
//Postcondition: Returns the number of letters that are in the set
//{A,a,E,e,I,i,O,o,U,u}
//w and len are unchanged.
{
	const int A = 1;
	const int a = 1;
	const int E = 1;
	const int e = 1;
	const int I = 1;
	const int i = 1;
	const int O = 1;
	const int o = 1;
	const int U = 1;
	const int u = 1;

	if(w[i]==A,a,E,e,I,i,O,o,U,u);
	int count=0; 
	for(int i=0; i<len; i++;;)
	{
		{
		if(w[i]%5==0)} return count
		};
		else 
		cout << "No vowels.\n"
	}

I can't even see how that compiles, as you're missing brackets and semicolons. What do you intend all the constants to do? The if statement doesn't work, and doesn't control anything anyway. What's the modulo five for?

You've got the for loop right; you want to step through each character and do something with it. How can you test if it is a vowel or not? Maybe a big if statement comparing each character to every known vowel? What about a nested loop that rips through an array of known vowels to find it if it exists in that list?
 
buzzard34 said:
Code:
 #include <iostream>
 #include <cctype>
 #include <iomanip>
 using namespace std;
 
 const int MAX_WORD_LENGTH = 20;
 
 //Function Prototypes
 
 
 
 void Description();
 //Description of program 
 
 
 void InitWord(char w[],int len);
 //Cells 0..len -1 are initialized to space character
 
 void GetWord (char w[], int& len);
 //User is prompted to enter a word with less than MAX_WORD_LENGTH
 //letters. The user is then prompted to enter the word.
 
 void ShowWord (char w[], int len);
 //Precondition: w is an array which contains a word of
 //len letters stored in cells 0..len-1.
 //Postcondition: the word is displayed on the screen.
 // w and len are unchanged.
 
 void ShowReversedWord (char w[], int len);
 //Precondition: w is an array which contains a word of
 //len letters stored in cells 0..len-1.
 //Postcondition: the word is displayed backwards on the screen.
 //w and len are unchanged
 
 bool WordsAreSame (char w1[], int len1, char w2[], int len2);
 //Precondition: w1 is an array which contains a word of
 //len1 letters stored in cells 0..len1-1.
 // w2 is an array which contains a word of
 //len2 letters stored in cells 0..len2-1.
 //Postcondition: Returns true if the words are the same, false otherwise
 //Case is ignored in that Hello and hello will be regarded as equal.
 // w1, w2, len1, and len2 are unchanged.
 //Uses <cctype>
 
 bool IsPalindrome (char w[], int len);
 //Precondition: w is an array which contains a word of
 //len letters stored in cells 0..len-1.
 //Postcondition: Returns true if the word is a palindrome, false otherwise
 //Case is ignored in that AbcBa will be regarded as a palindrome.
 // w, len are unchanged.
 //Uses <cctype>
 
 int NumberVowels (char w[], int len);
 //Precondition: w is an array which contains a word of
 //len letters stored in cells 0..len-1.
 //Postcondition: Returns the number of letters that are in the set
 //{A,a,E,e,I,i,O,o,U,u}
 //w and len are unchanged.
 
 void DisplayWordCharm (char w[], int len);
 //Precondition: w is an array which contains a word of
 //len letters stored in cells 0..len-1.
 //Postcondition: The word is displayed on the screen as an ancient 
 //word charm as described on the assignment.
 //Uses <iomanip> and <cctype>
 
 
 //The main program
 
 
 int main()
 {
 
  
     char word1[MAX_WORD_LENGTH];
     int word1Length;
     char word2[MAX_WORD_LENGTH];
     int word2Length = MAX_WORD_LENGTH;
     
     Description();
     
     cout << "First word:\n";
     InitWord(word1,MAX_WORD_LENGTH);
     GetWord(word1,word1Length);
     cout << "\n\nSecond word\n";
     InitWord(word2,MAX_WORD_LENGTH);
     GetWord(word2,word2Length);
  
  
  //Test GetWord and ShowWord
     cout <<"Your first word(between the bars): ";
     cout << '|';
     ShowWord(word1,word1Length);
     cout << '|'<< endl;
     cout << "Your second word:";
     cout << '|';
     ShowWord(word2,word2Length);
     cout << '|'<< endl;
     
  //Test ShowReversedWord
     cout << "Your first word spelled backwards is ";
     ShowReversedWord(word1,word1Length);
     cout << endl;
     
  //Test WordsAreSame  
     if (WordsAreSame(word1,word1Length,word2,word2Length))
         cout << "Your words were the same.\n";
     else
         cout << "Your words are different.\n";
 
 
 //Test NumberVowels
     cout << "The number of vowels in your second word is ";
     cout << NumberVowels(word2,word2Length);
     cout << ".\n";
     
  //Test IsPalindrome 
     if (IsPalindrome(word1,word1Length))
         cout << "Your first word is a palindrome.\n";
     else
         cout << "Your first word is not a palindrome.\n";
     
 //Test WordCharm
     cout << "Your second word displayed as a word charm:\n\n";
     DisplayWordCharm(word2,word2Length);
     
 
     return 0;
 }
 
 //Function Implementations
 
 
 //Description of program
 
 void Description()
 {
     cout << "Greetings to the fun with words program.\n";
     cout << "You will be prompted to enter two words with ";
     cout << MAX_WORD_LENGTH << " \nletters or less. The program";
     cout << " will then test your \nfunction implementations.\n";
 }
 
 void InitWord(char w[],int len)
 //Cells 0..len -1 are initialized to space character
 {}
 
 void GetWord (char w[], int& len)
 //User is prompted to enter a word with less than MAX_WORD_LENGTH
 //letters. The user is then prompted to enter the word.
 {   cout << "Please enter the number of letters in your word.\n";
     cout << "Make sure the number given doesn't exceed "
     << MAX_WORD_LENGTH << " characters in length: ";
     cin >> len;       
     cout << "Now enter your word.\n";       
 
     for (int i=0; i<len; i++)
     cin >> w[i];
 }
 
 void ShowWord (char w[], int len)
 //Precondition: w is an array which contains a word of
 //len letters stored in cells 0..len-1.
 //Postcondition: the word is displayed on the screen.
 // w and len are unchanged.
 {
     for (int i = len; i<0; i++)
     cout << w[i];
 }
 
 void ShowReversedWord (char w[], int len)
 //Precondition: w is an array which contains a word of
 //len letters stored in cells 0..len-1.
 //Postcondition: the word is displayed backwards on the screen.
 //w and len are unchanged
 {
     for(int i = len-1; i>0; i--)
     cout << w[i];
 }
 
 bool WordsAreSame (char w1[], int len1, char w2[], int len2)
 //Precondition: w1 is an array which contains a word of
 //len1 letters stored in cells 0..len1-1.
 // w2 is an array which contains a word of
 //len2 letters stored in cells 0..len2-1.
 //Postcondition: Returns true if the words are the same, false otherwise
 //Case is ignored in that Hello and hello will be regarded as equal.
 // w1, w2, len1, and len2 are unchanged.
 //Uses <cctype>
 {
      if((len1==len2)&&(w1==w2));
      else 
      cout << "Words are different.\n";
 }
 
 bool IsPalindrome (char w[], int len)
 //Precondition: w is an array which contains a word of
 //len letters stored in cells 0..len-1.
 //Postcondition: Returns true if the word is a palindrome, false otherwise
 //Case is ignored in that AbcBa will be regarded as a palindrome.
 // w, len are unchanged.
 //Uses <cctype>
 {int
     if((c1==c5)&&(c2==c4))
     return true
     if (toupper(c1)=toupper(c5));
     
 }
 
 int NumberVowels (char w[], int len)
 //Precondition: w is an array which contains a word of
 //len letters stored in cells 0..len-1.
 //Postcondition: Returns the number of letters that are in the set
 //{A,a,E,e,I,i,O,o,U,u}
 //w and len are unchanged.
 {const int A = 1;
  const int a = 1;
  const int E = 1;
  const int e = 1;
  const int I = 1;
  const int i = 1;
  const int O = 1;
  const int o = 1;
  const int U = 1;
  const int u = 1;
    
     if(w[i]==A,a,E,e,I,i,O,o,U,u);
     int count=0; 
     for(int i=0; i<len; i++;);
     {{if(w[i]%5==0)} return count};
     else 
     cout << "No vowels.\n"
     
 }
 
 void DisplayWordCharm (char w[], int len)
 //Precondition: w is an array which contains a word of
 //len letters stored in cells 0..len-1.
 //Postcondition: The word is displayed on the screen as an ancient 
 //word charm as described on the assignment.
 //Uses <iomanip> and <cctype>
 {
      int setw = 42;
 }

  
  and this is whats its supposed to put out, if we used the word goober for example:

 
 Greetings to the fun with words program.
 You will be prompted to enter two words with 20
 letters or less. The program will then test your
 function implementations.
 First word:
 How many letters are in your word? 6
 Enter your word:goober
 
 
 Second word
 How many letters are in your word? 6
 Enter your word:goober
 Your first word(between the bars): |goober|
 Your second word:|goober|
 Your first word spelled backwards is reboog
 Your words were the same.
 The number of vowels in your second word is 3.
 Your first word is not a palindrome.
 Your second word displayed as a word charm:
 
                                                               G
                                                             O O
                                                           O O O
                                                          B B B B
                                                        E E E E E
                                                      R R R R R R
   I give up. What did I do wrong? I know it involves functions, damn those arguments...

How's that?
 
At first glance, since you are using iostream, you should have used STL strings. It would have made a lot of things simpler.

The STL string class defines getline(istream, string), so none of that "please don't enter a line too big!"
 
That's a lot better. Again, your program isn't well-formed and won't compile.

In addition ot the "Number Vowels" problem I edited in above, IsPalindrome is wonky:

Code:
bool IsPalindrome (char w[], int len)
 //Precondition: w is an array which contains a word of
 //len letters stored in cells 0..len-1.
 //Postcondition: Returns true if the word is a palindrome, false otherwise
 //Case is ignored in that AbcBa will be regarded as a palindrome.
 // w, len are unchanged.
 //Uses <cctype>
 {[COLOR=Red]int[/COLOR]
     if((c1==c5)&&(c2==c4))
     return true
     if (toupper(c1)=toupper(c5));
     
 }

The extra "int" there is wrong. You need a semicolon after true. The second if statement is an assignment (single equals =), not a conditional (double equals ==). And that second if statement doesn't control another statement.

How can you test to see if a word is a palindrome or not? You can do it by checking one half of the characters against the other half, right? How would you code that?

WordsAreSame has a problem, too. There shouldn't be a semicolon on the if line.

Code:
bool WordsAreSame (char w1[], int len1, char w2[], int len2)
//Precondition: w1 is an array which contains a word of
//len1 letters stored in cells 0..len1-1.
// w2 is an array which contains a word of
//len2 letters stored in cells 0..len2-1.
//Postcondition: Returns true if the words are the same, false otherwise
//Case is ignored in that Hello and hello will be regarded as equal.
// w1, w2, len1, and len2 are unchanged.
//Uses <cctype>
{
if((len1==len2)&&(w1==w2));
else 
cout << "Words are different.\n";
}

You want to compare the strings, not the pointers to the arrays. stricmp() is a function which will compare strings, ignoring case. Or, you could write your own for loop to do it yourself.

The else there doesn't make sense, though I guess you're lucking out with its presence and the spurious semicolon. Instead of writing what you have, an experienced program would code this:

Code:
if((len1 != len2) || (w1 != w2))
    cout << "Words are different.\n";

But that still leaves the comparison of the two strings being incorrect, as I mentioned above.

I don't know what the rules are for making a "Word charm", so I can't help with that part. (I'm not in your class, so I don't have the assignment description in front of me.)
 
How can you test to see if a word is a palindrome or not? You can do it by checking one half of the characters against the other half, right? How would you code that?
You see thats my problem. Half the struggle in C++ is firguring out how to translate what I want a program to do into comprehensible language for the compiler. :mad:

/was never very good at puzzles anyway
//would the coding be somethinf like

i%2=c1,c2
c1==c2

???
 
buzzard34 said:
You see thats my problem. Half the struggle in C++ is firguring out how to translate what I want a program to do into comprehensible language for the compiler. :mad:

That's the struggle in any programming language.

buzzard34 said:
//would the coding be somethinf like

i%2=c1,c2
c1==c2

???

No. You're really missing the basics, and I haven't seen any evidence that you know what you want before you try coding it. I think it might be best if you get help from someone face-to-face.

When you use an operator like % or =, you should be thinking about what you really want from it. i%2 takes i, divides it by two, and results in the remainder of that division operator. But then you use an equals sign, and "c1,c2". What do you expect c1,c2 to evaluate to? What is "c1==c2" testing for?

Maybe a good way to find out if a word is a palindrome is to see if the first letter is the same as the last letter, and the second letter is the same as the second to last letter, and so on. Would you agree? Does that make sense?

How would you code it?
 
Ok...step back...your commiting the #1 noober coder crime of doing too much too quickly.

Try for the following milestones:

- Get user input to work properlly, simply have the user input stuff and print back what they enter.

- Then try for each task seperatly

Also, don't post questions the way you have. It reeks of assignment "do my assignment for me", you'll never get any help that way. Post micro questions, ex:

Q: I am trying to print what the user is entering back to the screen, here is the small chunk of code I developed to do that. What am I doing wrong?
 
Before you even attempt this assignment, you should first learn the language fundamentals. Read up on loops, conditional expressions, etc.

The following chunk of code :


if(w==A,a,E,e,I,i,O,o,U,u);
int count=0;
for(int i=0; i<len; i++;
{{if(w%5==0)} return count};
else
cout << "No vowels.\n"

Really makes no logical sense. Not a criticism, just pointing out that your currently attempting an unnatainable goal. If you continue hacking away at this assignment, you will never finish it.
 
thanks for the pointers. I do apologize for the messiness, but i need serious help in figuring out EVERYTHING in c++. I have received close to a failing grade on nearly every assignment I've been given. Classwork mainly involves copying down everything the professor writes down on the board, he never really explains anything. Plus he never assigns any reading, so I am never sure what to learn from the book. Now I'm rambling, but seriously I still don't understand c++, what the hell should I do? :confused:
 
Get a book. I can't understand why you wouldn't "know what to read". If you start with a good book like "C++ Primer", you can either start at the beginning or cruise the table of contents for words that topics that look familiar from the lectures.

Does your school not have tutors available? If not, maybe it's time to drop the class.
 
I assume the class has a textbook. I recommend you start reading there. See if that doesn't answer your questions. Your code won't compile because it doesn't follow the syntax of C++. There are many books that will explain the basic syntax of C++. I'm surprised you have to make the user tell you how many letters their input word is going to be. That should be easy to figure out as long as you flush your input buffer. Good luck.
 
No offense or anything, but how did you type out that much code before realizing something was wrong? Did you not try and test each function as you coded them?
 
NKDietrich said:
No offense or anything, but how did you type out that much code before realizing something was wrong? Did you not try and test each function as you coded them?
well the professor gave us a template to begin with, most of the program was already written. I did compile the program several times to see about the functions, THEY ARE MY WEAK POINT. I am confused about functions, can someone explain them to me simply? my text and professor really dont explain it that well to me.
 
buzzard34 said:
well the professor gave us a template to begin with, most of the program was already written. I did compile the program several times to see about the functions, THEY ARE MY WEAK POINT. I am confused about functions, can someone explain them to me simply? my text and professor really dont explain it that well to me.

Basically:

They look like this:
f-type name(a1-type arg1, a2-type arg2) {
code
return something
}

The type of the function (f-type) is what this function is supposed to return. If the type is void, you don't have to return anything (void is sort of "nothing"), but otherwise you have to return a value of the correct type.

You seem to have understood the basics of arguments.
One thing to be aware of: When an argument is a pointer or an array, changing the value of it inside the function also changes it on the outside. For other types (e.g. int), you don't.
This is passing by reference (you tell the function where the data is), and passing by value (you give the function a copy of the data).

That's basically it. You give some data to a function, and it does something with it. If it's not a void function, it returns a value. As an example:
Code:
bool isNumberEven( int num ) {
  bool result = false;

  if (num%2 == 0) result = true;  
  return result;
}

To use it:
int testMe = 2;

if (isNumberEven(testMe)) {
  cout << "2 is Even";
} else {
  cout << "2 is odd";
}
 
Back
Top