Error in If else statement

Hi everyone, I new to C++ and seeking for some advice from c++ experts here for the following task. This post is a continuation of my previous post (http://www.cplusplus.com/forum/beginner/261746/). Looking at the previous post was too long ago, I decide to write a new post.

Matrix data (10x3) were provided to test the effectiveness of this coding.
0 9 8 7 6 5 4 3 2 1
0 1 2 3 4 5 6 7 8 9
5 4 3 2 1 0 9 8 7 6

The task as follows:
1. Need to choose one random number in the range 3 to 9. (done)
For example, random number (r1) = 3

2. Find position of this random number for each row. (done)
For example,
For row 1, random number 3 was found at position 8.
For row 2, random number 3 was found at position 4.
For row 3, random number 3 was found at position 3.

3. Do neighbor swapping with condition for each row. (Error here line 64-71)
Must swap random number (r1) with it neighbor which is its next position. But can't swap with number (0, 1, 2). If this three number appear. Must swap with position before random number.

For example (Row 1):
0 9 8 7 6 5 4 3 2 1
Random number 3 at position 8. Since next position is number 2. We need to swap number 3 with number 4 (position before number 3).

4. Display new vector after swapping.
For example (Row 1):
0 9 8 7 6 5 3 4 2 1

I don't know how to correct the errors. Hopefully someone can teach me or give some advice. Thank you for your kindness.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>		// cout
#include <fstream>
#include <sstream>
#include <ctime>		// time
#include <cstdlib>		// srand, rand()
#include <algorithm>    // find(), iter_swap()
#include <vector>       // vector

using namespace std;

int main()
{
	// Initialize random seed:
	srand(time(NULL));

	// Read data from text file
	ifstream txtdata("data.txt");
	vector<vector<int>> vec;

	for (string line; getline(txtdata, line); )
	{
		vec.push_back(vector<int>());
		istringstream iss(line);
		for (int n; iss >> n; )
			vec.back().push_back(n);
	}

	// Print Original Vector
	cout << "Original vector :" << endl;
	for (size_t i = 0; i < vec.size(); i++)
	{
		for (size_t j = 0; j < vec[i].size(); j++)
		{
			cout << vec[i][j] << " ";
		}
		cout << "\n";
	}
	cout << "\n";

        // Choose random number in the range 3 to 9
	int r1 = rand() % 6 + 3;

	cout << "First random number = " << r1 << endl;
	cout << "\n";

	for (size_t i = 0; i < vec.size(); i++)
	{
		// Iterator used to store the position of searched element
		vector<int>::iterator it, ik;

		// Find position of first random number, r1
		it = find(vec[i].begin(), vec[i].end(), r1);
		if (it != vec[i].end())
		{
			cout << "For row " << i + 1 << "\n";
			cout << "First random number " << r1 << " was found at position ";
			cout << it - vec[i].begin() + 1 << "\n";
		}

		// Condition to swap
		// 1. Must swap it with it + 1
		// 2. But, if (it + 1) equal to {0, 1 or 2). Swap with it - 1.

		if (vec[it + 1] == 0) || (vec[it + 1] == 1) || (vec[it + 1] == 2);
		{
			ik = it - 1;
		} 
		else
		{
			ik = it + 1;
		}

		// Swap them
		if (it != vec[i].end() && ik != vec[i].end())
		{
			iter_swap(it, ik);
		}
	}
	cout << "\n";

	// Print New Vector
	cout << "New vector after swapping :" << "\n";
	for (size_t i = 0; i < vec.size(); i++)
	{
		for (size_t j = 0; j < vec[i].size(); j++)
		{
			cout << vec[i][j] << " ";
		}
		cout << "\n";
	}
	cout << "\n";

	return 0;
}
Last edited on
I don't know how to correct the errors.

What errors?

If you are going to write a new topic because a previous topic was too long include the exact text of the errors you receive in the new topic.
Iterators are more like pointers. They were intentionally defined so that pointers satisfy that definition: pointers are a kind of iterator.

To access the element an iterator refers to, say *my_iterator, not my_vector[my_iterator].

Beware out-of-bounds access. Your code has the potential to attempt to access elements that don't exist, for example, if your random number isn't found in a row. That will yield wrong results.

It is very likely that your standard library implementation comes with checked containers and iterators - which are very very helpful for identifying (and fixing) certain hidden defects in your code. Those features are typically off by default; the details of how to turn them on your depend on the compiler you're using.

Google search "debug mode c++ standard library" along with the name of your compiler, or mention it here and someone will tell you how to turn those features on.
Last edited on
Hi Furry Guy,

What errors?

If you are going to write a new topic because a previous topic was too long include the exact text of the errors you receive in the new topic.


Sorry for my mistake. The error is as follows:

error C2679: binary '[': no operator found which takes a right-hand operand of type 'std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>' (or there is no acceptable conversion)

error C2059: syntax error: '||'

error C2143: syntax error: missing ';' before '{'

error C2181: illegal else without matching if

Hopefully someone can give advice to me. Thank you :)
You are missing parentheses on line 64.
Hi mbozzi,

You are missing parentheses on line 64


I had improved the code by include the parentheses. But errors C2679 and error C2088: '[': illegal for class occured.

How to fix it. Any suggestions are highly appreciated.

I'm using Visual Studio 2017.
Remove the semi-colon at the end of line 64 as well as adding parentheses.
Try
if (*(it + 1) == 0 || *(it + 1) == 1 || *(it + 1) == 2)
Hi mbozzi, furry guy and lastchance,

I had fix my code based on your suggestions and it worked. Thanks for your help.
Thank you so much.

Wish all of you have a nice day ahead :)
Last edited on
Topic archived. No new replies allowed.