I have a program that I've been given as homework and I have all the logic down but I'm getting one huge problem with it and I don't know where I've gone wrong with it. Just to clarify, if you enter A, the program asks you how many numbers you are going to input and then gets the highest number of those numbers. If you enter B, you are asked to enter numbers until you hit -99 to end find the lowest number out of all those numbers entered. If you enter C, the program closes.
I have A and B working but I keep getting a problem where the question is asked twice and then when I try to input "C" it just says invalid input and keeps asking for A, B, or C.
#include <iostream>
#include <string>
usingnamespace std;
main ()
{
int stop;
string input = "input";
cout << "Please make a selection from the following options:" << endl;
cout << "A - Find the largest # with a known quantity of numbers." << endl;
cout << "B - Find the smallest # with an unknown quantity of numbers." << endl;
cout << "C - Quit." << endl;
cout << "Please enter your choice: ";
getline (cin, input, '\n');
while ( input.compare("c") != 0 || input.compare("C") != 0)
{
if ( input.compare ("a") == 0 || input.compare("A") == 0)
{
int count, highNumA, limit, num;
count = 0;
highNumA = -99999999;
cout << "You have selected A." << endl << endl;
cout << "How many numbers are you going to input? ";
cin >> limit;
while (count != limit)
{
count++;
cout << "Please enter a number: ";
cin >> num;
if (num > highNumA)
{
highNumA = num;
}
}
cout << "Your highest number was: " << highNumA << endl << endl;
}
elseif (input.compare ("b") == 0 || input.compare("B") == 0)
{
int lowNumB, in;
lowNumB = 99999999;
in = 99999999;
cout << "You have selected B." << endl;
while (in != -99)
{
cout << "Enter each number in the sequence (-99 to exit): ";
cin >> in;
if (in != -99)
{
if (in < lowNumB)
{
lowNumB = in;
}
}
}
cout << "The lowest number in the sequence was: " << lowNumB << endl << endl;
}
else
{
cout << "Please make another selection." << endl <<endl;
}
cout << "Please make a selection from the following options:" << endl;
cout << "A - Find the largest # with a known quantity of numbers." << endl;
cout << "B - Find the smallest # with an unknown quantity of numbers." << endl;
cout << "C - Quit." << endl;
cout << "Please enter your choice: ";
cin >> input;
cout << "you have chosen " << input << endl;
}
cout << endl << endl << endl << "This is the end of the program.";
cin >> stop;
return 0;
}
If anyone can help me with this it would be highly appreciated. Thank you.
Edit:
The int "stop" is just there so I don't have to add a system pause.
while ( input.compare("c") != 0 || input.compare("C") != 0)
You have a logic error in your while loop. Ask yourself what is the practical difference between OR and AND. And what you're telling the computer here.
Well from my understanding that line in plain english means:
While input is not "c" OR input is not "C" then { rest of code }. I'm telling the computer to keep looping as long as the input isn't the letter "c". But I always check if the letter is "c" within the loop. Am I approaching this in the wrong way (or at least not the easy way)?