My Program Works but Im not sure why!

Hello I am new to the forum, I am teaching myself C++ using the book "Programming Principles and Practice Using C++". I am doing one of the drills were you are asked to create a program that reads in ints and then displays them, states which has the greatest value and which has the least value. That is the easy part. The part I am having trouble with is stating when two of the integers entered are equal.

I got the program to work by using a while loop and an if statement, however I am not sure why this is need. I kind of just took a shot in the dark and it worked, I am not sure why the first while loop is needed, it seems a little repetitive to me.

Any help would be appreciated.

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
 
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

inline void keep_window_open(){ char ch; cin >> ch;}

int main(){

     vector <int> numbers;
     int previous;
     int x;
    
 while (cin >> x){

     if ( x != previous)
          numbers.push_back(x);

     else if (x == previous)
          previous = x;
}

int a;
std :: sort (numbers.begin(), numbers.end());

for (int a : numbers)
     cout << a << '\n';
     cout <<"Smallest Number is " << numbers[0] << '\n';
     cout << "Largest Number is " << numbers[numbers.size()-1]<<'\n';

for (int i; i < numbers.size(); ++i)
     if (numbers[i] == numbers[i+1])
     cout << "This number is repeated " << numbers[i];

return 0;
}
     
Last edited on
Never mind, I tried a different compiler and it seems to work without the if statement in the first while loop. I'm not sure why my compiler was giving me trouble.
If I'm understanding you correctly, the vector of numbers shouldn't contain any duplicate numbers. Each element is supposed to be unique. Is this correct?

There are some problems with your solution:

1.) You access the variable previous on lines 19 and 22, but you haven't initialized it. The variable contains garbage. This is only really a problem until you assign a value to it on line 23, but a problem nonetheless.

2.) The previous variable only keeps track of the last number entered. This means that a user could input the sequence 1, 2, 1, effectively pushing duplicate numbers onto the vector. This isn't really a huge concern, since the vector will be sorted, and duplicate values will not affect your final results, but unless that is explicitly what you had in mind, it is a flaw in your design.

EDIT - just took a closer look at your code. Obviously, it's not a concern if there exist duplicates. Your for loop on lines 34-36, however, assumes the vector is sorted, which is a bit iffy. It also deserves some opening and closing braces. Additionally, if I enter the sequence 1, 1, 1, your program will state that "1" is repeated twice - not that the number exists twice in the vector, but it will literally print the same message twice.

3.) On line 34, your integer i hasn't been initialized. It contains garbage. You then use i as an index for every iteration of the loop. You also access it in the for loop's condition, and increment garbage + 1 for every iteration.
Last edited on
Awesome thanks for the help! I haven't been initializing my ints because my compiler lets me get away with it, but your right I should be more thorough.
Topic archived. No new replies allowed.