Question

In: Computer Science

(C++ program) ***User Interface Write a program that offers an easy way to add items, remove...

(C++ program)

***User Interface

Write a program that offers an easy way to add items, remove the last item, look at the last item put in the list. You will write all of the code for processing the stack - do not use any predefined objects in C++.  You decide how the interface will look. A menu driven program is always a good choice.

***Rules for program***

  • NEVER use break, exit, return, pass, continue or anything to leave a loop (or iteration), function, or other construct prematurely, unless it is part of the structure as in a case statement.
  • NEVER use global variables. However, you may use global constants if it is appropriate and they are used properly.
  • Should only have only one return statement in a function. (Exception – Multiple return statements may be necessary in a recursive function.)

create a class “Item” with the following private data attributes:

  • Item name (string)
  • Color (string)

For this lab have separate files in a project. Put your class definitions in header files and the implementations of the methods in a .cpp file.

You will have the following public methods:

  • Accessors and mutators for each attribute
  • Constructor that initializes the attributes to nulls (empty string)

Create another class to implement a stack of "Items" using an array-based implementation.

Your stack class should include the following operations:

  1. isEmpty – tell whether there are any entries in the stack
  2. push – add another item
  3. pop – remove the top item
  4. peek – look at the item on top
  5. display - displays the items in the stack indicating where the top is (this one just gives and easy way for me to see what is in the stack as I test your program)

Solutions

Expert Solution

// stack.h

#ifndef STACK_H
#define STACK_H

#include <string>
using namespace std;

class Item
{
private:
string name;
string color;
public:
Item();
void setName(const string& name);
void setColor(const string& color);
string getName() const;
string getColor() const;
};

class Stack
{
private:
static const int MAX = 100; // large so that stack is never full
Item items[MAX];
int top;
public:
Stack();
bool isEmpty() const;
void push(const Item& item);
void pop();
Item peek() const;
void display() const;
};

#endif // STACK_H

//end of stack.h

// stack.cpp

#include "stack.h"
#include <iostream>

// default constructor
Item::Item() : name(""), color("")
{}

// set the name of the item
void Item:: setName(const string& name)
{
this->name = name;
}

// set the color of the item
void Item:: setColor(const string& color)
{
this->color = color;
}

// return the name of the item
string Item:: getName() const
{
return name;
}

// return the color of the item
string Item:: getColor() const
{
return color;
}

// default constructor to create an empty stack of maximum size 100
Stack::Stack(): top(-1)
{}

// returns true if stack is empty else false
bool Stack:: isEmpty() const
{
return top == -1;
}

// insert item at top of stack
void Stack:: push(const Item& item)
{
top++;
items[top] = item;
}

// remove the top element of stack
void Stack:: pop()
{
if(!isEmpty()) // stack is not empty
top--;
}

// return the top element of stack
Item Stack:: peek() const
{
Item item;
// non-empty stack
if(!isEmpty())
item = items[top]; // get the top item
return item;
}

// display all the items of the stack
void Stack:: display() const
{
cout<<"Top: "<<top<<endl; // display top index
// display the items from top to bottom
cout<<"Stack Items(top to bottom): "<<endl;
for(int i=top;i>=0;i--)
cout<<"Name: "<<items[i].getName()<<" Color: "<<items[i].getColor()<<endl;
}

//end of stack.cpp

// driver.cpp : C++ program to test the stack class
#include <iostream>
#include "stack.h"

using namespace std;

int main()
{
Stack stack;
int choice;
string name, color;
Item item;
// loop that continues until the user exits
do
{
// display menu
cout<<"1) Add an item"<<endl;
cout<<"2) Remove the top item"<<endl;
cout<<"3) Is the stack empty"<<endl;
cout<<"4) Get top item"<<endl;
cout<<"5) Display the stack"<<endl;
cout<<"6) Exit"<<endl;
cout<<"Enter your choice: ";
cin>>choice;

// based on menu choice perform the operation
if(choice == 1)
{
cout<<"Name: ";
cin.ignore();
getline(cin, name);
cout<<"Color: ";
getline(cin, color);
item.setName(name);
item.setColor(color);
stack.push(item);
}
else if(choice == 2)
{
stack.pop();
}
else if(choice == 3)
{
if(stack.isEmpty())
cout<<"Stack is empty"<<endl;
else
cout<<"Stack is not empty"<<endl;
}
else if(choice == 4)
{
if(!stack.isEmpty()){
item = stack.peek();
cout<<"Name: "<<item.getName()<<" Color: "<<item.getColor()<<endl;
}else
cout<<"Stack is empty"<<endl;
}
else if(choice == 5)
stack.display();
else if(choice != 6)
cout<<"Invalid choice"<<endl;
cout<<endl;

}while(choice != 6);


return 0;
}

//end of driver.cpp

Output:


Related Solutions

write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for a file name. If the file is not found, an error message is output, and the program terminates. Otherwise, the program prints each token in the file, and the number of times it appeared, in a well formatted manner. To accomplish all this, do the following: - Open the file - the user must be prompted and a file name input. DO NOT hardcode...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
Write a Java Program that can:​ Remove a particular element from an array.​ Add a new...
Write a Java Program that can:​ Remove a particular element from an array.​ Add a new element to an array.​ Change an element with the new one.​ Search for a particular element in the array.​ ​The code must have four separate methods for each task stated above.​ Do not use any pre-defined Java functions.​ You are free to use int or String data-type for the array.​
Write a C++ program that finds the minimum number entered by the user .The user is...
Write a C++ program that finds the minimum number entered by the user .The user is allowed to enter negative numbers 0, or positive numbers. The program should be controlled with a loop statement. Break statements is not allowed to break from loop. After each number inputted by the user, The program will prompt user if they need to enter another number. User should enter either Y for yes or N for no. Make Sure the user enters either Y...
Write a C++ program that prompts the user for the radius of a circle and then...
Write a C++ program that prompts the user for the radius of a circle and then calls inline function circleArea to calculate the area of that circle. It should do it repeatedly until the user enters -1. Use the constant value 3.14159 for π Sample: Enter the radius of your circle (-1 to end): 1 Area of circle with radius 1 is 3.14159 Enter the radius of your circle (-1 to end): 2 Area of circle with radius 2 is...
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
Write a program in C that prompts the user for a number of seconds and then...
Write a program in C that prompts the user for a number of seconds and then converts it to h:m:s format. Example: 5000 seconds should display as 1:23:20 (1 hour, 23 minutes, 20 seconds.) Test with several values between about 100 seconds and 10,000 seconds. use unint and remainders for this and keep it as simple as possible.
Tail of a File, C++ Program. write a program that asks the user for the name...
Tail of a File, C++ Program. write a program that asks the user for the name of a text file. The program should display the last 10 lines, or all lines if less than 10. The program should do this using seekg Here is what I have so far. #include<iostream> #include<fstream> #include<string> using namespace std; class File { private:    fstream file;    string name; public:    int countlines();    void printlines(); }; int File::countlines() {    int total =...
Program specifics: Write a C++ program that does the following: a. Asks the user for the...
Program specifics: Write a C++ program that does the following: a. Asks the user for the distance to the pin and the depth of the green (both in yards). (Note: The pin is the hole in the green and the depth is the diameter of the green, assuming it is circular.) b. Asks the user for an integer club number from 2 to 10, where 10 is the pitching wedge (this club lifts the ball out of rough, sand, etc)....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT