Test 3 CSC201

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 5

Define a struct called PayInfo that contains a float field representing the

hourly payrate and a float representing the hours worked in a week.


Assuming this struct has been defined, define a global variable called
pinfo that is of this data type.
struct PayInfo {
float payrate;
float hours;}
PayInfo pinfo;
_______________________
using a struct in main

Consider the following partial code:


struct Address {        string street;        string city;        string state; 
string zip;};int main(){
 Address addr;
        cout << "Enter street -> "; getline(cin,addr.street);
        cout << "Enter city -> "; getline(cin,addr.city);
        cout << "Enter state -> "; getline(cin,addr.state);
        cout << "Enter zip code -> "; getline(cin,addr.zip);   
        return 0;
}

Consider the following partial code:


struct Name {        string first;        string middle;        string last;};
struct Address {        string street;        string city;        string state; 
string zip;};
struct Person {        Name name;        Address addr;};int main(){   
Person jo;        cout << "Enter first name -> "; getline(cin,jo.name.first);        
cout << "Enter middle name -> "; getline(cin,jo.name.middle);         cout <<
"Enter last name -> "; getline(cin,jo.name.last);         cout << "Enter street ->
"; getline(cin,jo.addr.street);         cout << "Enter city -> "; getline(cin,jo.addr.city);        
cout << "Enter state -> "; getline(cin,jo.addr.state);         cout << "Enter zip
code -> "; getline(cin,jo.addr.zip);           return 0;}
___________________________________________________
Given the following struct definition, define a function called
display, that will accept this struct as a parameter and display the
contents of this struct using the indicated specifications. Assume
the output will go to a file that is indicated by a reference
parameter to an ofstream. We want to optimize the performance
of this function by passing the struct as a constant reference
parameter.output specs:1 linestarting with 10 spacesfollowed by
the code, left-justified in 15 spacesfollowed by a spacefollowed by
the quantity, right-justified in 10 spacesfollowed by a space
followed by the price, right-justified in 15 spaces including two
digits to the right of the decimalfollowed by a carriage return.
 
struct Item {        string code;        int qty;        float price;};
// define your function here
void display(ofstream& fout, const Item& item)
{
        fout << setw(10) << ""
                  << setw(15) << left << item.code
                 << " "
                 << setw(10) << right << item.qty
                 << " "
                 << setw(15) << right << fixed << setprecision(2) <<
item.price
                 << endl;
}
________________________
Given the following struct definition, define a function called input, that
will accept this struct as a reference parameter, along with a reference to
an ifstream, and accept a value for each field in the struct. The function
should return true if the input was successful and false otherwise.
 
struct Item {        string code;        int qty;        float price;};
// define your function here
 
Your Answer:
bool input(ifstream& fin, Item& item){        fin >> item.code >>
item.qty >> item.price;        return fin;}
_______________

We want to represent a Price of items as a float. Create a user-


defined type called PriceType that is implemented as a float using
the typedef command.

typedef float PriceType; 

Use enum to create a list of constants containing the following names: bad, good, better, best. we
want basd to have a value of zero and best to have a value of 3. We also want to two additional
words in the list: firstr, and last. fist should have the same value as bad and last should have the
same value as best.

Your Answer:
enum QualityType { first, bad=0, good, better, best, last=3 };

___________________________________

Assume the following are available.


enum to string
enum MenuOption {
 UNKNOWN=-1, FIRST, OPEN=FIRST, SEARCH, EDIT, ADD,
DELETE, CLOSE, LAST=CLOSE
};
 
string options[] = { "open", "search", "edit", "add", "delete", "close"};
 
string menu2String(MenuOption mo); // returns the string equivalent of
the supplied value. The first character in the retyrbed string is upper case
and all others are lower case.
MenuOption string2Menu(string option);  // returns the MenuOption
equivalent of the supplied string. The string is treated as case insensitive.
string toLower(string str); // returns a string that is equivalent to the
supplied string, except that all letters in the returned string are lower
case.
 
Write the definition for the menu2String function. If there is no match
for the supplied value, return the string "Unknown". Do not return a
string equivalent for FIRST and LAST.

===

string menu2String(MenuOption mo)


{
 switch(mo) {
  case OPEN:   return "Open";
  case SEARCH: return "Search";
  case EDIT:   return "Edit";
  case ADD:    return "Add";
  case DELETE: return "Delete";
  case CLOSE:  return "Close";
  default:     return "Unknown";
 } 
}

You might also like