Sign in to follow this  

Can someone help with this please?

This topic is 4045 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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");
}


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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");
}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Quote:
Original post by Palidine
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


Because I thought that maybe I could help her and look a little smart if I could figure it out. She is not posting because she is not a very socialble person and it does not matter for her anymore as it had to be turned in earlier this week. She is going to school for programming and I am hopefully going to get a game programming degree. I had an analysis class that recommended this site. I'm trying to figure this stuff out in the Beginner Forum because I don't know what I'm doing! She is not getting any more help from me because she does not need the help anymore. This is now for my own personal learning because it does not make total sense to me right now. I know what I want to do, but getting the stuff right to make the code do what I want it to do, and understand exactly how everything works. Anyway, thank you for the hint and now I will try to figure out the noOfGuesses part even though now I'm not sure if I need that if it is not about guessing the number. I'm tryign to figure out how to make it display the highest number af entering 5 random numbers.

Share this post


Link to post
Share on other sites
ok I now have the following code going, but I get 2 error messages when I compile it. As far as the lowest number going back to -99 would I use what they call a "bool"?

expected constructor, destructor, or type conversion '>>' token
expected',' or ';' before '>>' token

#include <iostream>

using namespace std;

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

char number;

cin >> num1 >> num2 >> num3 >> num4 >> num5;

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 << "Please choose five numbers 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 five numbers: ( 0 - 100)\n"
<< "\n\n"
<< "What do you want to do?"
<< endl;
cout << " The numbers you entered are:" << endl;
cin >> number;
switch (number)
{
case '1':
Start();
break;
case '2':
Quit();
break;
default:
Start();
break;
}
}

void Least()
{


system("cls");
cout << "The lowest number is:\n"
<< " > -99\n\n"
<< " Do you want to continue\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");
}

Share this post


Link to post
Share on other sites
Quote:
Original post by RavynousHunter
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:
*** Source Snippet Removed ***


You do realize that your program is recursing indefinitely, right? (Meaning it could run out of stack space.) It is not so much an issue in this case, but it is something to be aware of. Also, you shouldn't be using those system() calls. "cls" is not typically a program in unix, for instance.


benslancer: read the FAQ regarding posting source code.

Share this post


Link to post
Share on other sites
#include <iostream>

using namespace std;

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

// You use this to get input, but char is just ONE LETTER.
// If you want numbers
char number;

// This code won't work. It must be in a function. You must
// also actually create the variables. I commented it out
// and your code 'compiled' but it is still very very wrong
// so I'll make comments throughout.
//cin << num1 << num2 << num3 << num4 << num5;

int main()
{
// you're using functions which is good, but you
// are using them wrong which is bad. While, in a
// a way, it makes sense to call 'start' and then in
// start to call other functions. But you have to
// think of it like doors in a mansion. You must
// leave the room eventually! A program would be
// like a long hallway. You start at one end, enter
// a room to perform some task and leave the room.
// Then you go into the next room and perform a
// different task and leave it. Then again.
Start();
}

void Start()
{
// In order to repeat stuff in most programming applications you use
// loops. So instead of calling the start function again, you
// would simply leave the switch statement and loop to the top of the function.
// something like this in pseudo-code

// while the user hasn't chosen quit do...
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; // In this case number will do what you want, but in other
// situations it probably won't.
switch (number)
{
case '1':
Greatest();
cout << "Please choose five numbers you want to enter\n";
break;
case '2':
Least();
break;
case '3':
Quit(); // get rid of this! If you want to quit, use a variable
// and use the while loop to test if the variable was set
break;
default:
cout << "\n\n Invalid Input.\n\n";
system("pause");
Start(); // get rid of this too! You have a loop so you don't need
// or want it.
break;
}

// Loop back to top of while loop
}

// This function makes no sense. You ask that the user enter
// five numbers but only ask for one. Then you use a switch
// statement as if you gave them a menu with a list of options!
void Greatest()
{
system("cls");
cout << "Enter five numbers: ( 0 - 100)\n"
<< "\n\n"
<< "What do you want to do?"
<< endl;
cout << " The numbers you entered are:" << endl;
cin >> number;
switch (number)
{
case '1':
Start(); // get rid of this! At the end of this function
// you will return to the function that called this one!
// You can even return explicitly by writing
// return;
break;
case '2':
Quit(); // This doesn't make sense here either
break;
default:
Start(); // get rid of this! You'll go back
// to start without this! Just do what you did in case '1'
break;
}
}


// Take what I wrote above and apply it hear
void Least()
{


system("cls");
cout << "The lowest number is:\n"
<< " >> -99\n\n"
<< " Do you want to continue\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"); // You should do everything you can to never ever write this in your
// code again
}


Now I won't do your code for you, but I absolutely MUST show you an example program to better illustrate what you should do. Think of functions like
rooms. You can enter a room, but you must also exit it. Most places aren't designed so that you may enter one room, go into another, and then another, forever. Of course there are 'loop' of rooms, but pretend they don't exist. A program is rather like a hallway with rooms that don't lead anywhere. You start at one end of the hallway, enter a room, do your business in there, and then leave the room to go back into the hallway. Then you enter the next room, do your business, and leave. Etc. This following program will illustrate many of these points in code form, as well as input which will perform an action.

#include <iostream>

void study();
void desk();
int addNumbers();
int subtractNumbers();
int multiplyNumbers();
int largestNumbers();
void kitchen();

// 'main' is the entryway into 'The Hallway'
int main()
{
// We go to our first room:
std::cout << "Welcome! You have entered 'The Hallway' of my mansion. I'll give you a tour" << std::endl;
study();

// We go to the second room:
std::cout << std::endl << "Now, let us see what is in the next room." << std::endl;
kitchen();

// et cetera:
std::cout << std::endl << "And now we reach the end of my hall. I hope you enjoyed seeing my humble home." << std::endl
<< "Please visit again, would you?" << std::endl;

return 0;
}

// This function perfoms all actions associated with the study. It has a desk.
void study()
{
std::cout << "This is my study. Over there is where I sit at my desk and do math problems." << std::endl
<< "Would you like to see an example(y/n)? ";

// Find out if the user wants to do some math problems at the desk.
char response;
std::cin >> response;

// If yes, go to desk, otherwise leave room
if(response == 'y')
{
desk();
}
else
{
std::cout << "Maybe next time, yes?" << std::endl;
}

std::cout << "Well, let us leave the study and enter The Hallway" << std::endl;
}

// The desk is for solving math problems.
void desk()
{
std::cout << "Excellent! I'm glad you want to see me work." << std::endl;

// Continue doing math problems until the user opts to walk away from the desk
bool keepDoingMath = true;
while(keepDoingMath)
{
// First, print the menu
std::cout << "Here's what we can do:" << std::endl
<< " 1)Add two numbers together" << std::endl
<< " 2)Subtract One number from another" << std::endl
<< " 3)Multiply a few numbers" << std::endl
<< " 4)Find the largest of two numbers" << std::endl
<< " 5)Move away from desk" << std::endl << std::endl;
std::cout << "What would you like for me to do? ";

// Find out what the user wants to do
int menu_response;
std::cin >> menu_response;

// This variable stores the result of the mathematical operations performed
// assuming such a math op was done.
int math_result = 0;

switch(menu_response)
{
case 1:
math_result = addNumbers();
std::cout << "If I remember correctly, the answer is " << math_result << std::endl;
break;
case 2:
math_result = subtractNumbers();
std::cout << "If I remember correctly, the answer is " << math_result << std::endl;
break;
case 3:
math_result = multiplyNumbers();
std::cout << "If I remember correctly, the answer is " << math_result << std::endl;
break;
case 4:
math_result = largestNumbers();
std::cout << "If I remember correctly, the answer is " << math_result << std::endl;
break;
case 5:
std::cout << "Changed your mind? I'm sorry to hear that." << std::endl;
keepDoingMath = false; // allow the exit from loop
return;
default:
std::cout << "Um, I'm afraid that wasn't a choice..." << std::endl;
break;
}// end switch
} // end while

std::cout << "Let us look away from the desk now." << std::endl;
}

// One thing you'll notice about all of these functions... they're almost identical!
// If I were to rewrite them I'd do it like this:
// int function(int first, int second)
// {
// return first OPERATION second;
// }
// And int the calling function I'd say:
// std::cout << "give me two numbers, etc: ";
// std::cin >> first >> second;
// std::cout << "The result is " << function(first, second) << std::endl;

// But I'm too tired.
int addNumbers()
{
std::cout << "Give me two numbers seperated with a space and hit enter, please: ";
int first, second;
std::cin >> first >> second;
return first + second;
}

int subtractNumbers()
{
std::cout << "Give me two numbers seperated with a space and hit enter, please: ";
int first, second;
std::cin >> first >> second;
return first - second;}

int multiplyNumbers()
{
std::cout << "Give me two numbers seperated with a space and hit enter, please: ";
int first, second;
std::cin >> first >> second;
return first * second;
}

int largestNumbers()
{
std::cout << "Give me two numbers seperated with a space and hit enter, please: ";
int first, second;
std::cin >> first >> second;
if(first > second)
{
return first;
}

// logically, if first isn't larger than second then second is equal
// or larger than first. So we return second
return second;
}

// A very boring kitchen. What do you expect, it's 1:30am!?
void kitchen()
{
std::cout << "This is my kitchen. I do not have a chef, I prefer to prepare my own meals. I" << std::endl
<< "know you ate recently or I would offer you something to eat! Let's leave here." << std::endl;
}


Hopefully seeing a more complete example of functions will help you on your quest.

Share this post


Link to post
Share on other sites
Quote:
Original post by nobodynews
Think of functions like rooms. You can enter a room, but you must also exit it. Most places aren't designed so that you may enter one room, go into another, and then another, forever. Of course there are 'loop' of rooms, but pretend they don't exist. A program is rather like a hallway with rooms that don't lead anywhere. You start at one end of the hallway, enter a room, do your business in there, and then leave the room to go back into the hallway. Then you enter the next room, do your business, and leave. Etc.


That's a pretty strained model.

OP: Try this (zipped HTML E-book).

Share this post


Link to post
Share on other sites
This is my attempt at totally redoing the souce code. Does this work for everone else? The only thing is the Dev-C++ program that I am using after I compiled and ran the program I chose to enter 5 numbers and I put in 5 numbers, but when I hit enter the program looked like it did what it was supposed to, but immediately closed. I just need to know if there is something that I am missing if it does work for other people. I think I may be grasping it a little bit here. Here is the source code:

#include <iostream>

using namespace std;

const int HIGH_NUM = 100;

int lowestnumber ()

{
int answer = HIGH_NUM;

int howMany;
cout << "How many numbers do you want to enter? ";
cin >> howMany;

cout << "Now, please enter " << howMany << " numbers"
<< endl;
for (int a = 0; a < howMany; a++)
{
int num;
cin >> num;
if(num < answer)
{
answer = num;

}

}

return answer;

}

int main (int argc, char **pArgv)

{
cout << endl << "The lowest number that you chose was: " <<
lowestnumber() << endl;

return 0;
}

Share this post


Link to post
Share on other sites
I think this might be near completion, but I just need to know if it works wehn you try to run it. I need to get the program to close if the third choice of Quit is chosen as well. Thanks for the help y'all I actually feel like I am starting to grasp a little more of this day by day.


#include <iostream>

using namespace std;

const int HIGH_NUM = 100;
char sel;

int lowestnumber ()

{
system ("cls");
cout << "1) Find the highest # in a list you choose\n"
<< "2) Find the lowest # in a list you choose\n"
<< "3) Quit\n\n"
<< "What would you like to do?"
<< endl;
cin >> sel;
int answer = HIGH_NUM;

int howMany;
cout << "How many numbers do you want to enter? ";
cin >> howMany;

cout << "Now, please enter " << howMany << " numbers"
<< endl;
for (int a = 0; a < howMany; a++)
{
int num;
cin >> num;
if(num < answer)
{
answer = num;

}

}

return answer;

}

int main (int argc, char **pArgv)

{
cout << endl << "The lowest number that you chose was: " <<
lowestnumber() << endl;

return 0;
}

Share this post


Link to post
Share on other sites
When posting source code, particularly for large amounts, please use the [source] tag:
[source]
// code here
[/source]


Also, in the future, please provide more descriptive subject headings. Most threads are started because someone wants help; giving your thread a more informative title makes it easier for people to identify whether they can help you, improving response rates.



Quote:
I need to get the program to close if the third choice of Quit is chosen as well.

C++ has an if statement. Your objective is dependent on an if conditional. You want something along the lines of:
if (condition)
{
// execute code
}

What's the condition? The input (in sel) is equal to the exit option, 3. Try writing out the code for that.

Share this post


Link to post
Share on other sites
I finally read 3 different books and came up with this. It seems to work, but I can't get it to go back to the main menu after I do A and B. Anything to help remedy this?



using namespace std;

const int SENTINEL = -99;

int main()

{

char choice;
int menu;
int C;
int counter;
int limit;
int number;
int first;
int second;


while (menu!=C);
{
cout << "A - Find the largest # with a known quantity of numbers:\n";
cout << "B - Find the smallest # with an unknown quantity of numbers:\n";
cout << "C - Quit:\n";
cout << "Please enter your choice: ";
cin >> choice;

switch(choice)
{
case 'A':
cout << "Enter the amount of numbers for largest:\n";
cin >> limit;
cout << "Enter the first number:\n";
cin >> first;
for (counter = 1; counter < limit; counter++)
{
cout << "Enter the next number:\n";
cin >> second;
if(first < second)
first = second;
}
cout << "The largest number is: \n " << first << endl << endl;
break;
case 'B':
cout << "Enter the first number (-99 to end):\n";
cin >> first;
cout << endl;
while (first!= SENTINEL)
{
cout << "Enter the next number (-99 to end):\n";
cin >> second;
cout << endl;
if(second == SENTINEL)
break;
if(first > second)
first = second;
}
if(first!= SENTINEL)
cout << "The smallest number is: \n " << first << endl << endl;
break;
}
}

return 0;
}


//EDIT: Added source tags to your post. - Kaz.

[Edited by - Kazgoroth on November 12, 2006 11:59:22 PM]

Share this post


Link to post
Share on other sites
You'll need to take a look at your while loop, there are two problems with it. The first is a simple problem with the syntax. As a hint, the basic structure of a while loop in C++ looks like this:
while(condition)
{
//do stuff in here
}
Take a look at yours and see if you can spot what's different about it.


You also have two problems with the actual condition you're checking at the top of the loop:
  • To solve the first one, look at what variable you're storing the user's choice in, and what variable you're checking.
  • The second is what you're checking against and could be tricky to figure out, so as a big hint, a character literal (that is, a character you've hardcoded in) looks like this:
    'X' // like the ones you've used as the expressions in your case statement
    Without the single-quotes around the character the compiler will think you're talking about a variable (and you've actually added a variable you don't need, which probably got rid of the error for you but won't behave correctly).


So, take a good look at your while loop and see if you can fix those three problems. If you solve them correctly you'll also find that you'll be able to remove three unused variables you've declared at the beginning of main.




You also shouldn't try to use an uninitialised variable in an expression (such as a loop condition) - that is, a variable which you've declared but which doesn't have a value yet. Your while loop checks it's condition once before the user is asked to input a choice, and while this might work it's actually undefined behavior, because you don't know what value that variable will have.

The neatest solution to this is to use a slightly different type of loop known as a do...while loop. In a do...while loop the condition isn't checked until the end of the loop - this means that the code inside the loop will always run at least once, and in your case would avoid checking the uninitialised variable. A basic do...while loop looks like this:
do{
// do stuff here
} while(condition);


Hope that helps, you're pretty close now. [smile]

Share this post


Link to post
Share on other sites
Um yeah you got me confusedright there. I'm gonna have to look at it some more and try it again I guess. If I fix those problems will the program go back to the A,B,C menu after completing the loop?

Share this post


Link to post
Share on other sites
Quote:
Original post by benslancer
If I fix those problems will the program go back to the A,B,C menu after completing the loop?
Yes.

Apart from the bit about changing the type of loop you only need to look at the first line of your while loop:
while (menu!=C);
Specifically, look at:
- The overall structure of a while loop. Is anything missing or anything there that shouldn't be?
- The variable (menu) you're using in the expression, and the variable you're storing the user's selection in.
- What you're comparing that variable to. What should the C in your expression be, and what does the compiler think it is? Have a think about what I explained about character literals. You want to check against a certain character, so it needs to be either a character literal or a variable of type char.


do...while loops can sometimes be confusing for beginners, but I suspect that your compiler will let you ignore that particular problem if you can fix the other ones, so try those first and if you can't figure out how to implement the do...while we'll help you out with it and see if we can come up with a better explanation of why you'd want to use it.

Share this post


Link to post
Share on other sites
OK the last part that I did, I was wondering how would I make parameters for A and B? Also what exactly is the purpose of a parameter? Is it a way of just separating the functions or what?

Share this post


Link to post
Share on other sites

This topic is 4045 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this