Can you take a few min to look at my c++ code....

Hi there, i am a c++ newbie.I was asked to write a function that takes in 3 integer parameters and returns two smallest integers among them.
Here is what i done so far...though there is 2 error C2143 which i really dun noe whats wrong T_T.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
int main()
{
	int i, small;
	int num[3];
	cout << "Enter #1 integer: "<< endl;
		cin >> num[0];
	cout << "Enter #2 integer: "<< endl;
		cin >> num[1];
	cout << " Enter #3 integer: "<< endl;
	cin >> num[2];
	
	small=num[0];
    for(i=1,i<4,++i)
	{
		if(num[i] < small) small= num[i];
	}
	cout << "Smallest number: " << small << endl;
	return 0;
}

Please tell me i am on the right track and why there is a error...
And any hints or good websites on how to find the second smallest number would be greatly appreciated.
What is error message? (Not the #, the message...)
This is the only obvious error I see.
for(i=1,i<4,++i)

Use semicolons between the for statements, not comma.
@firedraco
sry..i am a newbie..i dun understand what your saying..T_T
@kennpofighter
omg..i am so stupid...thnks man.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
int main()
{
	int i, small;
	int num[3];
	cout << "Enter #1 integer: "<< endl;
		cin >> num[0];
	cout << "Enter #2 integer: "<< endl;
		cin >> num[1];
	cout << "Enter #3 integer: "<< endl;
	cin >> num[2];
	
	small=num[0];
    for(i=1;i<3;++i)
	{
		if(num[i] < small) small= num[i];
	}
	cout << "Smallest number: " << small << endl;
	return 0;
}

finnaly got it right...
now i need to find the second smallest number which i have no idea how to begin with, can anyone give me hints?
firedraco was asking for the error message (for example "expected ; before ,"), not the error code (C2143).
Never post the error code by itself. It's very unlikely anyone will be able to figure out the error message from it. However, including the error code along with the error message makes it easier for search engines.
Last edited on
Why don't you just sort the array? it is so small it'll be super fast and then the first two numbers will be the two smallest. std::sort will do the trick.
std::sort(num, num + 3);

After that, elements 0,1 are the smallest, next smallest numbers respectively.
Topic archived. No new replies allowed.