The problem with Program number 1 is that I need to somehow determine the
number of letters each word has and display/keep count of it.
I'm not sure how to do this..
1 - Write a program that prompts the user to enter a word. Store the word in
a one-dimensional character array that can store a maximum of 10 characters
and the null character. After storing the word in the array, the program
should then prompt the user to enter a letter. The program should count how
many times the letter appears in the word, then display the value on the
screen. The program should include a loop that allows the user to enter as
many words as desired. It should also include a loop that allows the user to
enter as many letters as desired. For example, if the user enters the word
ball, the program should allow the user to count how many times the letter a
appears in the word, how many times the letter x appears in the word, and so
on. When the user is finished with one word, the program should allow him or
her to enter another word.
What do you mean by 'help'?
> The problem with Program number 1 is that I need to somehow determine the
> number of letters each word has and display/keep count of it.
> I'm not sure how to do this..
How to do what exactly?
> 1 - Write a program that prompts the user to enter a word. Store the word
in
> a one-dimensional character array that can store a maximum of 10
characters
> and the null character. After storing the word in the array, the program
> should then prompt the user to enter a letter. The program should count
how
> many times the letter appears in the word, then display the value on the
> screen. The program should include a loop that allows the user to enter as
> many words as desired. It should also include a loop that allows the user
to
> enter as many letters as desired. For example, if the user enters the word
> ball, the program should allow the user to count how many times the letter
a
> appears in the word, how many times the letter x appears in the word, and
so
> on. When the user is finished with one word, the program should allow him
or
> her to enter another word.
There are a number of things you need to do, which are given in the
question:
- define a character array
- prompt the user for a word
- retrieve and store the user's input
- prompt the user for a letter
- count the occurrences of the letter in the word
- display the count on the screen
- do the above in the loops specified
There are other things the question implies, such as how the user indicates
that he has finished with a word.
Exactly which parts do you need help with, or were asking for someone to
write the whole thing?
David
> I'm writng this program for my C++ class
> if anybody is up to the challenge of helping me I'll appreciate it
> very much,
>
> The problem with Program number 1 is that I need to somehow determine the
> number of letters each word has and display/keep count of it.
> I'm not sure how to do this..
I don't think that *is* the intent. The end of the word is always indicated
by the null character. Don't try to collect data from the word, just store
it and examine it each time a new query character is entered.
thanx
"David White" <n...@email.provided> wrote in message
news:FzGra.3417$e8.1...@nasal.pacific.net.au...
As osmium said, I don't think that's what you were asked to do. You just
have to store the word, such as "apple", in a character array as it is, and
then count the number of times the letter entered by the user appears in it.
That is, count the letter _after_ each time the user enters one. So you
don't have to do anything before the user enters a letter except store the
word in its present form. What you proposed is more complicated than
necessary, and, I'm pretty sure, not what your teacher/book expects you to
do.
David
If you know how to do this can you write that piece of code. or point me
somewhere where I can learn this,
TX
"David White" <n...@email.provided> wrote in message
news:HDHra.4297$e8.1...@nasal.pacific.net.au...
As I said, you don't need to keep a count. You just count the letter when
it's entered, display it, then throw it away. Here is the skeleton of a
program that does what you want. This is probably far more help than
students normally get here, since the whole idea is for you to do it
yourself. But you will have to do the rest.
int main()
{
while(true) // Outer infinite loop
{
const int maxLetters = 10;
char word[maxLetters+1]; // +1 for null character
// Place code to prompt user for word here
// Place code to input word here
// Place code to conditionally break out of loop here
// (e.g., on empty word)
while(true) // Inner infinite loop
{
char letter;
// Place code to prompt user for letter here
// Place code to input letter here
// Place code to conditionally break out of loop here
int count = 0; // Count starts at zero
int i = 0; // Start at first letter in word
while(word[i] != '\0') // Loop till null (end of word) found
{
// Place code here to add 1 to count if current
// letter in word matches input letter
// Place code to move to next letter here
}
// Place code to output count here
}
}
return 0;
}
David
Tip: Don't try to write the whole thing all at once.
First, write a program that just asks the user to enter a word, then
displays it, then loops back and asks the user for another word, etc.
That's the first loop that the description mentions. Get this working to
your satisfaction before going any further.
Then, add code to the program so that for each word, it asks the user to
enter a letter, displays the number of times that letter occurs in the
word, then loops back and asks for another letter, etc. All this code
should be contained completely inside the loop that you wrote for the
first stage. IN fact, if you've covered functions already, you could make
a function out of it and call that function inside the loop for the first
stage. That will impress your instructor, believe me! ;-)
--
Jon Bell <jtbe...@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
thanks.
char word[11]; // has space for 10 chars plus a terminating null
cout << "Gimme a word: "
cin >> word;
Now, if you enter the word 'apple', the array will contain
word[0] --> 'a'
word[1] --> 'p'
word[2] --> 'p'
word[3] --> 'l'
word[4] --> 'e'
word[5] --> '\0'
word[6] through word[10] --> undefined; treat them as random garbage
Note carefully:
1. Reading an entire sequence of data into an array in one swell foop
works *only* for arrays of char. For arrays of ints, doubles, widgets,
etc., you have to read one item at a time in a loop.
2. The code above gives undefined results if you try to enter a word that
contains more than 10 chars. In the context of your assignment you're
probably not supposed to worry about this, but in Real World Programming
you most certainly *do* have to worry about things like this. That's why
Real C++ Programmers Who Know What They're Doing will usually use the
standard string data type, unless they have a very good reason to do
otherwise:
#include <string>
using namespace std;
// blah blah blah
string word;
cout << "Gimme a word: ";
cin >> word;
In this case 'word' automatically expands as necessary to accommodate
whatever the user types in. No need to worry about overflow.