can't get program to work

I'm new to C++ and I am learning from a book. One exercise asks to read a sequence of values into a vector and print the sum of all values, the smallest, the largest, and the mean value. I have done one exercise very similar to this but without using for statements. The code below has for statements and I want to know why the program doesn't work. When I enter my values, nothing happens.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "../../std_lib_facilities.h"
int main() {
	vector<double>distances;
	double sum = 0;
	cout << "Enter the distance between neighboring cities along a route: ";
	for (double distance; cin >> distance;)
		distances.push_back(distance);
	double smallest = distances[0];
	double largest = distances[0];
	for (double x : distances)
		sum += x;
	for (double x: distances)
		if (x < smallest)
			smallest = x;
		else if (x > largest)
			largest = x;
	cout << "Total distance: " << sum << '\n';
	cout << "Smallest distance: " << smallest << '\n';
	cout << "Greatest distance: " << largest << '\n';
	cout << "Mean distance: " << sum / distances.size() << '\n';
}
Last edited on
I don't use vectors a lot, but looks like your for loops don't have anything telling them when to stop.

Also I recommend using the tags around your code, make it a lot easier to read.
Last edited on
Did you press Ctrl+D (Ctrl+Z on Windows) after you entered all values?
I don't use vectors a lot, but looks like your for loops don't have anything telling them when to stop.

Correct me if I'm wrong but shouldn't the loop stop when there are no more characters to cin?

Did you press Ctrl+D (Ctrl+Z on Windows) after you entered all values?

Ctrl+Z worked but what exactly did that do? and why wouldn't it work before?
I am new to c++ too but htis is what I changed to get the code to work. I changed the first for loop, I changed the int distance to in distanceholder to prevent confusion with the vector<int> distance, I added a few more cin/cout and added braces for readability, it works for me with gcc. here is the code. I have never seen a for loop built like your first one, however as mentioned I am also new to c++. I hope this helps.

edit.
I am assuming your include links to a header file containing vector, iostream and "using namespace std"

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
#include <iostream>
#include <vector>

using namespace std;

int main() 
{
	vector<double> distances;
	double sum = 0;
	double distanceholder;
	int cities; 
	int i;
	cout << "how many cities are along the route?" << endl;
	cin >> cities;
	
	for(i = 0; i < (cities - 1); i++)
	{
	  cout << "Enter the distance between neighboring cities along a route: ";
	  cin >> distanceholder;
	  distances.push_back(distanceholder);
	}
	
	
		
	double smallest = distances[0];
	double largest = distances[0];
	for (double x : distances)
	{
	  sum += x;
	}
	for (double x: distances)
	{
	  if (x < smallest)
	  {
	    smallest = x;
	  }
	  else if (x > largest)
	  {
	    largest = x;
	  }
	}
	  
	cout << "Total distance: " << sum << '\n';
	cout << "Smallest distance: " << smallest << '\n';
	cout << "Greatest distance: " << largest << '\n';
	cout << "Mean distance: " << sum / distances.size() << '\n';
}
Last edited on


Ctrl+Z worked but what exactly did that do? and why wouldn't it work before?


from what I see the reason you need to press ctrl+d is because the first for loop has no conditional in the second field telling it when to stop. ctrl+d send an EOF telling the for loop there is no more input thus ending the loop and continuing on with the code. Using /0 or EOF as a value does the same thing.

Hope this helps clarify things.
Last edited on
Using /0 or EOF as a value does the same thing.
No, it does not. It just sends 2 or 3 characters containing these symbols.

The loop stops though as '/' and 'E' are not valid digits and cannot be entered to double valiable. So stream enters "failed" state which breaks loop too.
My bad as mentioned in my previous post I am new to c++, I I read that if you cannot get a for-loop to exit that is stuck in an infinit loop enter '/0' (with single quotes) and it will exit because it signifies an EOF and it had worked for me in the past (though I usually use ctrl+d) . I was also under the impression that 'EOF' was an enum representing an '/0'. '/0' worked for me after I read it else where and they were not corrected on the comments to the article by anybody so I took it as truth. I'll find the article I read.

I see now that 'EOF' adds to the sum, I should have tested before posting it here.
I'm sure it would stop after the first loop, but not the second and third loops.

(don't quote me on that I absolutely despise for loops)
That's strange because the book I'm learning from used as an example for(double temp; cin>>temp;)
Now I'm guessing that format is invalid. The book also writes sort(temps);. When I tried calling the sort function that way, it wouldn't work until I wrote sort(temps.begin(), temps.end()); Was the book's example also invalid?
for(double temp; cin>>temp;) is valid and will do the same as:
1
2
3
4
5
double temp;
while (cin>>temp)
{

}
It reads until cin>>temp fails for some reason. It could be because you entered a non-numeric value or the EOF was reached.

sort(temps); could be valid if you have declared a function that takes the container (whatever type temps is), but there is no such function in the C++ standard library.
Last edited on
Thank you all so much!! I got my program to work :)
Last edited on
This is why I use while loops
This is why I use while loops

Huh? What reason are you citing as "this"?

Peter87 showed that
for(double temp; cin>>temp;)
and
1
2
3
4
5
double temp;
while (cin>>temp)
{

}

are equivalent. In either case an EOF (ctrl-d or ctrl-z) is needed to continue the loop. How is that a deciding factor as to whether to use for loops or not?

I happen to like for loops, but I see the advantage to favoring while loops at times for clarity or maintainability or some semantic reason. And in this case, I probably would have leaned toward a while loop myself. But this is not a reason to not use for loops.

For loops are extremely clear when the initial condition, final condition and iterator expression involve the same variable. For instance:
for (int i = 0; i < 10; ++i){...}
Other examples could be iterating through the members of a collector class or looping through the elements of an enum type.

Both for loops and while loops are valid. There are code situations that favor one or the other. There is no reason to despise either.
Last edited on
Topic archived. No new replies allowed.