0

I'm getting this error on this line of code. Please help me solve it.

for (int i=0; i=((Main.size())-1); i++) {

Main code..

#include <istream>
#include <fstream>
#include <vector>
#include <algorithm>

#include "data.hpp"

using namespace std;

int main() {

vector<double> Main;
int count;
string lineData;
double tmp;

ifstream myfile ("sheffield.data", ios::in);

//double number;  

myfile >> count;
for(int i = 0; i < count; i++) {
    myfile >> tmp;
    Main.push_back(tmp);
    cout << count;
}

cout << "Numbers:\n";
cout << Main.size();
for (int i=0; i=((Main.size())-1); i++) {
    cout << Main[i] << '\n';
}

cin.get(); 
return 0;

}

user3445723
  • 21
  • 2
  • 5
  • 1
    Add more code. I guess Main is a std::vector or std::list? This means it returns a size_t, or unsigned integer, where you are using a int. Also, why do you assign a value to i in the second column? – MatthiasB Mar 21 '14 at 09:31
  • Independent of the rest of the code, this loop is wrong. It will only terminate if `Main` has exactly one element. – filmor Mar 21 '14 at 09:32
  • Possible duplicate of [C: how can I fix warnings like: "comparison between signed and unsigned"](http://stackoverflow.com/questions/859943/c-how-can-i-fix-warnings-like-comparison-between-signed-and-unsigned) – phuclv Mar 24 '17 at 13:33

3 Answers3

4

The type of value returned by member function size is unsigned while i is declared as signed.

for (int i=0; i=((Main.size())-1); i++) {
    cout << Main[i] << '\n';
}

So instead of int i use at least unsigned int i or size_t i .It would be even better if you would use the type that is defined in class std::vector that is std::vector<double>::size_type i

Also in the condition part of the loop you are using the assignment operator = instead of the comparison operator == But if you would update the operator the condition will be wrong until size() will be equal to 0. Instead of operator == you have to use <=

The loop should look the following way

for ( std::vector<double>::size_type i = 0; i < Main.size(); i++ ) {
    cout << Main[i] << '\n';
}

Also instead this for statement you could use the range based for statement that looks much simpler. For example

for ( double x : Main ) {
    cout << x << '\n';
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
2

The i is of type int(signed) and the result of Main.size() of unsigned int, probably size_t depending on the tye of Main. If you compare them in the for-loop, you have your explanation.

But be aware: a single = is an assignment, not a comparison.

Fix(probably):

for (unsigned int i=0; i < Main.size(); i++) {
tgmath
  • 12,813
  • 2
  • 16
  • 24
0

You could also consider using iterators instead:

cout << "Numbers:\n";
cout << Main.size();
for (vector<double>::const_iterator i = Main.begin(); i != Main.end(); ++i) {
    cout << *i << '\n';
}

(And as a side note, naming a variable (Main) with a capital letter is somewhat confusing!)

benjymous
  • 2,102
  • 1
  • 14
  • 21