Novice C++ programmer need help

Discussion in 'C++' started by lilchiko1286@gmail.com, Jul 28, 2008.

  1. Guest

    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.
    , Jul 28, 2008
    #1
    1. Advertising

  2. raj s Guest

    On Jul 28, 9:35 am, wrote:
    > 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);
    }
    raj s, Jul 28, 2008
    #2
    1. Advertising

  3. Jim Langston Guest

    <> wrote in message
    news:...
    >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.
    Jim Langston, Jul 28, 2008
    #3
  4. >> output
    >> the amount of times the letter appears in the word.


    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.
    Gernot Frisch, Jul 28, 2008
    #4
  5. James Kanze Guest

    On Jul 28, 10:10 am, "Gernot Frisch" <> wrote:
    > >> output
    > >> the amount of times the letter appears in the word.


    [...]
    > 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?

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jul 28, 2008
    #5
  6. Puppet_Sock Guest

    On Jul 28, 12:35 am, 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
    Puppet_Sock, Jul 28, 2008
    #6
  7. Noah Roberts Guest

    wrote:
    > 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.
    Noah Roberts, Jul 28, 2008
    #7
  8. Guest

    On Jul 28, 4:10 am, "Gernot Frisch" <> wrote:
    > >> output
    > >> the amount of times the letter appears in the word.

    >
    > 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.
    , Jul 31, 2008
    #8
  9. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    wrote:
    > 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-----
    Federico Zenith, Aug 1, 2008
    #9
  10. writes:
    > 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.


    --
    __Pascal Bourguignon__
    Pascal J. Bourguignon, Aug 1, 2008
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roedy Green

    Little job for novice programmer

    Roedy Green, Sep 9, 2003, in forum: Java
    Replies:
    0
    Views:
    341
    Roedy Green
    Sep 9, 2003
  2. AMT2K5
    Replies:
    6
    Views:
    732
    Larry I Smith
    Jun 9, 2005
  3. AMT2K5
    Replies:
    9
    Views:
    6,468
    AMT2K5
    Jun 11, 2005
  4. AMT2K5
    Replies:
    6
    Views:
    852
    AMT2K5
    Jul 6, 2005
  5. AMT2K5
    Replies:
    15
    Views:
    576
    Victor Bazarov
    Jul 17, 2005
Loading...

Share This Page