6 Replies - 306 Views - Last Post: 01 October 2019 - 03:25 PM Rate Topic: -----

#1 Adriel16   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 21-May 13

How do I use an iterator or for loop to display a vector as an int?

Posted 28 September 2019 - 03:23 AM

Hello, I'm writing a code that makes a vector of circles and then the program reads from a file coordinates of 5 circles which is then displayed by using a for loop. I then have to display the vector as an int using a function of the Class Circle called getCount(). I used for loops and auto iter but they have not worked because the compiler has come back with an error that says I reach the end of the function and there is no return. My question(s), for the For Loop to work do I need to place the return statement and then a break statement? For the auto iter function to work do I need to use push_back and if I do need to how would i go about using push_back? Do I need to pull the information from the file again, fill the vector and then display it by returning a variable?
main.cpp
#include "pch.h"
#include<iostream>
#include "Circle.h"
#include <fstream>
#include <vector>
#include<cstdlib>

#include <fstream>
#include <sstream>

using namespace std;
int const SIZE = 7;

void inputData(vector<Circle> &circleVector, string filename) {
	//create the input string stream called instream
	ifstream input;
	string line;
	int count = 0;
	string fname, lname;
	int x = 0;
	int y = 0;
	int r = 0;

	input.open("dataLab4.txt");
	
	input.precision(2);

	if (!input) {
		cout << "error" << endl;
	}
	else {
		while (getline(input, line) && count < SIZE) {

			stringstream instream(line);
			
			instream >> x >> y >> r;
			
			Circle newCircle(x, y, r);
			
			circleVector.push_back(newCircle);
			
			count++;
		}
	}

	//open file
	//if there is a problem opening the file, throw an exception and exit - use a try catch statement
	//otherwise
	//use getline to read data line feed to the instream
	//create a circle using the data in the instream
	//add the circles to the vector
	//keep doing this until all the data in the file is read

}

int main() {
	

	vector<Circle> circleVector;
	string filename;
	int i = 0;
	int amount = circleVector.size();
	//Opening and filling vector
	cout << "The circles created are:" << endl;
	inputData(circleVector, filename);


	//Display cirlces
	for (int i = 0; i < circleVector.size(); i++) {
		cout << circleVector[i].toString() << endl;
	}

	


	cout << "The number of circles, using getCount method is "  << endl;
	cout << "The numher of circles, using vetor size method is " << circleVector.size() << endl;

	//clear vector
	for (auto iter = circleVector.begin(); iter != circleVector.end(); iter++) {
		if (circleVector.begin() < circleVector.end()) {
			circleVector.erase(iter);
			iter--;
		}
	}

	cout << "The number of circles, using getCount method is " << endl;
	cout << "The number of circles remaining is ";

	return 0;
}



#include "pch.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include "Circle.h"
#include <cmath>
#include "math.h"
#include <string>
#include <sstream>
#include <vector>
using namespace std;
Circle::Circle() {
	x = 0;
	y = 0;
	radius = 1;
}

Circle::Circle(int x, int y, int radius) {
	this->x = x;
	this->y = y;
	this->radius = radius;

}

int Circle::getX() {
	return x;

}

int Circle::getY() {
	return y;
}

int Circle::getRadius() {
	return radius;
}

void Circle::setX(int newX) {
	x = newX;
}

void Circle::setY(int newY) {
	y = newY;

}
void Circle::setRadius(int newRadius) {
	radius = newRadius;
}

double Circle::getArea() {
	return 3.14 * radius * radius;

}

double Circle::getCircumference() {
	return 2 * 3.14 * radius;

}
string Circle::toString() {
	return "(" + to_string(getX()) + "," + to_string(getY()) + "):" + to_string(getRadius());
}



double Circle::getDistance(Circle other) {
	return sqrt(pow((x - other.getX()), 2) + pow((y - other.getY()), 2));

}

void Circle::moveTo(int newX, int newY) {
	setX(newX);
	setY(newY);

}
bool Circle::intersects(Circle other) {
	return (getRadius() + other.radius >= getDistance(other));
}

void Circle::resize(double scale) {
	setRadius(getRadius()*scale);
}

Circle Circle::Rresize(int scales)
{
	return (Circle(x, y, (getRadius()*scales)));
}

int Circle::getCount() {
	vector<Circle> circleVector;
	for (auto iter = circleVector.begin(); i < circleVector.end(); i++) {
		cout << *iter << endl;
	}
	
}


bool Circle::greaterThan(Circle other) {
	if (getRadius() > other.getRadius()) {
		return true;
	}
	else {
		return false;
	}


}




Is This A Good Question/Topic? 0
  • +

Replies To: How do I use an iterator or for loop to display a vector as an int?

#2 jimblumberg   User is offline

  • member icon

Reputation: 5767
  • View blog
  • Posts: 17,655
  • Joined: 25-December 09

Re: How do I use an iterator or for loop to display a vector as an int?

Posted 28 September 2019 - 05:11 AM

Quote

I used for loops and auto iter but they have not worked because the compiler has come back with an error that says I reach the end of the function and there is no return.


You have a function that you promised to return a value so you must return something from that function.

int Circle::getCount() {
    vector<Circle> circleVector;
    int value = 0;
	
    for (auto iter = circleVector.begin(); i < circleVector.end(); i++) {
        cout << *iter << endl;
        ++value;
    }
    return value;
}


By the way what is the purpose of this function?

Do you realize that circleVector is empty in this function;

Do you realize that there is an "easier" syntax you could use, if you're using one of the more current C++ standards, for that loop:
for(auto& iter : circleVector)
{
    // Do what ever.
}


But since you're using a std::vector you really don't need to iterate through the vector to get the vector's size, just return the vectors.size() instead.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Adriel16   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 21-May 13

Re: How do I use an iterator or for loop to display a vector as an int?

Posted 28 September 2019 - 12:27 PM

Hi Jim,

In my code I use
circleVector.size()
when I cout the following statement
cout << "The number of circles, using vetor size method is " << circleVector.size() << endl;
. The getcount method should do the same by getting called just above the cout statement

I haven't learned the following code yet just the one above I used in the code.
for(auto& iter : circleVector)
{

    // Do what ever.
}



This part
cout << *iter << endl;
is throwing an error. It states "no operator "<<" matches these operands operand types are: std::iostream << Circle". I've seen examples from cpp.com and geeksforgeeks use that statement. My compiler is Visual Studio 2017. Do I need another header?

This post has been edited by Adriel16: 28 September 2019 - 12:34 PM

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is offline

  • member icon

Reputation: 5767
  • View blog
  • Posts: 17,655
  • Joined: 25-December 09

Re: How do I use an iterator or for loop to display a vector as an int?

Posted 28 September 2019 - 02:24 PM

Quote

Do I need another header?

No you need to make an overload of the output operator<< for your class. By the way why are you trying to print anything in the getcount() function?

Quote

I haven't learned the following code yet just the one above I used in the code.

So? No time like the present to learn. That is called a "ranged based loop" for your googlieness.

Quote

The getcount method should do the same by getting called just above the cout statement

Okay, but why are you trying to iterate through the vector to get a count instead of just returning std::vector.size()?


Jim
Was This Post Helpful? 0
  • +
  • -

#5 Adriel16   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 21-May 13

Re: How do I use an iterator or for loop to display a vector as an int?

Posted 01 October 2019 - 02:55 PM

Sorry for the late reply. To answer your question about why I'm wanting to use the getcount method is because of my professor who wants us to get the same count using different ways. Why? I do not know.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg   User is offline

  • member icon

Reputation: 5767
  • View blog
  • Posts: 17,655
  • Joined: 25-December 09

Re: How do I use an iterator or for loop to display a vector as an int?

Posted 01 October 2019 - 03:01 PM

Quote

To answer your question about why I'm wanting to use the getcount method

I did not question the use of the getcount() function, I questioned why you were doing certain things in that getcount() function, for example printing, and iterating through the vector.

Quote

Why? I do not know.

Hopefully so that you realize that there is more than one way to skin a cat.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7107
  • View blog
  • Posts: 24,134
  • Joined: 05-May 12

Re: How do I use an iterator or for loop to display a vector as an int?

Posted 01 October 2019 - 03:25 PM

View PostAdriel16, on 01 October 2019 - 05:55 PM, said:

Why? I do not know.

As mentioned above, in C and C++, there's more than one way of doing things, and this is usually encouraged. This is different from Python, where there maybe multiple ways of doing something, but there is usually one correct "Pythonic" way of doing it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1