Thread: Need help

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    3

    Unhappy Need help

    Please help to prototype a code:
    This program first asks a user to enter a string of characters, then asks the user to enter a single character from that string. The progra should then output how many times the character appears in the string.
    Please help!

  2. #2
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331

    Re: Need help

    Originally posted by Cbase
    Please help to prototype a code:
    This program first asks a user to enter a string of characters, then asks the user to enter a single character from that string. The progra should then output how many times the character appears in the string.
    Please help!
    Well, you will need two char variables, string header file, a for loop might be good for finding how oftent he char is enterd. Try writing a psudo code algorithm

  3. #3
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    Where is your problem? Are you having trouble with the algorithm itself or is there a certain thing you need to do, like searching for example, that you're having trouble with?


    Exactly like ROD said, try psudo code first, figure out your steps. If you need help on it, let us know what you're having trouble with.

    Also please make sure to read the Announcements and make sure that if you post code you used [code] tags.

    Let me know if any of this doesn't make sense via PM.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    cout << "Enter a string: ";
    
    char pszBuffer[256];
    cin.getline(pszBuffer, 255, '\n');
    
    cout << "\nEnter a character, i'll tell you how many instances of it occour in the string: ";
    
    char cSearch;
    cin >> cSearch;
    
    int iLength = strlen(pszBuffer);
    int iCount = 0;
    for (int i = 0; i < iLength; i++)
    {
         if (pszBuffer[i] == cSearch)
              count++;
    }
    
    cout << iCount << " instances of " << cSearch << " found!\n";

  5. #5
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    edit:// Just making a point.
    Last edited by RoD; 10-30-2002 at 03:01 PM.

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    3
    Thanks for the inputs guys, I need to understand it in the most basic form and I'm trying to digest all of it in, to figure it out by my self. I first understand the concept of counting the number of characters of a string then outputing that number, but it gets a little complicated in the next step of asking a "single" character and counting & diplaying that "integer".

  7. #7
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    >>but it gets a little complicated in the next step of asking a "single" character and counting & diplaying that "integer".



    something Like
    int count, letterfound;

    while (letterfound == 1)
    {
    count++;
    }

    cout<<"The letter was found "<<count<<" times.\n";

  8. #8
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Thanks for the inputs guys, I need to understand it in the most basic form and I'm trying to digest all of it in, to figure it out by my self. I first understand the concept of counting the number of characters of a string then outputing that number, but it gets a little complicated in the next step of asking a "single" character and counting & diplaying that "integer".
    See my post. It asks for a string, and a character. The instances of character in string is printed out. From what I can gather, this is what you want.

    and we wonder why they ask for code to whole programs...
    That was a very trivial routine, it took me all of 1 minute to type it out... I wouldn't call it an "entire program". Don't worry about how I answer questions and I won't worry about how you do the same.

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    3
    Thanks guys especially Eibro, I figured it out. As for the "woonder why they need whole programs" No, i dident need the whole program, but understanding the concept as a whole, from a beginners perspective helps because i was just stuck on one part, and its not cheating cause its not for school,i already had the first part. Anyways you guys are great, i aspire to reach your level of C++ knowledge someday.

Popular pages Recent additions subscribe to a feed