-
September 25th, 2016, 02:02 PM
#1
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.
-
September 25th, 2016, 02:48 PM
#2
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
-
September 25th, 2016, 03:16 PM
#3
Re: Program help
Originally Posted by VictorN
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.
-
September 25th, 2016, 03:25 PM
#4
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
-
September 25th, 2016, 04:17 PM
#5
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)
-
September 25th, 2016, 10:54 PM
#6
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.
-
September 26th, 2016, 01:27 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|