0
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
    string title, nameX, nameY;
    const int maxData = 20;
    int data, maxValue;
    int count = 0;
    vector<int> v;
     maxValue = *(max_element(v.begin(), v.end()));
    for(int a = maxValue; a > 0; a++)
    {
        if (a < 10)
        {
            cout << a << "  |";
        }
        else
        {
            cout << a << " |";
        }
        for(int b = 0; b < v.size()-1; b++)
        {
            int temp = v[b];
            if (temp >= maxValue)
            {
                if (b < 10)
                {
                    cout << " * ";
                }
                else
                {
                    cout << "  * ";
                }
            }
            else
            {
                if (b < 10)
                {
                    cout << "   ";
                }
                else
                {
                    cout << "    ";
                }
            }
        }
        maxValue--;
        cout << endl;
    }
}
3
Contributors
4
Replies
20
Views
3 Years
Discussion Span
Last Post by Moschops
0

Line 15.

The vector v has no contents. It is of size zero. max_element returns the iterator that is v.end(). You then try to dereference this iterator, which is forbidden.

0

how can i correct it? still cannot run.

0

What moschops is saying is that you need to check for an empty vector or end iterator OR make sure that vector v is not empty.

std::vector<int>::iterator it = (max_element(v.begin(), v.end()));
if (it != v.end())
{
    maxValue = *it;
    //all other code (that for loop) goes here..
}
else
{
    std::cout<<"Vector v is empty!!\n";
}

Edited by triumphost

0

Given that you create the vector empty, and never put anything in it, what exactly is it for? Did you mean to put something in it?

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.