Starting from:

$25

CSCI1310- Assignment 3

It’s like Craigslist, only different
 

Objectives:

 

1.     Applying the concept of program decomposition.

2.     Implement fundamental programming constructs, such as variables, expressions, conditionals, and iterative control structures.

3.     Become acquainted with  simple file I/O commands.

4.     Design functions in program construction, and understand parameter passing and return values.

 

 

Read the entire assignment carefully before beginning. In this assignment, you’re going to develop a simulated community message board where users can post items they have for sale or search for items they want to buy. Your program will search for matches, e.g. there is a bike for sale for $50 and a bike wanted, where the buyer will pay up to $60, and remove items from the message board as they are sold. Users have the option of posting and searching for as many items as they choose before they quit the program.

 

Getting started
Your program should display a menu with the following selections:

1.     Insert an item.

2.     Search for an item.

3.     Print the message board.

4.     Quit.

 

Use the following C++ print and input statements for your menu:

 

cout << "1. Insert an item." << endl; cout << "2. Search for an item." << endl; cout << "3. Print the message board." << endl;

cout << "4. Quit."<< endl; getline(myFile, choice);

 

​                     Note: the getline​   () command assumes you have a variable called “choice” defined and that it’s a string. This menu should be displayed after every transaction is complete unless the user selects Quit. The user can select the option they want using the number for the option. For example, to add an item, they type 1.

 

Item types
There are only five types of items: bike, microwave, dresser, truck and chicken. When you prompt the user, your prompt needs to include the exact text shown here:

 

"Enter the item type-b,m,d,t,c:"

"Enter the item cost:"

 

The user types the first letter of the item type to indicate the type they want to add.

 

Data in the message board is stored as a 2D array. One dimension of the array is the number of items in the message board and the other dimension is the parameters for each item. Each item on the message board contains the type and the cost.

 

​             Assume you have an array called msgBoard​     and you want to store the following three items. 

 

bike, $50 truck, $1000

microwave, $10

 

Your array would look like

 

msgBoard[0][0] = ‘b’; msgBoard[0][1] = ‘50’; msgBoard[1][0] = ‘t’; msgBoard[1][1] = ‘1000’; msgBoard[2][0] = ‘m’;

msgBoard[2][1] = ‘10’;

 

All values are stored as strings in this example. 

 

Insert an item
If the user selects 1 to add an item, they should be prompted for the type of item to add and the cost of the item. When an element is added to the message board array, it should be added at the first available position. Don’t sort the data, we will be running test cases with expected output for each input. Duplicates are also okay.

 

Search for an item
When the user selects 2 to find an item, they should be prompted with the item type, and the maximum price they are willing to pay for the item. Your code should then search the array and return the first item with the correct type and a cost that is less than or equal to the price that the user will pay.

 

Use the following text in your prompts:

 

"Enter the item type-b,m,d,t,c:"

"Enter the maximum item cost:"

 

For example, if the user types b and 50, they want a bicycle and are willing to pay up to $50 for it. Your program should find the first bicycle in the list that sells for $50 or less. 

 

If a match is found, print “Sold <typefor<cost” 

 

                            The type​ is one of the following: bike, microwave, dresser, truck, ​ or​ chicken​ .​
 

                            The cost​ is the actual item cost, not what the user is willing to pay.

 

The item should then be deleted from the array. 

 

If the item is not found, print “Item not found”.

 

Note: if your values are stored as strings, they will need to be cast as integers to compare price values. You will also need to convert the type values stored in the array to nice, understandable values. For example, ‘b’ needs to be displayed as ‘bike’.

 

Print the message board
If the user selects Print the message board, your program should display the items in the message board array in the following format:

 

<type: <cost

 

Quit
If the user selects 4 for Quit, the program should exit the while loop that controls the program, which will cause the program to exit.

 

Implementation details
There are a few requirements for your program that you need to meet to receive full credit.

 

1.     Your array needs to be big enough to store 20 items.

2.     You need to have functions for inserting to the message board, searching the message board, and printing the message board.

3.     Your text you display in your menu and other outputs need to match the text given in this document.

 

Breaking down the problem
This assignment is our first assignment this semester that is one large problem instead of two or three small problems. If you’ve read the assignment write-up and you’re not sure where to start, it can be helpful to think of the problem in stages and complete each stage before moving on to the next stage. Here is a suggested approach for breaking down this problem into smaller problems.

 

1.     Build the main menu. Write the loop that asks for user input and test that your menu recognizes the four user options to Insert, Search, Print, and Quit. In your code, you can print which menu item is selected to test that you menu code is working. Be sure to remove these print statements later once you’ve developed the real code for the menu item.

2.     Build the sub-menus. The Insert and Search menu items have sub-menus that are displayed when the user selects that menu item. Build and test your sub-menus using print statements to verify which sub-menu was selected. Again, make sure you remove the print statements once you develop the real code for the sub-menu item.

3.     Initialize the array to hold the message board data. The list should be initialized outside of your menu loop.

4.     Build the code to add items to the array. This code needs to use the user inputs for the type and cost of the item, and add them to the array in the appropriate index.

5.     Build the code to find items in the array and delete them from the array if the item is found. This code should be located under the find menu item and use the inputs from the user. 

6.     Build the code to print the array that runs when the print menu option is selected.

7.     If your main menu loop doesn’t already handle the Quit functionality, modify the menu loop to quit the program when the user selects Quit.

8.     Do a happy dance.

 

Submitting Your Code in Code Runner
Please also include comments in your code to describe what your code is doing. 

 

For your convenience we have given the main function and the prototype of the remaining functions. Code runner expects the entire program including the main function and headers. So please fill the necessary code in the Function definitions and copy the entire program with the main function and headers into the code runner.  Your TA will be giving instructions in the next recitation.

 

 

#include <iostream

#include <stdlib.h #include <fstream

using namespace std;

 

//Function Prototypes

string convertItemCString(string itemType); void printBoard(string carray[][2], int itemCount); void printMenu(); int insertItem(string cArray[][2], string itemType, string cost, int itemCount); int searchArray(string cArray[][2], string itemType, string cost, int itemCount); bool isValid(string itemType);

 

int main(){

 

 

    string cArray[20][2];     string choice = "1";     int itemCount = 0;     char filename[10];     cinfilename;     ifstream myFile;     myFile.open(filename);     if (!myFile.is_open()){         cout<<"File failed to open"<<endl;

    }else{

 

        while(choice != "4"){             //display menu             printMenu();

            getline(myFile,choice);

 

            string itemType;

            string cost;

 

            if(choice == "1"){                 cout<<"Enter the item type-b,m,d,t,c:"<<endl;                 getline(myFile,itemType);                 while(!isValid(itemType)){                     cout<<"Not a valid item type."<<endl;                     cout<<"Enter the item type-b,m,d,t,c:"<<endl;                     getline(myFile,itemType);

                }

                cout<<"Enter the item cost:"<<endl;

                getline(myFile,cost);                 itemCount = insertItem(cArray, itemType, cost, itemCount);

            }else if(choice == "2"){

 

                cout<<"Enter the item type-b,m,d,t,c:"<<endl;                 getline(myFile,itemType);                 while(!isValid(itemType)){                     cout<<"Not a valid item type."<<endl;                     cout<<"Enter the item type-b,m,d,t,c:"<<endl;                     getline(myFile,itemType);

                }

                cout<<"Enter the maximum item cost:"<<endl;

                getline(myFile,cost);                 itemCount = searchArray(cArray, itemType, cost, itemCount);

            }else if(choice == "3"){                 printBoard(cArray, itemCount);             }else if(choice == "4"){

                cout<<"Good bye"<<endl;

            }else{

                cout<<"Not a valid choice."<<endl;

            }

        }

    }

    return 0;

}

 

//Function Definitions

string convertItemCString(string itemType){

//Your code goes here

//the item type is entered as a string of a single character. This function changes that to a complete name of item

    }

 

void printBoard(string carray[][2], int itemCount){

//Your code goes here

//displays array contents

}

 

void printMenu(){

//Your code goes here

//prints menu

}

 

int insertItem(string cArray[][2], string itemType, string cost, int itemCount){

//Your code goes here

//populates the array and returns the sentinel (item count)

 

   }

 

int searchArray(string cArray[][2], string itemType, string cost, int itemCount){

//Your code goes here

// returns item count, it searches for a match and if found, prints and deletes item from array

   }

 

bool isValid(string itemType){

//Your code goes here

//it verifies if the itemType is valid, returns true if it is.

  }

 

What to do if you have questions
There are several ways to get help on assignments in 1310, and depending on your question, some sources are better than others. Piazza is a good place to post technical questions, such as how to get user input, or treat that input as an integer. When you answer other students’ questions on the forum, please do not post entire assignment solution.. The TA and CA are also a good source of technical information. If, after reading the assignment write-up, you need clarification on what you’re being asked to do in the assignment, the course instructor is a good source of information. 

 

 

 

More products