CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Program help

  1. #1
    Join Date
    Sep 2016
    Posts
    3

    Program help

    Hello! I am very new to programming and I can not figure out how to do this code. It has been a struggle the past weekend trying to figure it out. The program is:

    "A loop that keeps reading words until it runs out of input, and prints the words that are alphabetically first and last. You can assume that the words are actual words and all in lowercase."
    This is what I have so far:

    int main ()
    {
    string current;
    cout << "Enter a word" << ".\n"

    while(cin>>current)

    {
    }
    }

    I am completely stuck, so any help would be greatly appreciated.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Program help

    Did you try to take a piece of paper with a pen/pencil and write on how you would do it...?
    Victor Nijegorodov

  3. #3
    Join Date
    Sep 2016
    Posts
    3

    Re: Program help

    Quote Originally Posted by VictorN View Post
    Did you try to take a piece of paper with a pen/pencil and write on how you would do it...?
    Yes, but I just don't know how to execute it in the program. I'm just unsure how I would write it in C++ language in order for the program to run the way I want it to.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Program help

    Well, let's do it step-by-step!
    What is your two (or three) first paper-steps to solve the problem?
    Victor Nijegorodov

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Program help

    It is helpful when posting code to use code tags so that the code is formatted for readability. Go Advanced, select the code and click '#'.

    As you need to determine the first and last word, you're going to need two string variables - one to hold the current first and one to hold the current last. So for each word read, you'll need to determine if this one is the new current first or is the new current last.

    Once the loop has terminated (what is the terminating condition?) then the current first and current last variables will hold the values required.

    As Victor mentions, before you start coding you should design the program first, then code the program from the design and then test/debug the program.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Sep 2016
    Posts
    3

    Re: Program help

    I need to have a loop that reads in words, which requires the user to input a word throughout the loop. The program would then need to stop when the user doesn't input any more words into the program.

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Program help

    So you need something like this as pseudo code
    Code:
    loop
        display message
        obtain word
    
        if word entered then
           process word
    
    until word not entered
    
    display first and last word
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured