Novice C++ programmer need help

L

lilchiko1286

I am taking an intro to C++ course in college. I was assigned a
project that i must complete on MS Visual Studio in where I am
supposed to prompt the user for a name and then a letter and output
the amount of times the letter appears in the word. Like the game hang
man. Then I must prompt the user for another letter and repeat the
same steps until the user enters a specific key or a sentinel.

I am somewhat familiar with I/O, Functions and simple data types
however I am having a hard time getting used to arrays and how to pass
them as parameters to a function. Would someone be nice enough to give
me some advice. I would greatly appreciate it.
 
R

raj s

I am taking an intro to C++ course in college. I was assigned a
project that i must complete on MS Visual Studio in where I am
supposed to prompt the user for a name and then a letter and output
the amount of times the letter appears in the word. Like the game hang
man. Then I must prompt the user for another letter and repeat the
same steps until the user enters a specific key or a sentinel.

I am somewhat familiar with I/O, Functions and simple data types
however I am having a hard time getting used to arrays and how to pass
them as parameters to a function. Would someone be nice enough to give
me some advice. I would greatly appreciate it.
void fn_with_array(char a[])
{
//use a
a[1] = 'e';
}
main()
{
char main_a[] = {'a','b','c'};
fn_with_array(main_a);
}
 
J

Jim Langston

I am taking an intro to C++ course in college. I was assigned a
project that i must complete on MS Visual Studio in where I am
supposed to prompt the user for a name and then a letter and output
the amount of times the letter appears in the word. Like the game hang
man. Then I must prompt the user for another letter and repeat the
same steps until the user enters a specific key or a sentinel.

I am somewhat familiar with I/O, Functions and simple data types
however I am having a hard time getting used to arrays and how to pass
them as parameters to a function. Would someone be nice enough to give
me some advice. I would greatly appreciate it.

Hmm.. I can't think of any arrays that your program would need. Only
std::string and perhaps char. An int or two untless you use a std
algorithm.
 
G

Gernot Frisch

output
1st step:
get the name
look up "cout", "cin" and "std::string"
2nd step:
get the character, you should know this when you're here
3rd step:
get the count of that character in the word:
int CountOccourances(std::string& name, char character)
{
int count=0;
for( 'every character in "name" ' )
{
if('this character' == character) ++count;
}
return count;
}

4th step:
output the number of characters.


We're not doing your homework. Please, please try it yourself unless you
have specific questions. Programming is not about copy/paste. It's about
understanding how a computer works.
 
J

James Kanze

[...]
3rd step:
get the count of that character in the word:
int CountOccourances(std::string& name, char character)
{
int count=0;
for( 'every character in "name" ' )
{
if('this character' == character) ++count;
}
return count;
}

Why not just use std::count?
 
P

Puppet_Sock

On Jul 28, 12:35 am, (e-mail address removed) wrote:
[snip whinge on can't-do-homework]
Would someone be nice enough to give
me some advice. I would greatly appreciate it.

No, I don't think you would appreciate the advice I
would give to you.
Socks
 
N

Noah Roberts

I am taking an intro to C++ course in college. I was assigned a
project that i must complete on MS Visual Studio in where I am
supposed to prompt the user for a name and then a letter and output
the amount of times the letter appears in the word. Like the game hang
man. Then I must prompt the user for another letter and repeat the
same steps until the user enters a specific key or a sentinel.

I am somewhat familiar with I/O, Functions and simple data types
however I am having a hard time getting used to arrays and how to pass
them as parameters to a function. Would someone be nice enough to give
me some advice. I would greatly appreciate it.

In college I fed myself by working as a tutor.

I wasn't the only one.
 
L

lilchiko1286

1st step:
 get the name
 look up "cout", "cin" and "std::string"
2nd step:
 get the character, you should know this when you're here
3rd step:
 get the count of that character in the word:
 int CountOccourances(std::string& name, char character)
 {
    int count=0;
    for( 'every character in "name" ' )
    {
        if('this character' == character) ++count;
    }
    return count;
 }

4th step:
  output the number of characters.

We're not doing your homework. Please, please try it yourself unless you
have specific questions. Programming is not about copy/paste. It's about
understanding how a computer works.


Well yeah i know your not doing my homework. I have a pretty clear
understanding of the basic concepts of C++. It feels very good when
you can figure out things all by yourself but every so often you need
to ask for advice.

Most of the steps you outlined were similar to the ones in the
algorithm we were working on in class. However my teacher suggested a
boolean array to match the letter input by the user to every letter in
the alphabet. When a match occurs to increment the counter variable
and exit the loop.
 
F

Federico Zenith

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Most of the steps you outlined were similar to the ones in the
algorithm we were working on in class. However my teacher suggested a
boolean array to match the letter input by the user to every letter in
the alphabet. When a match occurs to increment the counter variable
and exit the loop.

Geez, if your C++ teacher is suggesting arrays for such a trivial
problem you should not listen to what (s)he says. Arrays are a pain in
the rear, and in C++ there are much better options.

Anyway, look at this reference page:
http://cppreference.com/cppalgorithm/index.html
Look at the «count» function. You can get your whole program done in six
instructions, only one of which is actual computing. Note that you have
to find out how to get the extreme iterators of a string (look around in
the same site!)

Always look in the libraries first :)
Cheers,
- -Federico
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFIkwhiBIpu+y7DlLcRAl0rAJ9vNYZJ8jBS3rRRSo7kRkvb3o8UGwCgppmi
jb9PaXxX4oe1l7JiKdomzuI=
=M9Sy
-----END PGP SIGNATURE-----
 
P

Pascal J. Bourguignon

Most of the steps you outlined were similar to the ones in the
algorithm we were working on in class. However my teacher suggested a
boolean array to match the letter input by the user to every letter in
the alphabet. When a match occurs to increment the counter variable
and exit the loop.

A vector of bool won't help you knowing the number of times the letter
occurs in the word. You would need a vector of integers for that.

std::string word="Syncopistically";

std::vector<unsigned int> counts(1<<CHAR_BITS,0);
for(int i=0;i<(1<<CHAR_BITS);i++){
counts=std::count(word.begin(),word.end(),(char)i);
}


Then you could know the number of times a letter occurs in word by
looking up this vector:

char letter;
std::cin>>letter;
std::cout<<counts[(unsigned int)letter];


But nowadays you would have to take into account unicode, that is a
character is no more a byte, and pre-building a counts vector for 2^21
characters is not worthwhile, unless you have more than 2^19 letters
to test in a word between 4 and 8 letters. It is better to apply
std::count directly to the word and letter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top