Is your problem just how to output something or is there something wrong with the program?
Output on the standard output is possible with: cout<<small;
The following is how I'd do it for smallest and second smallest number. Maybe it helps you in some way, it doesn't work perfectly but it works for all input smaller than 0.
#include<iostream>
using namespace std;
int main() {
int smallest = 0;
int second_smallest = 0;
int input;
bool program_continuation = true;
char yes_or_no;
while (program_continuation) {
cin>>input;
if (input < second_smallest) {
if (input <= smallest) {
second_smallest = smallest;
smallest = input;
}
else {
second_smallest = input;
}
}
cout<<"The current smallest number is: "<<smallest<<"\n";
cout<<"The current second smallest number is :"<<second_smallest<<"\n";
cout<<"Do you want to input another number?";
cout<<"Please type y for yes or n for no."<<"\n";
cin>>yes_or_no;
First the syntax for (; cin >> num ;) I guess is some way to loop as long there is input.Why don't you use while(cin >> num) instead?
Anyway you should do two checks for each case (smaller and bigger).
Compare with the bigger, if needed change it and second_bigger also, else compare with second_bigger if needed change it
Do the same for smaller.
1 2
if ( num < big)
small = num;
If num < big it's true you shouldn't do any change! You have the condition wrong here.
I modified the program to below but if i put second_number = 0 and my first input is less than 0 then i will lose first entry. do you have any solution for this.
What you could do is not initializing smallest and second_smallest, but assign to them the first two values entered by the user and only then going into the for loop for checking if the next input is smaller. I'm not sure, but I think this should fix the problem.
So it would be something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
double smallest = 0;
double second_smallest = 0;
double input;
cin>>input;
smallest = input;
cin>>input;
if (input < smallest) {
second_smallest = smallest;
smallest = input;
}
else {
second_smallest = input;
}
//Rest as you already have it from line 8 on
double max = 0; // largest # seen so far
double max2 = 0; // second largest # seen so far
double min = 0; // smallest # seen so far
double min2 = 0; // second smallest # seen so far
unsigned count = 0; // # of numbers seen so far
for(;;){
double x;
cin >>x;
if( !cin ) break;
count++;
if( count == 1 || x > max ){
max2 = max;
max = x;
}elseif( count == 2 || x > max2 ){
max2 = x;
}
if( count == 1 || x < min ){
min2 = min;
min = x;
}elseif( count == 2 || x < min2 ){
min2 = x;
}
}
if( count >= 2 )
cout <<min2 <<' ' <<max2 <<endl;
ignoreWord();