Loops

Write your question here.
For this small assignment, I have to return the second smallest number depending on the users input. The only thing I am supposed to use to solve this problem is while loops. It is supposed to be extremely basic. What I am trying to do is store the smallest number in a variable, and store the second smallest into another variable. I am sometimes getting the correct answer when I try random numbers and other time's I am not, which makes me think my code is wrong. Any help will be greatly appreciated, thank you!

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
#include <iostream>
#include <limits.h>
using namespace std;

int main () {
int a,x,y,z;
a = INT_MAX


while (cin >> y) {

 if (y < a) {
  x = y;
}
 if (y > a) {
  z = y;
}

 if (z < x) {
  a = z;
}

}

cout << "The second smallest number is: " << a << endl;
return 0;
}


First, the names of your variables do not tell what those variables are for.
Second, your indentation could be more expressive. As is, it is hard to see what is in the loop.
Third, your statement on line 7 lacks a semicolon.

Logically, there are three possibilities when a new value comes in:
1. The value is the new smallest. Therefore, the previous smallest is now second smallest. Both smallest and second do update.
2. The value is not new smallest, but still smaller than the current second. The second must update.
3. Neither needs update.

#include <iostream>
#include <limits.h>
using namespace std;

int main () {
int a,x,y,z;
a = INT_MAX;


while (cin >> y) {

if (y < a) {
x = y;
}
if (y > a) {
z = y;
}

if (z < x) {
a = z;
}

}

cout << "The second smallest number is: " << a << endl;
return 0;
}


I understand the idea that I have to update my numbers but I have no idea where to start

#include <iostream>
#include <limits.h>

int main() {
int lowest, second_lowest, input;
lowest = second_lowest = INT_MAX;
input = 0;
std::cout << "Enter your numbers and enter -99 when done" << std::endl;
while (input != -99)
{
std::cin >> input;
if (input != -99)
{
if (input < lowest || input < second_lowest)
{
if (input < lowest)
{
second_lowest = lowest;
lowest = input;
}
else if (input < second_lowest && input > lowest)
{
second_lowest = input;
}
}
}
}

std::cout << "The second smallest number is: " << second_lowest << std::endl;
return 0;
}

Here is what i did. Naming variables with names that determine what they do will go a long way to making the task easier. See if this gets you going, it seems fine to me. Happy programming!
Code tags would be nice. Color and indentation do help "seeing" the code.

The -99 was not in the picture and you test it twice. That can be simplified.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <climits> // C++ version

int main() {
  // initialize on declaration
  int lowest = INT_MAX;
  int second_lowest = INT_MAX;
  int value = 0;

  std::cout << "Enter your numbers and enter -99 when done\n";
  while ( std::cin >> input && input != -99 ) {
    if ( input < lowest || input < second_lowest ) {
      if ( input < lowest ) {
        second_lowest = lowest;
        lowest = input;
      }
      else if ( input < second_lowest && input > lowest ) {
        second_lowest = input;
      }
    }
  }
  std::cout << "The second smallest number is: " << second_lowest << '\n';
  return 0;
}

There are still unnecessary conditions in that. They can be simplified.
@keskiverto thanks for the refining..it really looks much better now :), i also need to work on my conditions.
Topic archived. No new replies allowed.