The following code compiles well, but im having trouble with the code in red. wh
ID: 3635028 • Letter: T
Question
The following code compiles well, but im having trouble with the code in red. when you enter a value it just never stops asking and never went to the cout part.
#include<iostream>
#include<set>
#include<map>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<iomanip>
using namespace std;
template <class T>
class SimpleVector
{
private:
T *aptr;
int arraySize;
void memError();
void subError();
public:
// Default constructor
SimpleVector()
{ aptr = 0; arraySize = 0;}
// Constructor declaration
SimpleVector(int);
// Copy constructor declaration
SimpleVector(const SimpleVector &);
// Destructor declaration
~SimpleVector();
// Accessor to return the array size
int size() const
{ return arraySize; }
// Accessor to return a specific element
T getElementAt(int position);
// Overloaded [] operator declaration
T &operator[](const int &);
};
template <class T>
SimpleVector<T>::SimpleVector(int s)
{
arraySize = s;
try
{
aptr = new T [s];
}
catch (bad_alloc)
{
memError();
}
for (int count = 0; count < arraySize; count++)
*(aptr + count) = 0;
}
template <class T>
SimpleVector<T>::SimpleVector(const SimpleVector &obj)
{
arraySize = obj.arraySize;
aptr = new T [arraySize];
if (aptr == 0)
memError();
for(int count = 0; count < arraySize; count++)
*(aptr + count) = *(obj.aptr + count);
}
template <class T>
SimpleVector<T>::~SimpleVector()
{
if (arraySize > 0)
delete [] aptr;
}
template <class T>
void SimpleVector<T>::memError()
{
cout << "ERROR ";
exit(EXIT_FAILURE);
}
template <class T>
void SimpleVector<T>::subError()
{
cout << "ERROR ";
exit(EXIT_FAILURE);
}
template <class T>
T SimpleVector<T>::getElementAt(int sub)
{
if (sub < 0 || sub >= arraySize)
subError();
return aptr[sub];
}
template <class T>
T &SimpleVector<T>::operator[](const int &sub)
{
if (sub < 0 || sub >= arraySize)
subError();
return aptr[sub];
}
template <class T>
class SearchableVector : public SimpleVector<T>
{
public:
SearchableVector() : SimpleVector<T>()
{}
SearchableVector(int size) : SimpleVector<T>(size)
{ }
SearchableVector(const SearchableVector &);
void SortItems();
};
template <class T>
SearchableVector<T>::SearchableVector(const SearchableVector &obj) :
SimpleVector<T>(obj.size())
{
for(int count = 0; count < this->size(); count++)
this->operator[](count) = obj[count];
}
template <class T>
void SearchableVector<T>::SortItems()
{
for (int i = 0; i <= this->size(); i++)
{
for(int j=0;j<this->size()-1;j++)
{
if (getElementAt(j) > getElementAt(j+1))
{
T temp=this->operator [](j);
this->operator [](j)=this->operator [](j+1);
this->operator [](j+1)=temp;
}
}
}
}
//main function
int main()
{
//Variable declartion
const int SIZE = 10;
int count;
SearchableVector<double> dequeue(SIZE);
//Inputting elements to dequeue
cout<<"Enter Values: "<<endl;
for (count = 0; count < SIZE; count++)
{
cin>>dequeue[count];
}
cout << "The entered values are: ";
for (count = 0; count < SIZE; count++)
cout << dequeue[count] << " ";
cout << endl;
//Function calls to sort list of elemts in tables
dequeue.SortItems();
//Outputtind lists
cout << "After sorting: ";
for (count = 0; count < SIZE; count++)
cout << dequeue[count] << " ";
cout << endl;
//To pause system for a while
system("pause");
set<string> Set;
//Task 2 using set
//inserting party list
Set.insert("Jordan Dumlao");
Set.insert("Kayton Summers");
Set.insert("Helaman Tafua");
Set.insert("Sean Carlos");
Set.insert("Cohco Harbour");
Set.insert("Arlinda Yamaguchi");
Set.insert("Kayton Summers");
Set.insert("Ekolu Rawlins");
Set.insert("Cody Yamamoto");
Set.insert("Cohco Harbour");
Set.insert("Lashauna Wilson");
Set.insert("Kathy Todd");
Set.insert("Lashauna Wilson");
Set.insert("Gang Cheng");
Set.insert("Kyle Douglas");
Set.insert("Kevin Santana");
Set.insert("Ian Makida");
Set.insert("Theryn Groetken");
Set.insert("Kevin Santana");
Set.insert("Helaman Tafua");
Set.insert("Kathy Todd");
Set.insert("Kayton Summers");
cout << "Party List:" << endl;
// note const_iterator, not iterator. This iterator cannot be
// used to make changes to the set
set<string>::const_iterator setItr;
for(setItr = Set.begin(); setItr != Set.end(); setItr++)
{
cout << *setItr << endl;
}
cout << endl;
//To pause system for a while
system("pause");
//Task 3 using map
map<int, int> histogram;
int a=0;
while(a>=0)
{
cout << " enter value of a" ;
cin >>a;
histogram[a]++;
}
map<int,int>::iterator it;
for ( it=histogram.begin() ; it != histogram.end(); it++ )
{
cout << (*it).first << " occurs " << setw(3) << (*it).second << (((*it).second == 1) ? " time." : " times.") <<endl;
}
return 0;
}
Explanation / Answer
int a=0; while(a>=0) { cout >a; if(a>0){ histogram[a]++; }// make change like this } and enter '-1' to terminate cin; hope this will help you . write to me if need more help.Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.