Question

After writing some pretty terse C++ using the sub script operator, I have a small error in the program - there's no output.

I input this (Linux)

54 73 89 43 38 90

in then hit Cntrl + D for EOF. The program outputs nothing and stops executing.

Source:

#include <iostream>
#include <string>
#include <vector>

using std::cin;
using std::vector;
using std::cout;
using std::endl;

int main() {
vector<unsigned> scores(11, 0); //11 buckets, all initially 0
unsigned grade;
while(cin >> grade) //read the grades
{
   if(grade <=100) //handles only valid inputs
  {
   ++scores[grade/10]; //increment counter for the current cluster
  }
 }
}

I haven't changed my settings in VIM so the coding style is slightly off. I cannot conceive what is wrong, the while loop is pretty standard. It reads into grades until it find the stream is not valid. I then check the input for being less than 100 (inclusive). The final piece of code (it's pretty terse) find the correct element in the vector to increment the counter.

I have a feeling it could perhaps be my input that's causing the program to not output.

EDIT 1: I added the output statement, I did this by using dereferencing a which is always a reference.

#include <iostream>
#include <string>
#include <vector>

using std::cin;
using std::vector;
using std::cout;
using std::endl;

int main() {
vector<unsigned> scores(11, 0); //11 buckets, all initially 0
unsigned grade;
while(cin >> grade) //read the grades
{
   if(grade <=100) //handles only valid inputs
  {
   ++scores[grade/10]; //increment counter for the current cluster
  }
 }
for(auto it = scores.begin(); it != scores.end(); ++it) {
cout << *it << endl;
 }
}
Was it helpful?

Solution

I have a feeling it could perhaps be my input that's causing the program to not output.

Not quite. It's the absence of output statements in your program that's causing it to not output.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top