Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help write the main() function for the following code in C++ This assignment's task is to read in card structs from standard input and

Please help write the main() function for the following code in C++

This assignment's task is to read in card structs from standard input and place such into a frequency histogram. Input is to occur until EOF (end-of-file) or an input error occurs. After reading in all input, for each entry in the frequency histogram's map, output the following (always on its own line):

  • occurs time(s)

where is the card and is the number of times it occurs in the histogram.

image text in transcribed

Code:

#include

#include

#include

#include

using namespace std;

//------

struct card

{

enum class suit { club, spade, diamond, heart };

enum { ace=1, jack=10, queen=11, king=12 };

using number = int;

number num_;

suit suit_;

};

bool operator ==(card const& a, card const& b)

{

return a.num_ == b.num_ && a.suit_ == b.suit_;

}

bool operator

{

return a.num_

}

istream& operator >>(istream& is, card& c)

{

// number followed by the suit (CSHD)...

is >> c.num_;

char ch;

if (is >> ch)

{

switch (ch)

{

case 'C': c.suit_ = card::suit::club; break;

case 'S': c.suit_ = card::suit::spade; break;

case 'H': c.suit_ = card::suit::heart; break;

case 'D': c.suit_ = card::suit::diamond; break;

default:

is.setstate(ios::failbit);

break;

}

}

else

is.setstate(ios::badbit);

return is;

}

ostream& operator

{

os

switch (c.suit_)

{

// card::suit allows us to access suit

// suit::club allows us to access club

// thus, card::suit::club lets us access club

case card::suit::club:

os

break;

case card::suit::spade:

os

break;

case card::suit::diamond:

os

break;

case card::suit::heart:

os

break;

}

return os;

}

//------

Sample Program Input The program input will be a series of zero or more cards. (Read the input until EOF or an error.) For example, sample input might be placed in a file called input.dat, e.g., 1 2. 3. li $ cat input.dat 10D3S4H9C10D8H2D12510D6H2D4H $ Sample Program Run The program output for the above input.dat file is: 1. 2. 3. 5. 6. $ g++-10.2.0 -Wall - Wextra -Werror a3.cxx $ ./a.out

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions