What's wrong with this program, and how to fix it?
Hi,
When I compile this program, there's always a message on the screen as below:
"exer3.cpp: In function ‘int main()’:
exer3.cpp:13: warning: comparison between signed and unsigned integer expressions
exer3.cpp:18: warning: comparison between signed and unsigned integer expressions
exer3.cpp:22: warning: comparison between signed and unsigned integer expressions
exer3.cpp:27: warning: comparison between signed and unsigned integer expressions
exer3.cpp:35: warning: comparison between si "
Despite this, I am able to run the program and have the expected results. However, I don't know how to fix the areas of the program which the warnings point to. Could you please help? Thanks a lot!
Code:
// Exercise 3/Chapter 3: Programming: Principles and Practice using C++
#include "std_lib_facilities.h"
int main()
{
vector <double> myvector;
double number = 0;
while (cin >> number)
myvector.push_back(number);
double sum = 0;
int i;
for (i = 0; i < myvector.size(); ++i)
sum += myvector[i];
// compute distance
vector <double> mydistance;
for (i = 0; i < (myvector.size()-1); ++i)
mydistance.push_back(myvector[i+1]- myvector[i]);
cout << "Numbers: " << endl;
for (i = 0; i < myvector.size()-1; ++i) {
cout << " " << myvector[i];
}
cout << "\nDistance: " << endl;
for (i = 0; i < mydistance.size(); ++i) {
cout <<" " << mydistance[i];
}
// find min, max
double max, min;
max = min = myvector[0];
cout << '\n';
for (i = 0; i < myvector.size(); ++i) {
if (myvector[i] > max) {
max = myvector[i];
cout << "Max = " << max << "\tMin = " << min << endl;
min = max;
}
else if (myvector[i] < max) {
min = myvector[i];
cout << "Max = " << max << "\tMin = " << min << endl;
max = min;
}
}
}