Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I keep getting the following error and can\'t find what is wrong Here is the cod

ID: 3713899 • Letter: I

Question

I keep getting the following error and can't find what is wrong

Here is the code...

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

//head_to_val() method assigns an integer value to cards with different heads
int head_to_val(char c)
{
if(c=='C') return 1;
else if (c=='D') return 2;
else if (c=='H') return 3;
else if (c=='S') return 4;
}

//tag_to_val() method assigns an integer value to card of every denomination with a head set
int tag_to_val(char c)
{
if(c=='2') return 1;
else if (c=='3') return 2;
else if (c=='4') return 3;
else if (c=='5') return 4;
else if (c=='6') return 5;
else if (c=='7') return 6;
else if (c=='8') return 7;
else if (c=='9') return 8;
else if (c=='M') return 9; //I have used M for 10
else if (c=='J') return 10;
else if (c=='Q') return 11;
else if (c=='K') return 12;
else if (c=='A') return 13;
}

//cmp method compares between two cards
bool cmp(string s1,string s2)
{
if(head_to_val(s1[0])==head_to_val(s2[0]))
{
return tag_to_val(s1[1])<tag_to_val(s2[1]);
}
else
{
return head_to_val(s1[0])<head_to_val(s2[0]);
}
}

class list
{
//listarr stores strings present in the list
//dynamic array of size 20
string* listarr;

// n keeps count of total no. of elements in te listarr
int n;

public:
list()
{
n=0;
// Part1 : To Create a list by dynamic allocated array and set the size to 20
listarr= new string[20];

// Initialize every string in list by a space" "
for(int i=0;i<20;i++)
{
listarr[i] = " ";
}
}

void PutItem(string s);
void GetItem(string s);
void DeleteItem(string s);
void Display()
{
for(int i=0;i<n-1;i++)
{
if(listarr[i][1]=='M')
{
cout<<listarr[i][0]<<"10,";

continue;

}

cout<<listarr[i]<<",";
}
cout<<listarr[n-1]<<endl;
}
};

//PutItem() method insert string(card) s into sorted Poker cards list.
void list::PutItem(string s)
{
if(listarr[0]==" ")
{
listarr[0]=s;
}
else
{
int i=0;

while(cmp(listarr[i],s) && listarr[i]!=" ")
{
i++;
}
if(listarr[i]==" ")
{
listarr[i]=s;
}
else
{
for(int j=18;j>=i;j--)
listarr[j+1] = listarr[j];

listarr[i] = s;
}
}

n += 1;
}

//DeleteItem() method deletes string(card) s into sorted Poker cards list.
void list::DeleteItem(string s)
{
if(listarr[0]==" ")
{
return;
}
else
{
int i=0;

while(listarr[i]!=s && i<20 && listarr[i]!=" ")
{
i++;
}

if(listarr[i] == s)
{
for(int j=i;j<19;j++)
{
listarr[j] = listarr[j+1];
}

n-=1;
}
else
{
cout<<"Item "<<s<<" not present!"<<endl;
}
}
}

//GetItem() method finds string(card) s in the sorted Poker cards list.

void list::GetItem(string s)
{
if(listarr[0]==" ")
{
cout<<s<<" NO"<<endl;
}
else
{
int i=0;

while(listarr[i]!=s && i<n && listarr[i]!=" ")
{
i++;
}

if(listarr[i] == s)
{
cout<<s<<" YES ";
}
else
{
cout<<s<<" NO ";
}
}
}

int main()
{
ifstream fobj;
fobj.open("DataFile.txt");
int cnt = 0;

if(!fobj.good())
{
cout<<"Problem in opening file!!"<<endl;
}
else
{
list l;
string str;
getline(fobj,str);
stringstream ss(str);

//Part2 : Read the first 20 cards in the first line of the file, the put them one by one into the list by using PutItem()
int i = 0;

while( ss.good() && i<20)
{
string substr;
getline( ss, substr, ',' );

if(substr[1]=='1' && substr[2]=='0')
{
substr[1] = 'M';
substr[2] = '';
}

l.PutItem(substr);
i++;
}

l.Display();

//Part3 : delete the cards indicated in the second line of the file by using DeleteItem
getline(fobj,str);

stringstream ss1(str);

i = 0;

while( ss1.good() && i<20)
{
string substr;
getline( ss1, substr, ',' );

if(substr[1]=='1' && substr[2]=='0')
{
substr[1] = 'M';

substr[2] = '';
}

l.DeleteItem(substr);
i++;
}

l.Display();

//Part4 : put the items in the third line in to the list. Must use PutItem()
getline(fobj,str);
stringstream ss2(str);

i = 0;

while( ss2.good() && i<20)
{
string substr;

getline( ss2, substr, ',' );

if(substr[1]=='1' && substr[2]=='0')
{
substr[1] = 'M';
substr[2] = '';
}

l.PutItem(substr);
i++;

}

l.Display();
//Part5 : Search the current list for the elements in the list
getline(fobj,str);

stringstream ss3(str);

i = 0;

while( ss3.good() && i<20)
{
string substr;
getline( ss3, substr, ',' );

l.GetItem(substr);
i++;
}

cout<<endl;
}

return 0;
}

Item D5 not present! Item HK not present! Item D2 not present! C2,C4,C8,?9 , CJ , CQ, D2,05,08,D10,DJ,00,DK , H3 , Hjhj , HK,57,SM terminate called after throwing an instance of std::logic_error what): basic_string::_M_construct null not valid This application has requested the Runtime to terminate it in an unusual way, Please contact the application's support team for more information

Explanation / Answer

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

//head_to_val() method assigns an integer value to cards with different heads
int head_to_val(char c)
{
   if(c=='C') return 1;
   else if (c=='D') return 2;
   else if (c=='H') return 3;
   else if (c=='S') return 4;
}

//tag_to_val() method assigns an integer value to card of every denomination with a head set
int tag_to_val(char c)
{
   if(c=='2') return 1;
   else if (c=='3') return 2;
   else if (c=='4') return 3;
   else if (c=='5') return 4;
   else if (c=='6') return 5;
   else if (c=='7') return 6;
   else if (c=='8') return 7;
   else if (c=='9') return 8;
   else if (c=='M') return 9; //I have used M for 10
   else if (c=='J') return 10;
   else if (c=='Q') return 11;
   else if (c=='K') return 12;
   else if (c=='A') return 13;
}

//cmp method compares between two cards
bool cmp(string s1,string s2)
{
   if(head_to_val(s1[0])==head_to_val(s2[0]))
   {
   return tag_to_val(s1[1])<tag_to_val(s2[1]);
   }
   else
   {
   return head_to_val(s1[0])<head_to_val(s2[0]);
   }
}

class list
{
   //listarr stores strings present in the list
   //dynamic array of size 20
   string* listarr;

   // n keeps count of total no. of elements in te listarr
   int n;

public:
   list()
   {
   n=0;
   // Part1 : To Create a list by dynamic allocated array and set the size to 20
   listarr= new string[20];

   // Initialize every string in list by a space" "
   for(int i=0;i<20;i++)
   {
   listarr[i] = " ";
   }
   }

   void PutItem(string s);
   void GetItem(string s);
   void DeleteItem(string s);
   void Display()
   {
   for(int i=0;i<n-1;i++)
   {
   if(listarr[i][1]=='M')
   {
   cout<<listarr[i][0]<<"10,";

   continue;

   }

   cout<<listarr[i]<<",";
   }
   cout<<listarr[n-1]<<endl;
   }
};

//PutItem() method insert string(card) s into sorted Poker cards list.
void list::PutItem(string s)
{
   if(listarr[0]==" ")
   {
   listarr[0]=s;
   }
   else
   {
   int i=0;

   while(cmp(listarr[i],s) && listarr[i]!=" ")
   {
   i++;
   }
   if(listarr[i]==" ")
   {
   listarr[i]=s;
   }
   else
   {
   for(int j=18;j>=i;j--)
   listarr[j+1] = listarr[j];

   listarr[i] = s;
   }
   }

   n += 1;
}

//DeleteItem() method deletes string(card) s into sorted Poker cards list.
void list::DeleteItem(string s)
{
   if(listarr[0]==" ")
   {
   return;
   }
   else
   {
   int i=0;

   while(listarr[i]!=s && i<20 && listarr[i]!=" ")
   {
   i++;
   }

   if(listarr[i] == s)
   {
   for(int j=i;j<19;j++)
   {
   listarr[j] = listarr[j+1];
   }

   // n-=1;
   }
   else
   {
   cout<<"Item "<<s<<" not present!"<<endl;
   }
   }
}

//GetItem() method finds string(card) s in the sorted Poker cards list.

void list::GetItem(string s)
{
   if(listarr[0]==" ")
   {
   cout<<s<<" NO"<<endl;
   }
   else
   {
   int i=0;

   while(listarr[i]!=s && i<n && listarr[i]!=" ")
   {
   i++;
   }

   if(listarr[i] == s)
   {
   cout<<s<<" YES ";
   }
   else
   {
   cout<<s<<" NO ";
   }
   }
}

int main()
{
   ifstream fobj;
   fobj.open("DataFile.txt");
   int cnt = 0;
   cout<<"Enterred ito maibn"<<endl;
   if(!fobj.good())
   {
   cout<<"Problem in opening file!!"<<endl;
   }
   else
   {
   list l;
   string str;
   getline(fobj,str);
   stringstream ss(str);

   //Part2 : Read the first 20 cards in the first line of the file, the put them one by one into the list by using PutItem()
   int i = 0;

   while( ss.good() && i<20)
   {
   string substr;
   getline( ss, substr, ',' );

   if(substr[1]=='1' && substr[2]=='0')
   {
   substr[1] = 'M';
   substr[2] = '';
   }

   l.PutItem(substr);
   i++;
   }

   l.Display();

   //Part3 : delete the cards indicated in the second line of the file by using DeleteItem
   getline(fobj,str);

   stringstream ss1(str);

   i = 0;

   while( ss1.good() && i<20)
   {
   string substr;
   getline( ss1, substr, ',' );

   if(substr[1]=='1' && substr[2]=='0')
   {
   substr[1] = 'M';

   substr[2] = '';
   }

   l.DeleteItem(substr);
   i++;
   }

   l.Display();

   //Part4 : put the items in the third line in to the list. Must use PutItem()
   getline(fobj,str);
   stringstream ss2(str);

   i = 0;

   while( ss2.good() && i<20)
   {
   string substr;

   getline( ss2, substr, ',' );

   if(substr[1]=='1' && substr[2]=='0')
   {
   substr[1] = 'M';
   substr[2] = '';
   }

   l.PutItem(substr);
   i++;

   }

   l.Display();
   //Part5 : Search the current list for the elements in the list
   getline(fobj,str);

   stringstream ss3(str);

   i = 0;

   while( ss3.good() && i<20)
   {
   string substr;
   getline( ss3, substr, ',' );

   l.GetItem(substr);
   i++;
   }

   cout<<endl;
   }

   return 0;
}

/************* DataFile.txt **************

C1 S2 D3 H4 C5 H6 D7 H8 H9 SA SJ SK SQ H1 C2 C3 C4 D5 D6 D7
C5 H6 D7 H8 H9
H2 HK CK DK
*/

/**************** OUTPUT OF PROGRAM ***************

Enterred into main program function
C1 S2 D3 H4 C5 H6 D7 H8 H9 SA SJ SK SQ H1 C2 C3 C4 D5 D6 D7
Item C5 H6 D7 H8 H9 not present!
C1 S2 D3 H4 C5 H6 D7 H8 H9 SA SJ SK SQ H1 C2 C3 C4 D5 D6 D7
C1 S2 D3 H4 C5 H6 D7 H8 H9 SA SJ SK SQ H1 C2 C3 C4 D5 D6 D7,H2 HK CK DK
NO
***************************************************/