I'm curious why if i want to do this with vector(which holds strings)
if(vec[i]=="0") ... Error prompt appers.
Program: *** line;
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<string>vec;
string min="", max="", mode="",temp_mode="", x;
int count=0, temp_count=0;
cout<<"Enter strings"<<endl;
while(cin>>x){
vec.push_back(x);
}
sort(vec.begin(), vec.end());
cout<<"Sorted strings:"<<endl;
for(int y=0; y!=vec.size(); ++y){
cout<<vec[y]<<endl;
}
min=vec[0];
max=vec[vec.size()-1];
for(int i=0; i!=vec.size(); ++i){
*** if(vec[i]=="0"){ // i've changet to if(i==0);
++temp_count;
temp_mode=vec[i];
}
else {
if(vec[i]==vec[i-1]){
++temp_count;
}
else {
if(temp_count>count){
count=temp_count;
mode=temp_mode;
temp_count=1;
temp_mode=vec[i];
}
else{
temp_count=1;
temp_mode=vec[i];
}
}
}
}
cout<<"Maximum value: "<<max<<endl;
cout<<"Minimum value: "<<min<<endl;
cout<<"Mode is "<<mode<<" with "<<count<<" repeated times"<<endl;
getchar();
getchar();
return 0;
}
Last edited on
It says : "Debug assertion failed, Expression: vector subscript out of range"
It should be if (i==0) { ... The semicolon at the end is wrong.
Also, please use code tags in future. The code is almost unreadable without them.