Can someone help with this please?

Started by
21 comments, last by benslancer 17 years, 4 months ago
I have the following thing that I'm trying to help my sister with, but I have no clue how to tell her do it here it is: Write a program that displays a menu with the following choices to the user. A - Find the largest # with a known quantity of numbers B - Find the smallest # with an unknown quantity of numbers C - Quit Please enter your choice ___ This menu needs to be repeatedly displayed until the user chooses to quit. If A is chosen, I need to prompt the user for the quantity of numbers they wish to answer. Use a for loop to read these and display the largest. If B is chosen, I need to prompt the user for numbers until they enter the # -99 to signal they are done. Find and display the smallest number entered. The code should be in C++ using MVS (Microsoft Visual Studio 2005). I use Dev C++, but my sister uses MVS. I just like DEV C++ better, but my sister uses MVS for school because her instructor says it is a dominate IDE. Is there an easy way to come up with the code for this? If someone could do the code and/OR sort of explain it so that I can explain it to her when she figures it out then that would be great. I'm a novice myself, but she is struggling with hers as well. Thanks in advance. I mena I'm just now lerning Unix/Linux and have not yet gotten into programming, but I'm trying to learn so that I can be a little more successful when I get to the level 2 class. My wife said that a friend of hers suggested that I look for help here. Even if someone posts the code and helps to explain it to me that would be cool. Ben
Advertisement
Of you read the FAQ you will see we don't to homework for people..

Sorry..

theTroll
Well, take it step by step.

First, you need to be able to repeatedly display something based on user input. So, do that bit first. You need a loop that will get user input on every loop through and check it.

That's actually most of it. For the other two options, when you find that the input matches A or B, you just need to use another loop (you'll need one for each option) to get more user input, and do something based on that.

Just take it step by step.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
i say you use 4 functions for this (excluding "main"), to make things a bit easier. You make a "Start()" function, which is called right off the bat from main(), its got your menu, and all the stuff for your code. Then, make a "ChoiceA()" and "ChoiceB()" functions, put the code in there, when theyre done, itll clear the screen, ask wether or not the user wants to do it again, and either goto the Start() function, again, or going to the 4th function, the "Quit()" function. The Quit() function clears the screen, then calls the "system("exit");" function, whichll exit the console as soon as you press a key.

something like this:
#include <iostream>using namespace std;void Start();void Greatest();void Least();void Quit();char sel;int main(){    Start();}void Start(){    system("cls");    cout << "1) Find the largest # in a list\n"         << "2) Find the smallest # in a list\n"         << "3) Quit\n\n"         << "What do you want to do?"         << endl;    cin >> sel;    switch (sel)    {        case '1':            Greatest();            break;        case '2':            Least();            break;        case '3':            Quit();            break;        default:            cout << "\n\n Invalid Input.\n\n";            system("pause");            Start();            break;    }}void Greatest(){    // put the guts here    // ...    system("cls");    cout << "Do again?\n"         << "=========\n\n"         << "1) Yes\n"         << "2) No\n\n"         << "What do you want to do?"         << endl;    cin >> sel;    switch (sel)    {        case '1':            Start();            break;        case '2':            Quit();            break;        default:            Start();            break;    }}void Least(){    // put the guts here    // ...    system("cls");    cout << "Do again?\n"         << "=========\n\n"         << "1) Yes\n"         << "2) No\n\n"         << "What do you want to do?"         << endl;    cin >> sel;    switch (sel)    {        case '1':            Start();            break;        case '2':            Quit();            break;        default:            Start();            break;    }}void Quit(){    system("cls");    system("exit");}
Quote:Original post by TheTroll
Of you read the FAQ you will see we don't to homework for people..

Sorry..

theTroll


I'm not doing homework! It was my sister's work and it was due today, but since she did not get it done she did not get any points. I'm just looking for someone to help me out with it since it don't matter for her anymore. Troll, I'm not trying to get anyone to do my homework cause I don't have that class. I'm trying to learn for myself.
We've only got your word that it's actually your sisters homework though, and giving you the answer to pass on to her still isn't really particularly beneficial. We've got the no homework policy for a reason - namely because people learn better by working through things by themselves, and secondly because people don't really want to spend thier free time doing other people's homework.


In short, noone is going to give you a solution, but we'll give you some pointers if you show that you're making an attempt yourself. You should be able to get started using the hints and sample code (which incidentally you shouldn't really have given RavynousHunter) that have already been given, and ask us if you have any specific questions or something you want clarification on.

- Jason Astle-Adams

Thank you for the help, but what exactly does all of that mean and why was it done that way?It's great to have the answer, but not having the reasons behind it makes it useless to me in the future if I can't replicate it in another situation. I really do appreciate the help though, just need some clarification and such.

Ben
Quote:Original post by benslancer
what exactly does all of that mean and why was it done that way?It's great to have the answer, but not having the reasons behind it makes it useless to me in the future if I can't replicate it in another situation.
...and that is exactly why it would have been better if you'd come up with your own solution with a little help rather than being given some source - take note for similar questions in future RavynousHunter. [wink]

You've got most of a working solution there, sans the logic to solve the actual number problems, so I'll try walking you through it a bit and explain things a little clearer.


Firstly, RavynousHunter chose to implement his solution using several different functions. Breaking down your code into smaller functions can help to increase readability as well as taking the first step towards making sections of your code modular and reusable. If you're likely to have to do something more than once then you might want to consider making it a function; any given function you create should have a name which clearly decsribes it's purpose, and should carry out one specific task. You could have a read through the cprogramming.com Functions tutorial if you'd like a basic overview of functions in C++.

RavynousHunter's solution starts off by going straight in to his Start() function, which simply presents a menu of options to the user and calls a different function based on the choice. There's nothing particularly complex in there, but if you havn't seen the switch case structure before you might want to read up on it - it's basically a neater alternative to a lot of ifs.

The other functions presented in his solution don't actually contain the problem solving logic yet; have a go at figuring out that part yourself and show us your attempt, we'll help you out if you can't nut it out or we can give you some pointers if you aren't sure how to get started on it. The spots where the logic is missing have been marked by comments. Other than that his functions just present the user with another menu and take another action based on it.


The solution could also be implemented very differently, but I won't confuse the issue by showing an alternative unless you want to see it, and then not till you take a shot at filling in the missing logic. [wink]

Hope that helps somewhat. [smile]

- Jason Astle-Adams

I tried the following source code using a while Loop, but I'm not quite sure exactly how to tell the user to pick 5 numbers and then display the largest number The line I am referring to is:

while ((noOfGuesses < 10) && (!done))

and it did not work:


#include <iostream>

using namespace std;

void Start();
void Greatest();
void Least();
void Quit();

char sel;

int main()

{
Start();
}

void Start()
{

while ((noOfGuesses < 10) && (!done))
{
system("cls");
cout << "1) Find the largest # in a list\n"
<< "2) Find the smallest # in a list\n"
<< "3) Quit\n\n"
<< "What do you want to do?"
<< endl;
cin >> sel;
switch (sel)
{
case '1':
Greatest();
cout << "How Many numbers do you want to enter\n"
break;
case '2':
Least();
break;
case '3':
Quit();
break;
default:
cout << "\n\n Invalid Input.\n\n";
system("pause");
Start();
break;
}
}

void Greatest()
{

system("cls");
cout << "Do again?\n"
<< "=========\n\n"
<< "1) Yes\n"
<< "2) No\n\n"
<< "What do you want to do?"
<< endl;
cin >> sel;
switch (sel)
{
case '1':
Start();
break;
case '2':
Quit();
break;
default:
Start();
break;
}
}

void Least()
{
// put the guts here
// ...

system("cls");
cout << "Do again?\n"
<< "=========\n\n"
<< "1) Yes\n"
<< "2) No\n\n"
<< "What do you want to do?"
<< endl;
cin >> sel;
switch (sel)
{
case '1':
Start();
break;
case '2':
Quit();
break;
default:
Start();
break;
}
}

void Quit()
{
system("cls");
system("exit");
}


So DO I need to moe the While statment or add something else. I'm not quite sure how to say -99 as the lowest number. I enede up with the following code, but it still does not seem to be working right, What more could I tweak? or is there a simpler way to do the code that I'm not finding?

#include <iostream>

using namespace std;

void Start();
void Greatest();
void Least();
void Quit();

char number;

int main()

{
Start();
}

void Start()
{

system("cls");
cout << "1) Find the largest # in a list\n"
<< "2) Find the smallest # in a list\n"
<< "3) Quit\n\n"
<< "What do you want to do?"
<< endl;
cin >> number;
switch (number)
{
case '1':
Greatest();
cout << "How Many numbers do you want to enter\n";
break;
case '2':
Least();
break;
case '3':
Quit();
break;
default:
cout << "\n\n Invalid Input.\n\n";
system("pause");
Start();
break;
}
}

void Greatest()
{

system("cls");
cout << "Enter your numbers: ( 0- 100)\n"
<< "0 - 100\n\n"
<< "1) Yes\n"
<< "2) No\n\n"
<< "What do you want to do?"
<< endl;
cin >> number;
switch (number)
{
case '1':
Start();
break;
case '2':
Quit();
break;
default:
Start();
break;
}
}

void Least()
{
// put the guts here
// ...

system("cls");
cout << "The lowest number is:\n"
<< "=========\n\n"
<< "1) Yes\n"
<< "2) No\n\n"
<< "What do you want to do?"
<< endl;
cin >> number;
switch (number)
{
case '1':
Start();
break;
case '2':
Quit();
break;
default:
Start();
break;
}
}

void Quit()
{
system("cls");
system("exit");
}
Hint: how is the value of noOfGuesses changing?

If it's actually for your sister why isn't she posting? That would be a lot more useful. right now she's getting information via a game a telephone where the person in the middle doesn't really know what he's talking about.

-me

This topic is closed to new replies.

Advertisement