Inputing Strings

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.

The code in question:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <string>

using namespace 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;
           }
		   else if (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.
Last edited on
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)?
Last edited on
If the input is "c", it's still not "C". So one of the conditions will be true, which will make the entire expression true.
I've realized my error and fixed the program. It works perfectly now how it should work. Thank you so much to jamesmk and filipe for your help.
Topic archived. No new replies allowed.