Thread: Need Help....Simple Program

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    3

    Need Help....Simple Program

    Hello all, im pretty new to C++ been learning over the past couple weeks. The problem im having is this:

    Im writing code to promt the user to enter thier name, either first or last...doesnt matter.
    Well thats easy but the problem im having is that i need to prompt the user to enter a letter and then check the name for how many times that letter occurs...and then display that to the screen...does anyone know how i can do that??

    Ohh and its due thursday so yeah this is my last resort....
    MVP

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    3
    to follow up i used name[30] as my char and used void getLetterCount() as my function

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    22

    reply

    I'm a relative newbie to C++ myself, but here is my suggestion. Have the user input the letter and store it in a char variable (with cin >> char_variable). Then have the user enter their name and store it in name (with cin.getline(name, 29, '\n')). Then use a FOR loop to cycle through the word and check if each letter is equal to the letter the user is looking for (char_variable). Each time you find a letter that is equal, increase the value of an integer variable (num) by one, and that variable will then be equal to the number of times that letter occurs in the word. Example:

    Code:
    int getLetterCount(name)
    {
    for (int x = 0; x < strlen(name); x++)
    {
      if (name[x] == char_variable)
      {
        num = (num + 1);
      }
    }
    return num;
    }
    Again, i am a newbie myself, so that may not be right, but it is my best suggestion.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yeah, that would work. Just make sure to declare and initialize num.
    Sent from my iPadŽ

  5. #5
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    yea, also you should think about how your function will get the information about the name and the character. either you could do what cjmdjm did and pass in the name, but then you have to prompt the user to input the letter from inside your function. OR you could prompt the user from main() and pass the name AND the character to be found to the function like this:
    Code:
    int getLetterCount(char name[], char letter_to_find)
    {
    ...
    }
    either way should work fine, whichever you want to do. i'd personally do what I just showed you but that's just me...

    and if you hadn't noticed, you should change your function to return int and not void, unless you're using that function to display how many times the character was found.
    Last edited by linucksrox; 11-01-2005 at 07:37 PM.
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    3
    Thx alot, it worked...i had to mod it a bit...but for what i need it to do it workss...thx alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM