I am a beginner taking a programming class and I am having trouble with my assignment. I am to make a program that can store and sort data(I chose games), and everything seems to be going alright. EXCEPT for when I choose to input a game, and later display the games I've entered, there will be nothing in the list. Is there something I'm missing?
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
struct VidyaGames {
string Title;
string Date;
string Developer;
};
void getGames(VidyaGames array[], int &k);
void displayGames(VidyaGames array[], int &k);
void deleteGames(VidyaGames array[], int &k);
void sortGames(VidyaGames array[], int &k);
const int MAX = 150;
string Title;
string Date;
string Developer;
int main()
{
char choice;
VidyaGames array[MAX];
bool kek = true;
int k = 0;
do
{
cout << "Welcome to the 'Super Incredible Amazing Game Storage-o-Tron 5000(and one)." << endl;
cout << " " << endl;
cout << "Please select which task you would like to perform by typing in the " << endl;
cout << "corresponding letter in the bracket: " << endl;
cout << " " << endl;
cout << "[I]nput a game into the list." << endl;
cout << "[D]isplay the games you have stored." << endl;
cout << "[S]ort the games you have stored." << endl;
cout << "[R]emove a game from the list." << endl;
cout << "[Q]uit the program." << endl;
cin >> choice;
switch (choice)
{
case 'I': getGames(array, k); break;
case 'D': displayGames(array, k); break;
case 'S': deleteGames(array, k); break;
case 'R': deleteGames(array, k); break;
case 'Q': kek = false; break;
default : cout << "Hey. Remember when I gave you the specific options you were allowed to choose?" << endl;
cout << "Maybe enter one of those?" << endl;
cout << " " << endl;
}
}
while (kek);
cout << "You have killed me." << endl;
}
void getGames(VidyaGames array[], int &k)
{
system("cls");
VidyaGames tmp;
char lel[100];
cout << "Enter the title of your game: " << endl;
getline (cin, Title);
cout << "Enter the date your game was published: (Example: March 15th, 2014)" << endl;
getline (cin, Date);
cout << "Enter the developer of your game: " << endl;
getline (cin, Developer);
}
void displayGames(VidyaGames array[], int &k)
{
system ("cls");
if (k==0)
cout << "There is literally nothing in this list, as you have made the mental choice to not put anything in it yet." << endl;
else if (k > 0) {
for (int i=0; i < 0; i++)
{
cout << "Title: " << array[i].Title << endl;
cout << "Release Date: " << array[i].Date << endl;
cout << "Developer: " << array[i].Developer << endl;
}
}
}
void deleteGames(VidyaGames array[], int &k) {
system("cls");
char deleteChoice;
if (k==0)
cout << "There is literally nothing in this list, as you have made the mental choice to not put anything in it yet." << endl;
else {
cout << "Please type the name of the game you would like to delete: " << endl;
cin >> deleteChoice;
}
}
void sortGames(VidyaGames array[], int &k)
{
}
you forget to set value in your array.
i add 4 lines to function getGames:
array->Title = Title;
array->Date = Date;
array->Developer = Developer;
k++;
and change one line in function displayGames:
for (int i=0; i < k; i++)
this is final code:
// test_3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
struct VidyaGames {
string Title;
string Date;
string Developer;
};
void getGames(VidyaGames array[], int &k);
void displayGames(VidyaGames array[], int &k);
void deleteGames(VidyaGames array[], int &k);
void sortGames(VidyaGames array[], int &k);
const int MAX = 150;
string Title;
string Date;
string Developer;
int main()
{
char choice;
VidyaGames array[MAX];
bool kek = true;
int k = 0;
do
{
cout << "Welcome to the 'Super Incredible Amazing Game Storage-o-Tron 5000(and one)." << endl;
cout << " " << endl;
cout << "Please select which task you would like to perform by typing in the " << endl;
cout << "corresponding letter in the bracket: " << endl;
cout << " " << endl;
cout << "[I]nput a game into the list." << endl;
cout << "[D]isplay the games you have stored." << endl;
cout << "[S]ort the games you have stored." << endl;
cout << "[R]emove a game from the list." << endl;
cout << "[Q]uit the program." << endl;
cin >> choice;
switch (choice)
{
case 'I': getGames(array, k); break;
case 'D': displayGames(array, k); break;
case 'S': deleteGames(array, k); break;
case 'R': deleteGames(array, k); break;
case 'Q': kek = false; break;
default : cout << "Hey. Remember when I gave you the specific options you were allowed to choose?" << endl;
cout << "Maybe enter one of those?" << endl;
cout << " " << endl;
}
}
while (kek);
cout << "You have killed me." << endl;
}
void getGames(VidyaGames array[], int &k)
{
system("cls");
VidyaGames tmp;
char lel[100];
cout << "Enter the title of your game: " << endl;
getline (cin, Title);
cout << "Enter the date your game was published: (Example: March 15th, 2014)" << endl;
getline (cin, Date);
cout << "Enter the developer of your game: " << endl;
getline (cin, Developer);
array->Title = Title;
array->Date = Date;
array->Developer = Developer;
k++;
}
void displayGames(VidyaGames array[], int &k)
{
system ("cls");
if (k==0)
cout << "There is literally nothing in this list, as you have made the mental choice to not put anything in it yet." << endl;
else if (k > 0) {
for (int i=0; i < k; i++)
{
cout << "Title: " << array[i].Title << endl;
cout << "Release Date: " << array[i].Date << endl;
cout << "Developer: " << array[i].Developer << endl;
}
}
}
void deleteGames(VidyaGames array[], int &k) {
system("cls");
char deleteChoice;
if (k==0)
cout << "There is literally nothing in this list, as you have made the mental choice to not put anything in it yet." << endl;
else {
cout << "Please type the name of the game you would like to delete: " << endl;
cin >> deleteChoice;
}
}
void sortGames(VidyaGames array[], int &k)
{
}
Related
I'm a novice coding student and trying to create a menu using structs, functions, and switch statements to make a mini database for a class assignment. I'm trying to implant the functions into the switch statements.
I'm getting errors on lines 87 and 137 and I'm not sure where I'm going wrong. Any help, explanation, or correction is much appreciated.
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;
// Jaret Clark
// Week 3 Interactive Assignment
// INT 499
// Prof. Joseph Issa
// 03/31/2022
struct EZTechMovie {
string name;
string *cast[10];
string rating;
};
void displaymovie(EZTechMovie movie, int cast_num) {
int i;
cout << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout << endl;
cout << "Your entry:\n";
//Movies
cout << endl;
cout << "Movie TITLE: " << movie.name;
cout << endl;
//Movie rating
cout << "Movie Rating: " << movie.rating;
cout << endl;
//Cast name
cout << "Main Cast Members: \n";
//loop for cast members ~ stores in array
for (int i = 0; i < cast_num; ++i) {
cout << movie.cast[i];
cout << endl;
}
}
void mainmenu() {
string movie_input;
int m;
cout << endl;
cout << "Would you like to store movies into database? (yes or no) ";
getline(cin, movie_input);
cout << endl;
if (movie_input == "yes") {
string cont;
string cast_name;
int x, m, n, i, cast_num;
EZTechMovie moviedb[100];
cout << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout << endl;
for (n = 0; n < 100; n++) {
cout << "Movie Title: ";
getline(cin, moviedb[n].name);
cout << endl;
cout << "Movie rating: ";
getline(cin, moviedb[n].rating);
cout << endl;
cout << "How many cast do you want to enter? ";
cin >> cast_num;
cout << endl;
cin.ignore();
for (i = 0; i < cast_num; i++) {
cout << "Cast name: First and Last name: ";
getline(cin, moviedb[n].cast[i]);
cout << endl;
}
cout << endl;
displaymovie(moviedb[n], cast_num);
cout << endl;
cout << "Add more movies? (yes or no) ";
getline(cin, cont);
if (cont == "no") {
break;
}
cout << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout << endl;
}
}
else if (movie_input == "no") {
return;
}
else {
cout << "INVALID Input";
mainmenu();
}
}
// menu
void movieMenu() {
int choice;
EZTechMovie movie;
do {
cout << "***********************Welcome to EZTechMovie Movie Entry Menu***********************" << endl;
cout << "Press 1 to Enter Movie Info - Name, Cast Members, and Rating.\n";
cout << "Press 2 to Retrieve movie info recently entered.\n";
cout << "Press 3 To Quit program.\n";
// evaluate menu options in switch case
switch (choice) {
case 1:
mainmenu();
break;
case 2:
displaymovie(EZTechMovie movie, int cast_num);
break;
case 3:
cout << "Thank you and Goodbye!";
break;
default:
cout: "Invalid Selection. Try again!\n";
}
//get menu selection
cin >> choice;
} while (choice != 3);
}
int main() {
movieMenu();
}
Regarding the error on line 87 (getline(cin, moviedb[n].cast[i]);) :
moviedb[n].cast[i] is a std::string*, not std::string like you might have meant.
A quick compilation fix would be to use:
getline(cin, *(moviedb[n].cast[i]));
i.e. dereference the pointer.
However - this code raises other design/programming issues:
Why do you use std::string* and not std::string in the first place.
Why do you use C style array instead of std::vector (or std::array if you can commit to the size). This is relevant for both: string *cast[10]; and EZTechMovie moviedb[100];
I have an assignment here that I've been working on for the past few hours and have been met with a problem. Whenever I compile and run the program in VS Code it throws me into an infinite loop where the text of the "menu" is printed repeatedly. I imagine this has something to do with the for(;;){ loop at the beginning of the menu.
I've deleted the for(;;){ statement at the beginning of the menu, and the continuous scrolling stopped, but I was unable to input any numbers (cases) and the program, essentially, just printed the menu and that was the end.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Student {
string name;
float GPA;
public:
string getName() const
{
return name;
}
float getGPA() const
{
return GPA;
}
void setName(string Name)
{
name = Name;
}
void setGPA(float gpa)
{
GPA = gpa;
}
Student(const string& name, float gpa)
: name(name)
, GPA(gpa)
{
}
Student()
{
}
void printDetails(Student s[], int n)
{
cout << setw(20) << "Name: " << setw(10) << "GPA: " << endl;
cout << "=========================================" << endl;
for (int i = 0; i < n; i++) {
cout << setw(20) << s[i].getName() << setw(10) << s[i].getGPA() << endl;
}
}
float calcAverageGPA(Student s[], int n)
{
float avg = 0;
float sum = 0;
for (int i = 0; i <= n; i++) {
sum += s[i].getGPA();
}
avg = sum / n;
return avg;
}
float getGPAbyName(Student s[], int n, string name)
{
for (int i = 0; i <= n - 1; i++) {
if (s[i].getName() == name)
return s[i].getGPA();
}
return -1;
}
void showListByGPA(Student s[], int n, float gpa)
{
cout << setw(20) << "Name: " << setw(10) << "GPA: " << endl;
cout << "=========================================" << endl;
for (int i = 0; i < n - 1; i++) {
if (s[i].getGPA() > gpa)
;
cout << setw(20) << s[i].getName() << setw(10) << s[i].getGPA() << endl;
}
}
};
int main()
{
int n = 0;
int ch;
Student s[50];
string name;
float GPA;
float avg = 0;
cout << "======================" << endl;
cout << "(1): Add a student" << endl;
cout << "(2): Print the details of a student" << endl;
cout << "(3): Get the GPA of a Student by name." << endl;
cout << "(4): Get names of students based on GPA." << endl;
cout << "(6): Quit the program." << endl;
cout << "Enter your option" << endl;
switch (ch) {
case '1':
cout << "\nEnter student's name" << endl;
cin >> name;
cout << "Enter GPA" << endl;
cin >> GPA;
s[n] = Student(name, GPA);
n++;
break;
case '2':
s[0].printDetails(s, n);
break;
case '3':
cout << "\nEnter the name of a student" << endl;
cin >> name;
GPA = s[0].getGPAbyName(s, n, name);
if (GPA == -1) {
cout << "\nStudent not found!" << endl;
}
else
cout << "\nGPA is: " << endl;
break;
case '4':
cout << "\nEnter GPA: " << endl;
cin >> GPA;
s[0].showListByGPA(s, n, GPA);
break;
case '5':
avg = s[0].calcAverageGPA(s, n);
cout << "\nAverage GPA: " << avg << endl;
break;
case '6':
exit(0);
}
}
I suspect the problem resides in main(). I included the prior blocks of the program in case they were necessary to provide any suggestions.
You obviously want to read in "ch".
You've also made this an int, and don't zero-initialize, which can cause some undefined behaviour if you don't set it beforehand.
The switch case should be
switch(X){
case 1:
// Code
break;
}
etc..
The difference is "1" or 1. It evaluates an enumeration value, instead of strings.
For safety: you might want to add a case default, which is common practice.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
// these should have all of my declared functions
int start(char x, char y, char z);
void pim();
void pmm();
void login();
void createa();
//my assignment wants me to have only one function used
int main()
{
start(pim(), login(), createa());
return 0;
}
//This should be the start of the code
void pim()
{
cout << "Please select a letter from the menu below:" << endl;
cout << "l - Login" << endl;
cout << "c - Create New Account" << endl;
cout << "q - Quit" << endl;
}
// this is to post a menu after you make the first choice above
void pmm()
{
cout << "d - Deposit Money" << endl;
cout << "w - Withdraw Money" << endl;
cout << "r - Request Balance" << endl;
cout << "q - Quit" << endl;
}
//this is to login to the account
void login()
{
cout << "Okay, you're in" << endl;
}
// this is to create an account
void createa()
{
int id = 0;
int password = 0;
cout << "create an ID of two numbers" << endl;
cin >> id;
cout << "Make a password of 4 numbers" << endl;
cin >> password;
}
// Starts and is basically the entire project
int start(char x, char y, char z)
{
char pim = x;
cout << pim;
int choice = 0;
char select = '\0';
cout << "Enter the choice that you want:";
cin >> choice;
if(select == 'l')
{
choice = 1;
}
else if( select == 'c')
{
choice = 2;
}
else
{
choice = 3;
}
switch (choice)
{
case 1:
cout << y << endl;
break;
case 2:
cout << z << endl;
break;
case 3:
exit(0);
break;
default:
cout << "This is not a command" << endl;
}
return 0;
}
I think what you are looking for is function pointers, eg:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
// these should have all of my declared functions
int start(void (*x)(), void (*y)(), void (*z)());
void pim();
void pmm();
void login();
void createa();
//my assignment wants me to have only one function used
int main()
{
start(&pim, &login, &createa);
return 0;
}
//This should be the start of the code
void pim()
{
cout << "Please select a letter from the menu below:" << endl;
cout << "l - Login" << endl;
cout << "c - Create New Account" << endl;
cout << "q - Quit" << endl;
}
// this is to post a menu after you make the first choice above
void pmm()
{
cout << "d - Deposit Money" << endl;
cout << "w - Withdraw Money" << endl;
cout << "r - Request Balance" << endl;
cout << "q - Quit" << endl;
}
//this is to login to the account
void login()
{
cout << "Okay, you're in" << endl;
}
// this is to create an account
void createa()
{
int id = 0;
int password = 0;
cout << "create an ID of two numbers" << endl;
cin >> id;
cout << "Make a password of 4 numbers" << endl;
cin >> password;
}
// Starts and is basically the entire project
int start(void (*x)(), void (*y)(), void (*z)())
{
x();
char choice = '\0';
cout << "Enter the choice that you want:";
cin >> choice;
switch (choice)
{
case 'l':
y();
break;
case 'c':
z();
break;
case 'q':
exit(0);
break;
default:
cout << "This is not a command" << endl;
}
return 0;
}
However, unless "function pointers" is the actual topic you are studying at the moment, then this is a very questionable design, even for a simple homework assignment. It would be cleaner and easier to read/manage to just call the functions directly instead, eg:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
// these should have all of my declared functions
int start();
void pim();
void pmm();
void login();
void createa();
//my assignment wants me to have only one function used
int main()
{
start();
return 0;
}
//This should be the start of the code
void pim()
{
cout << "Please select a letter from the menu below:" << endl;
cout << "l - Login" << endl;
cout << "c - Create New Account" << endl;
cout << "q - Quit" << endl;
}
// this is to post a menu after you make the first choice above
void pmm()
{
cout << "d - Deposit Money" << endl;
cout << "w - Withdraw Money" << endl;
cout << "r - Request Balance" << endl;
cout << "q - Quit" << endl;
}
//this is to login to the account
void login()
{
cout << "Okay, you're in" << endl;
}
// this is to create an account
void createa()
{
int id = 0;
int password = 0;
cout << "create an ID of two numbers" << endl;
cin >> id;
cout << "Make a password of 4 numbers" << endl;
cin >> password;
}
// Starts and is basically the entire project
int start()
{
pim();
char choice = '\0';
cout << "Enter the choice that you want:";
cin >> choice;
switch (choice)
{
case 'l':
login();
break;
case 'c':
createa();
break;
case 'q':
exit(0);
break;
default:
cout << "This is not a command" << endl;
}
return 0;
}
I'm a beginner at C++ and function,
Been getting the error "linker command failed with exit code 1" wondering anyone could give me some insight on how to resolve this problem, and on my overall code organization. Thank you very much for your time and opinion on this matter :)
ignore the below statement
(Due to lack of space I had to remove the #include statements which included the following iostream, iomanip, string and cmath).
#include<iostream>
#include<iomanip>
#include<string>
#include<cmath>
void header();
double number();
int menu();
void seeMenu();
void processChoice();
void negPos();
int squareRoot();
void evenOdd();
void numOfDigits();
void digitAtPos();
using namespace std;
int main()
{
int choice = 0;
header();
number();
menu();
seeMenu();
processChoice();
while (choice != 0) {
processChoice();}
return 0;
}
void header(){
cout << "Number Manipulator"<< endl; }
double number() {
double num;
cout << "Enter a number to continue: ";
cin >> num;
return num; }
int menu(){
cout << "\nHere are your choices:";
cout << endl << setw(29) << "1) Is it even or odd?";
cout << endl << setw(38) << "2) Is it positive or negative?";
cout << endl << setw(49) << "3) What is the square root?";
cout << endl << setw(41) << "4) How many digits in the number?";
cout << endl << setw(54) << "5) What is the digit at a particular location?";
cout << endl << setw(19) << "0) To Quit.";
}
int showMenu() {
int choice;
cout << endl << endl <<"Enter your choice: ";
cin >> choice;
return choice; }
void processChoice(int choice) {
switch (choice) {
case 1: squareRoot();
break;
case 2: evenOdd();
break;
case 3: negPos();
break;
case 4: numOfDigits();
break;
case 5: digitAtPos();
break;
default:
cout << "This is not a valid choice. Please try again!";
showMenu();
break; }}
void negPos(int num) {
if (num > 0) cout << "Number is Positive"<< endl;
if (num < 0) cout << "Number is Negative"<< endl;
if (num == 0) cout << "Number is Zero"<< endl; }
void evenOdd(int num) {
if (num%2 == 0) cout << "Number is even";
else cout << "Number is odd"; }
void squareRoot(int num) {
int numSqrt;
numSqrt=sqrt(num);
cout << "Square root of " << num << " is " << numSqrt; }
void numOfDigits(int num) {
int numDigits=0;
do {
num /= 10;
numDigits++;
} while(num);
cout << "The number of digits in " << num << " is " << numDigits; }
void digitAtPos(int num) {
int pos;
cout << "What Position?: ";
cin >> pos; }
//need to be completed
While quickly looking over your code, I noticed that the function processMenuChoice is declared as void processMenuChoice(), but it is defined as void processMenuChoice(int choice). And since the function needs that paramater, you need to change your while loop to
processMenuChoice(choice);
while(choice != 0)
{
processMenuChoice(choice);
}
Here is the fix to make your code work. Note, there are some other issues that you need to fix.
Hint: in void squareRoot(int num), the result is an integer...
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
void displayHeader();
double getNumber();
int showMenu();
void menu();
void processMenuChoice(int choice, int num);
void isPosNeg(int num);
void squareRoot(int num);
void isOddEven(int num);
void findNumDigits(int num);
void findDigitAtPosition(int num);
using namespace std;
int main()
{
int choice = 0;
displayHeader();
int num = getNumber();
menu();
choice = showMenu();
while (choice != 0) {
processMenuChoice(choice, num);
choice = showMenu();
}
return 0;
}
void displayHeader(){
cout << "Number Manipulator\n"; }
double getNumber() {
double num;
cout << "Enter a number to continue: ";
cin >> num;
return num; }
void menu(){
cout << "\nHere are your choices:";
cout << endl << setw(29) << "1) Is it even or odd?";
cout << endl << setw(38) << "2) Is it positive or negative?";
cout << endl << setw(49) << "3) What is the square root?";
cout << endl << setw(41) << "4) How many digits in the number?";
cout << endl << setw(54) << "5) What is the digit at a particular location?";
cout << endl << setw(19) << "0) To Quit.";
}
int showMenu() {
int choice;
cout << "\n\nEnter your choice: ";
cin >> choice;
return choice; }
void processMenuChoice(int choice, int num) {
switch (choice) {
case 1: isPosNeg(num);
break;
case 2: isOddEven(num);
break;
case 3: squareRoot(num);
break;
case 4: findNumDigits(num);
break;
case 5: findDigitAtPosition(num);
break;
default:
cout << "This is not a valid choice. Please try again!";
showMenu();
break; }}
void isPosNeg(int num) {
if (num > 0) cout << "Number is Positive\n";
if (num < 0) cout << "Number is Negative\n";
if (num == 0) cout << "Number is Zero\n"; }
void isOddEven(int num) {
if (num%2 == 0) cout << "Number is even";
else cout << "Number is odd"; }
void squareRoot(int num) {
int numSqrt;
numSqrt=sqrt(num);
cout << "Square root of " << num << " is " << numSqrt; }
void findNumDigits(int num) {
int numDigits=0;
do {
num /= 10;
numDigits++;
} while(num);
cout << "The number of digits in " << num << " is " << numDigits; }
void findDigitAtPosition(int num) {
int pos;
cout << "What Position?: ";
cin >> pos; }
//need to be completed
i need help. I have an array of 10, and it consists of a string and a value initialized to it. My question is how i can ask the user to input how much more to add or how much to remove. then display the updated list.
for example choose valve, then add 2 more, which will make it 12, then display the updated array with the other arrays. i need to pass it through a function also. please and thank you for the help in advance, here it my code so far. Feel free to criticize as much as you guys would like. It will only help me get better :)
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <cstring>
#include <string>
using namespace std;
//define Structure as invParts
struct invParts{
string name;
int qty;
};
void addparts(invParts *&c,int);
void removeparts(invParts *&c, int);
int main()
{
invParts *descrip;
int row = 0;
char choice;
invParts bin[10]= { {"valve",10}, {"Bearing",5}, {"Bushing",21},
{"Coupling",7}, {"Flange",5}, {"Gear",5},
{"Gear House", 5}, {"Vacuum Gripper", 25},
{"Cable",18}, {"Rod",12}, };
cout<<"-----------------------------------------------------" << endl;
cout<<"Part Description" << " " << "Number of parts in the bin" << endl;
cout <<"----------------------------------------------------" << endl;
cout << endl;
for(row = 0; row < 10; row++)
{
cout << setw(11)<< left <<bin[row].name << setw(25) << right << bin[row].qty<< endl;
}
cout << endl;
cout << "Here are 3 options" << endl;
cout << "Type A , to Add parts" << endl;
cout << "Type R , to Remove parts" << endl;
cout << "Type E, to Exit Program" << endl;
cout << "Choose your option: ";
cin >> choice;
cout << endl;
switch (choice)
{
int num;
case 'A' :
case 'a' : cout <<"You will now add" << endl;
addparts(descrip,num);
break;
case 'R':
case 'r': cout <<"You will now remove" << endl;
//removeparts(descrip,num);
break;
case 'E':
case 'e': cout<<"Now exiting program" << endl;
exit(0);
}
system("pause");
return 0;
}
void addparts(invParts *&c,int number)
{
string what;
int n;
cout <<"Which part? " << endl;
cin >> what;
//am i doing this right?
if ( what == "valve" || what == "Valve")
cout <<"How much do you want to add? "<<endl;
cin >> n;
}
/*void removeparts(invParts *&c, int)
{
//you guys can show me how to do the add, i can do the remove
}
*/
You are using the wrong data structure to capture the inventory. You need something like
typedef std::map<std::string, int> Inventory;
Here's an updated version of your program.
#include <iostream>
#include <string>
#include <iomanip>
#include <map>
using namespace std;
typedef std::map<std::string, int> Inventory;
void initializeInventory(Inventory& inv);
void displayInventory(Inventory const& inv);
void addparts(Inventory& inv);
void removeparts(Inventory& inv);
int main()
{
Inventory inv;
char choice;
initializeInventory(inv);
displayInventory(inv);
while ( true )
{
cout << "Here are 3 options" << endl;
cout << "Type A , to Add parts" << endl;
cout << "Type R , to Remove parts" << endl;
cout << "Type E, to Exit Program" << endl;
cout << "Choose your option: ";
cin >> choice;
cout << endl;
switch (choice)
{
case 'A' :
case 'a' : cout <<"You will now add" << endl;
addparts(inv);
break;
case 'R':
case 'r': cout <<"You will now remove" << endl;
//removeparts(inv);
break;
case 'E':
case 'e': cout<<"Now exiting program" << endl;
exit(0);
}
displayInventory(inv);
}
return 0;
}
void initializeInventory(Inventory& inv)
{
inv["Valve"] = 10;
inv["Bearing"] = 5;
inv["Bushing"] = 21;
inv["Coupling"] = 7;
inv["Flange"] = 5;
inv["Gear"] = 5;
inv["Gear House"] = 5;
inv["Vacuum Gripper"] = 25;
inv["Cable"] = 18;
inv["Rod"] = 12;
}
void displayInventory(Inventory const& inv)
{
cout<<"-----------------------------------------------------" << endl;
cout<<"Part Description" << " " << "Number of parts in the bin" << endl;
cout <<"----------------------------------------------------" << endl;
cout << endl;
for (auto item : inv )
{
cout << setw(15) << left << item.first << setw(25) << right << item.second << endl;
}
cout << endl;
}
void addparts(Inventory& inv)
{
string what;
int n;
cout <<"Which part? " << endl;
cin >> what;
//am i doing this right?
Inventory::iterator iter = inv.find(what);
if ( iter == inv.end() )
{
cout << "There is no such part in the inventory.\n";
return;
}
cout <<"How much do you want to add? "<<endl;
cin >> n;
iter->second += n;
}
void removeparts(Inventory& inv)
{
// ????
}
Update
The inventory can be initialized using:
Inventory inv = { {"Valve",10}, {"Bearing",5}, {"Bushing",21},
{"Coupling",7}, {"Flange",5}, {"Gear",5},
{"Gear House", 5}, {"Vacuum Gripper", 25},
{"Cable",18}, {"Rod",12} };
There is no need for a separate function to initialize the inventory.