help required friends

Pages: 12
Sep 17, 2013 at 2:01pm
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int n,smallest;
cout << "Enter A Number :" << endl;
cin >> n;
if (n >= 0)
smallest=n;
while (n >= 0)
{
cout << "Enter A Number :" << endl;
cin >> n;
if (n >= 0 && n < smallest)
smallest=n;
}
cout << "Smallest = " << smallest << endl;
getch();
return 0;
}
Sep 17, 2013 at 2:05pm
this program for smallest number and i also want to find 2nd smallest ...for this what will be the condition.....plz plz plz help this time
Sep 17, 2013 at 2:31pm
Did you do it by yourself?
Sep 17, 2013 at 2:47pm
you need another variable like n let's say n1. the smallest goes still to n. if it's greater than n but smaller than n1 it goes to n1
Sep 17, 2013 at 2:49pm
chriscpp.......i do it with own effort
Sep 17, 2013 at 2:53pm
coder777........you mean i write another int in this programm


















Sep 17, 2013 at 3:00pm
Yes
Sep 17, 2013 at 3:00pm
i also add second variable but when i run it he telled about only smallest
and at second smallest he give ans zero the number which i inialized the variable
Last edited on Sep 17, 2013 at 3:14pm
Sep 17, 2013 at 3:09pm
Ok, sorry. I see that I confused 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
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int n,smallest,smallest2;
cout << "Enter A Number :" << endl;
cin >> n;
smallest=n;
smallest2=n; // ok
while (n >= 0)
{
cout << "Enter A Number :" << endl;
cin >> n;
if (n >= 0)
{
if(n < smallest)
smallest=n;
else if(n < smallest2))
smallest2 = n;
}
}
cout << "Smallest = " << smallest << endl;
cout << "2nd Smallest = " << smallest2 << endl;
getch();
return 0;
} 
[EDIT]forgot to initialize smallest2
Last edited on Sep 17, 2013 at 3:15pm
Sep 17, 2013 at 3:35pm
coder77.......which code you write also gave error.......error is that smallest and smallest2 values are same
Sep 17, 2013 at 3:51pm
coder77....plz reply
Sep 17, 2013 at 4:06pm
Sounds like your trying to:

- Get two numbers
- Find out which one is smaller
- Report the smaller and logically the bigger one.

Is this correct?
Sep 17, 2013 at 4:09pm
yes.....correct
Sep 17, 2013 at 4:10pm
whenever u'll enter smaller number than previously stored in smallest that means previous value in smallest is 2nd smallest number.

1
2
3
4
5
6
7
8
if(n < smallest)
{
smallest2=smallest;
smallest=n;
}
else if(n < smallest2))
smallest2 = n;
}
Sep 17, 2013 at 4:25pm
dukhi X.......isko solve kesay kro
Sep 17, 2013 at 4:36pm
friend............plz solution bta do
below is code....what is problem in code
Sep 17, 2013 at 4:38pm
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int n,smallest,smallest2;
cout << "Enter A Number :" << endl;
cin >> n;
smallest=n;
smallest2=n; // ok
while (n >= 0)
{
cout << "Enter A Number :" << endl;
cin >> n;
if (n >= 0)
{
if(n < smallest)
smallest=n;
else if(n < smallest2))
smallest2 = n;
}
}
cout << "Smallest = " << smallest << endl;
cout << "2nd Smallest = " << smallest2 << endl;
getch();
return 0;
}
Sep 17, 2013 at 4:44pm
delete line 17 to line 20 from coder777s code and write these lines there.

1
2
3
4
5
6
7
8
if(n < smallest)
{
smallest2=smallest;
smallest=n;
}
else if(n < smallest2))
smallest2 = n;
}


syntax error in you code:

else if(n < smallest2)) // you are using )) 2 parantheses to close.error
Last edited on Sep 17, 2013 at 4:50pm
Sep 17, 2013 at 4:55pm
dost......smallest and smallest2 values same ati han
Sep 17, 2013 at 5:08pm
yar......ya work nhen kerta.....6,5 errors atay han.....dukhi x
Pages: 12