I have a question about variable scope in a C++ class. The problem I'm working on says to create a class that holds an array of structures, with each structure holding the name, cost, and amount for a particular type of drink.
The class should have public member functions to buy a drink and display the menu, and private functions to get and validate money input (called by buy_drink) and to display an end of day report (called by the destructor).
I have a problem with the scope in the private function input_money. I get an error saying that the array has not been defined yet. I tested the display_data function (for printing the menu), and it worked fine on its own, but now I can't figure out why input_money would have a scope error and display_data wouldn't. Here is the header file:
/* need to create a class that holds an array of
5 structures, each structure holding string drink name,
double cost, and int number in machine
class needs public functions to display data and
buy drink
private functions input money -- called by buy_drink to accept,
validate, and return to buy drink the amount of money input
daily report -- destructor that reports how much money
was made daily and how many pops are left in machine */
#ifndef DRINKS_H
#define DRINKS_H
#include <string>
class Drinks
{
private:
struct Menu
{
std::string name;
double cost;
int number;
};
Menu list[5]; // array of 5 menu structures
double money_made; // track money made during the day
double input_money(int); // return validated money to buy_drink()
void daily_report(); // called by deconstructor
public:
Drinks();
~Drinks();
void display_data();
void buy_drink(int);
};
#endif
And here is the implementation file:
/* implementation file for Drinks class */
#include <iostream>
#include <string>
#include "drinks.h"
using namespace std;
const int SIZE = 5;
const int START_SIZE = 100;
Drinks::Drinks()
{
list[0].name = "Coke";
list[1].name = "Root Beer";
list[2].name = "Orange Soda";
list[3].name = "Grape Soda";
list[4].name = "Bottled Water";
for (int count = 0; count < (SIZE-1); count++)
list[count].cost = .75;
list[4].cost = 1;
for (int count = 0; count < SIZE; count++)
list[count].number = 20;
money_made = 0;
}
void Drinks::display_data()
{
for (int count = 0; count < SIZE; count++) {
if (count == 0)
cout << count+1 << list[count].name << "\t\t$ ";
else
cout << count+1 << list[count].name << "\t$ ";
cout << list[count].cost << "\t"
<< list[count].number << endl;
}
}
double input_money(int c)
{
double input;
cin >> input;
while (input != list[c].cost) {
if (input < list[c].cost) {
cout << "Not enough money.\n"
<< "Enter " << list[c].cost - input
<< " more cents to buy\n\n> ";
cin >> input;
}
else if (input > list[c].cost) {
cout << "Too much money.\n"
<< "I only need $" << list[c].cost << endl
<< "Enter " << input - list[c].cost
<< " less money: ";
cin >> input;
}
}
return input;
}
void Drinks::buy_drink(int c) // this receives an int choice (to access corresponding structure in the list array)
{
double input;
cout << "Enter " <<list[c].cost
<< " to purchase " << list[c].name
<< "\n\n> ";
input = input_money(c); // input money returns a validated and accurate price for the drink and is passed the choice to access array
list[c].number -= 1;
money_made += list[c].cost; // add cost of drink to money made
}
void Drinks::daily_report()
{
int end_size = 0;
for (int count = 0; count < SIZE; count++)
end_size += list[count].number;
cout << "Today, you made $" << money_made << endl;
cout << "There are " << START_SIZE - end_size
<< " drinks left in the machine" << endl;
}
Drinks::~Drinks()
{
daily_report();
cout << "goodbye mr anderson\n";
}
Any help would be much appreciated! I can't seem to figure out why the input_money function does not have access to the structures in the array.
Thank you!
EDIT: Total noob mistake/carelessness. Forgot to add the name of the class in the input_money function definition and use the scope resolution operator (i.e. should be Drinks::input_money(int c)). Thanks to those who answered.
double Drinks::input_money(int c)
// ^^^^^^^^ forgot this
You forgot the class name while providing the implementation.
Notice the difference between your definition of
void Drinks::display_data
and
double input_money(int c)
In the second case you have defined a free function that is not a member of the class and has no information about the class members. It should be
double Drinks::input_money(int c)
Related
The objective is to output the same data as the first program.
The first program requires manual input of the room number and room cost. The second (problematic) program requires it to auto populate with the room number and room cost being hard coded in to an array within the main.
A default constructor of the class "room" is needed to populate the array and add setter functions to set the values of the room number and cost.
The objective of the whole program is to create the array, populate with test data and then list the data about the rooms.
Program 1 (manual in put) - this works fine;
#include<iostream>
using namespace std;
class room
{
int roomNo;
float roomCost;
public:
void GetData ();
void PutData ();
};
void room::GetData ()
{
cout << "\n\tEnter room number : ";
cin >> roomNo;
cout << "\n\tEnter room cost : ";
cin >> roomCost;
}
void room::PutData ()
{
cout << "\n\t\t\t" << roomNo << " \t|\t " << roomCost;
}
int main ()
{
int roomNo;
room roomList[10];
for (roomNo = 0; roomNo < 10; roomNo++)
{
cout << "\nEnter details of " << roomNo + 1 << " room" << endl;
roomList[roomNo].GetData ();
}
cout <<"\n*******************************************************************************";
cout << "\n\t\t\t Room Details";
cout <<"\n*******************************************************************************";
cout << "\n\t\t Room Number \t| Cost per Night";
cout << "\n\t\t--------------------------------------";
for (roomNo = 0; roomNo < 2; roomNo++)
roomList[roomNo].PutData();
getchar ();
getchar ();
}
Second (problematic) program;
#include<iostream>
using namespace std;
class room
{
int roomNo;
float roomCost;
public:
room();
void PutData ();
};
room::room()
{
cout<<"\n\t\t\t"<<roomNo<<" \t|\t "<<roomCost;
}
void room::PutData ()
{
cout << "\n\t\t\t" << roomNo << " \t|\t " << roomCost;
}
int main ()
{
int roomNo[10] = {1,2,3,4,5,6,7,8,9,10};
float roomCost[10] = {100.00, 90.00, 85.50, 80.00, 80.00, 50.00, 50.00, 45.50, 45.50, 40.00};
room roomList[10]; //Statement 3 : Creating Array of 3 Employees
cout <<"\n*******************************************************************************";
cout << "\n\t\t\t Room Details";
cout <<"\n*******************************************************************************";
cout << "\n\t\t Room Number \t| Cost per Night";
cout << "\n\t\t--------------------------------------";
for (int roomNo = 0; roomNo < 10; roomNo++)
roomList[roomNo].PutData();
getchar ();
getchar ();
}
The problem is that roomNo and roomCost never actually get set to anything for each room instance. The class won't automatically pull from the arrays you define in main.
Additionally,
roomList[roomNo].PutData();
Accesses uninitialized data:
void room::PutData ()
{
cout << "\n\t\t\t" << roomNo << " \t|\t " << roomCost;
}
(The constructor has the same problem)
The solution is to initialize each room with the correct data. I recommend using the constructor:
room(int _roomNum, float _roomCost) : roomNo(_roomNum), roomCost(_roomCost)
{}
Then use a vector to initialize (instead of room roomsList[10])
std::vector<room> rooms;
rooms.reserve(10);
for(size_t i = 0; i < 10; ++i)
rooms.emplace_back(roomNo[i], roomCost[i]);
Live Demo
You should have two setters, one for each member, and then write your loop like
for (int n = 0; n < 10; n++)
{
roomList[n].setNumber(roomNo[n]);
roomList[n].setCost(roomCost[n]);
}
(On a side note, you should do the same with the first program and handle user input in main.)
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int numofEmployees();
int daysMissed(int);
int AverageMissed(int, int);
int main()
{
cout << "Welcome to employee absentee calculator!" << endl;
int numEmployees = numofEmployees();
int Missed = daysMissed(numEmployees);
double misAverage = AverageMissed(numEmployees, Missed);
cout << "There are " << numEmployees << " in the company. They have missed " << Missed << " days total. On average, they have missed " << misAverage << " days." << endl;
return 0;
}
int numofEmployees() {
cout << "How many employees are in your company? ";
int employees;
cin >> employees;
while (employees < 1) {
cout << "Employee count must 1 or greater!" << endl;
}
return employees;
}
int daysMissed(int numEmployees) {
int Absence, totAbsence = 0;
for (int i = numEmployees; i < numEmployees; i++) {
cout << "How many days has each employee missed this passed year? ";
cin >> Absence;
totAbsence += Absence;
}
while (Absence < 0) {
cout << "Values entered must be positive numbers!" << endl;
cin >> Absence;
}
return totAbsence;
}
int AverageMissed(int numEmployees, int Missed){
double Average;
Average = double(numEmployees) / double(Missed);
return Average;
}
This code is being used to calculate the average number of employee absences by way of using three functions. The second function is not working correctly as it is not being called properly by the main. This is for a school assignment.
The problem is daysMissed - if numEmployees is <= 0, then Absense will be uninitialized. But, you say, "I check that in numofEmployees" - the problem is that the compiler doesn't do that sort of whole-program analysis before issuing these warnings.
There is another problem: daysMissed is wrong (twice). If there are two employees, and I enter -2 and 1, there will be no error for the negative number. If on the other hand, if I enter 1 and -2, you never correct totAbsence. You would be much better off writing a little function which reads a number >= some limit in a loop, and keeps prompting until given the correct value. Something like:
int read(const char* prompt, const char* err_prompt, int limit) {
cout << prompt << endl;
for(;;) {
int result;
cin >> result;
if (result >= limit) {
return result;
}
cout << err_prompt << endl;
}
}
Then daysMissed becomes much pleasanter to write - and you can use the same function to read the number of employees (which will go into an infinite loop at the moment)
You should also validate a division by zero plus change the return type.
double AverageMissed(int numEmployees, int Missed){
if (Missed > 0) return double(numEmployees) / Missed;
return 0;
}
by the way, there is no need to cast both operands in the division (/). Casting one of them will be enough to return a double type.
I know this may be something obvious to some with experience but I am in the middle of my first real class for C++ programming. I have come across something in my code that I have been trying to resolve without any success. I am trying to extract the sum of the total value of "m_cost" stored within a array that is inside of these class modules. I want to output the total value inside of a switch statement for print out. Just seeing if someone can point me in the right direction or if I have completely gone off-track with the logic.
Project Code section in question:
void materialsMenu()
{
Inventory record[MAX_REC];
int i, n;
cout << "\n=====Inventory Management=====\n";
cout << "\nHow many Materials are there to be used? : ";
cin >> n;
cout << "Enter " << n << " Materials\n";
for (i = 0; i < n; i++)
record[i].getdata();
cout << "\n\n---Material Information---\n";
cout << "\n" << setw(8) << "Item Name "
<< setw(10) << " Price per foot "
<< setw(19) << " Cost " << endl;
cout << endl << "-------------------------------------------" << endl;
for (i = 0; i < n; i++)
record[i].showdata();
}
void Inventory::getdata() {
cout << endl;
cout << "\nEnter Material Name : ";
cin >> itemName;
cout << "Enter Price Per Foot : ";
cin >> ppf;
cout << "Enter Total Length Needed in Feet (ft) : ";
cin >> length;
cout << endl;
m_cost = ppf*length;
m_costT=???????? **this is the issue**
}
Materials.H file contents:
#ifndef MATERIALS_H
using namespace std;
class Materials {
private:
char itemName[15];
float ppf;
float length;
double m_cost;
float m_costT;
public:
Materials()
{
ppf = 0;
length = 0;
m_cost = 0;
m_costT = 0;
}
Materials(int itemName, float ppf, float length, double m_cost, float
m_costT)
{
length = getLength();
ppf = getPpf();
m_cost = getCost();
m_costT = getTotal();
}
float getLength()
{
return length;
}
float getPpf()
{
return ppf;
}
double getCost()
{
return m_cost;
}
float getTotal()
{
return m_costT;
}
void getdata();
void showdata();
};
#endif // !MATERIALS_H
Consider something like this to hold your records. It's very simple, but it demonstrates the idea of one class holding another and shows information hiding and all sorts of other tidbits. You are struggling to get that total because the Record class shouldn't care about this. The total is a concept outside of the scope of a Record. And as such, it is very difficult to calculate this from where you wanted to do it. (Though it is possible, it breaks all sorts of C++ rules and should be avoided)
Example:
class Record;
class RecordHolder
{
public:
int GetTotal()
{
int retVal = 0;
for(int i=0; i<10; i++) // Magic number 10 for demo purposes only...
{
retVal += records[i].m_cost; // Or use a public get function.
}
return retVal;
}
private:
Record records[10]; // Magic number 10 for demo purposes only...
};
The RecordHolder could also do printing, add/removing records, etc. It controls the records array. The Records are just Records and don't care about such management.
Well I am trying to do an exercise in my programming book and it's difficult for me to grasp exactly what it wants.
My "enterAccountData()" function is suppose to ask the user for an account number and a balance neither of which can be negative and the account number cannot be less than 1000
The second one is the one I am stuck on "computeInterest()" THis function is suppose to accept an integer argument that represents the number of years the account will earn interest. The function then displays the account number from the previous function and displays its ending balance at the end of each year based on the interest rate attached to the BankAccount class. (The interest rate on "BankAccount" has to be a constant static field which is set at 3 percent(0.03)).
So My question is this: How do I set up "computeInterest()" too allow it to calculate the interest using the constant static field when my debugger will not allow me to actually keep the field as a constant? I am not trying to stop any random errors from happening for now, just trying to get the jist of what the book is exactly asking for. Here is my code.
#include <iostream>
#include <iomanip>
using namespace std;
class BankAccount
{
private:
int accountNum;
double accountBal;
static double annualIntRate;
public:
void enterAccountData(int, double);
void computeInterest();
void displayAccount();
};
//implementation section:
double BankAccount::annualIntRate = 0.03;
void BankAccount::enterAccountData(int number, double balance)
{
cout << setprecision(2) << fixed;
accountNum = number;
accountBal = balance;
cout << "Enter the account number " << endl;
cin >> number;
while(number < 0 || number < 999)
{
cout << "Account numbers cannot be negative or less than 1000 " <<
"Enter a new account number: " << endl;
cin >> number;
}
cout << "Enter the account balance " << endl;
cin >> balance;
while(balance < 0)
{
cout << "Account balances cannot be negative. " <<
"Enter a new account balance: " << endl;
cin >> balance;
}
return;
}
void BankAccount::computeInterest()
{
const int MONTHS_IN_YEAR = 12;
int months;
double rate = 0;
int counter = 0;
BankAccount::annualIntRate = rate;
cout << "How many months will the account be held for? ";
cin >> months;
counter = 0;
do
{
balance = accountBal * rate + accountBal;
counter++;
}while(months < counter);
cout << "Balance is:$" << accountBal << endl;
}
int main()
{
const int QUIT = 0;
const int MAX_ACCOUNTS = 10;
int counter;
int input;
int number = 0;
double balance = 0;
BankAccount accounts[MAX_ACCOUNTS];
//BankAccount display;
counter = 0;
do
{
accounts[counter].enterAccountData(number, balance);
cout << " Enter " << QUIT << " to stop, or press 1 to proceed.";
cin >> input;
counter++;
}while(input != QUIT && counter != 10);
accounts[counter].computeInterest();
system("pause");
return 0;
}
The constant field is easy enough:
class BankAccount
{
...
const static double annualIntRate = 0.03;
...
}
(Does your debugger complain about that? I'm using gcc 4.2.1) But there are other troubling things in your code, like the way computeInterest tries to set rate to zero, and the while loop... needs work.
EDIT:
One good principle is worth a hundred specific corrections. When you develop a complicated function, don't try to do it all at once; start with a simple piece, get that working perfectly, then build up. F'rinstance, computeInterest. You have several independent parts to get working: going through the while loop the correct number of times, calculating interest increments, keeping track of the balance-- right now computeInterest does none of these correctly. Tackle them one at a time, or in parallel if you want, but never combine parts that don't work.
boy it's been a LONG time since I've worked in C++, but I think all you have to do is this:
static double annualIntRate =.03;
in the "private" section of your code.
and then you can use annualIntRate as though it was a global (to each instance of the class) variable.
/*I got stumped within my code. I think classes will be simpler than structures, but the chapter within my book makes me do structures. : / I am currently getting an error message that my function was not matched up for an overloaded function. The book does talk about them, but the examples of overloading functions in the book aren't helping me out. Also the book wants me to enter account numbers and fill in the objects and when they are asked for an account number they should have the opportunity to "QUIT" entering numbers and proceed onto the next part of the program; that whole way of thinking has my brain a bit fried and I was hoping I could get some help. I apologize if the formatting of my code is messy, I tried to reformat it within here so it would all go into the code brackets.
The Error happens at line... 161 at the displayAccounts function. Parameters were different within the top and bottom of the two functions I changed it and it works. I am going to go over different parts and if its correct post the correct code.*/
I figured out exactly the question that I need. I need the "QUIT" loop to be allowed to be followed up within the account numbers. This would allow the user to enter in a 0 at any time when asked to enter an account number and this was what was confusing me the most.
#include <iostream>
#include <iomanip>
using namespace std;
struct BankAccount
{
void enterAccountsData(BankAccount *accounts);
void computeInterest(BankAccount *accounts);
void displayAccounts(BankAccount *accounts, const int QUIT);
int accountNum; // holds the account number.
double accountBal; // holds the account balance.
double annualInterest; // holds the interest rate.
int term; // holds the term for the accounts.
};
int main()
{
const int MAX_ACCOUNTS = 100; // The maximum number of bank accounts.
const int QUIT = 0; // sentinal value.
int input;
int num = 0;
BankAccount data[MAX_ACCOUNTS];
BankAccount display;
cout << "Enter " << QUIT << " to stop, otherwise enter 1 and procreed.";
cin >> input;
while(true)
{
if(input != QUIT)
{
data[MAX_ACCOUNTS].enterAccountsData(data);
data[MAX_ACCOUNTS].computeInterest(data);
}
else
{
break;
}
}
display.displayAccounts(data, QUIT);
//system("pause");
return 0;
}
void BankAccount::enterAccountsData(BankAccount *accounts)
{
cout << setprecision(2) << fixed;
const int NUM_OF_ACCOUNTS = 100; // the number of bank accounts. (change the number for more bank accounts)
int found;
int quit = 0;
/* First for loop which asks and holds the account information
entered in by the user. */
for(int num = 0; num < NUM_OF_ACCOUNTS; num++)
{
do
{
found = 0;
cout << "Enter in account # " << (num + 1) << endl;
cin >> accounts[num].accountNum; // holds the value of the account number
// Checks if the account number is valid.
while(accounts[num].accountNum < 999 || accounts[num].accountNum > 10000)
{
cout << "Account number must be four didgets:" << endl;
cin >> accounts[num].accountNum;
}
// Checks if the account numbers are the same.
for(int check = 0; check < num; check++)
{
while(accounts[num].accountNum == accounts[check].accountNum)
{
cout << endl << "Account Numbers cannot be the same, enter in a new account number." << endl;
found = 1;
break;
}
}
} while(found); // end of do while.
// Holds the values for the account balances.
cout << "Enter the accounts balance." << endl;
cin >> accounts[num].accountBal;
// Makes sure that the account balance is not negative.
while(accounts[num].accountBal < 0)
{
cout << "Account cannot have a negitive balance." << endl;
cin >> accounts[num].accountBal;
}
// Holds the interest rate.
cout << endl << "Enter the interest rate for account # " << (num + 1) << endl;
cin >> accounts[num].annualInterest;
// Makes sure the interest rate is valid
while(accounts[num].annualInterest > 0 && accounts[num].annualInterest > 0.15)
{
cout << endl << "Annual interest must be from 0 to 0.15." << endl;
cin >> accounts[num].annualInterest;
}
// Makes sure the interest rate is not negetive
while(accounts[num].annualInterest < 0)
{
cout << endl << "Interest rate cannot be negetive" << endl;
cin >> accounts[num].annualInterest;
}
// Holds the value for the length of the interest.
cout << endl << "How many years will this interest rate be held for? " << endl;
cin >> accounts[num].term;
//Checks for valid length of time for the term held
while(accounts[num].term < 0 || accounts[num].term > 11)
{
cout << "The Term must be greater than 1 and should not exceed 10" << endl;
cin >> accounts[num].term;
}
}
cout << "If you wish to stop enter 0 otherwise type 1 to proceed" << endl;
cin >> quit;
if(quit = 0)
{
return;
}
}
void BankAccount :: computeInterest(BankAccount *accounts)
{
const int NUM_OF_ACCOUNTS = 100; // the number of bank accounts.
const int MONTHS_IN_YEAR = 12;
double total = 0;
double average = 0;
for(int num = 0; num < NUM_OF_ACCOUNTS; num++)
{
/*Goes through the term year and calculates the total
of each account balance. Then calculates the average. */
for(int year = 0; year < accounts[num].term; year++)
{
for(int month = 0; month < MONTHS_IN_YEAR; month++)
{
accounts[num].accountBal = (accounts[num].accountBal * accounts[num].annualInterest) + accounts[num].accountBal;
}
int month = 1;
cout << endl << "Total amount for account # " << (num + 1) << " is: " << accounts[num].accountBal << endl ;
total += accounts[num].accountBal;
cout << endl << "The total amount of all accounts is: " << total << endl;
}
}
average = total / NUM_OF_ACCOUNTS;
cout << "Average of all the bank accounts is: " << average << endl;
}
void BankAccount :: displayAccounts(BankAccount *accounts)
{
int input = 0;
int found;
const int MAX_ACCOUNTS = 100;
int quit = 0;
cout << endl << "Which account do you want to access?" << endl <<
"To stop or look at none of the account numbers type: " << quit << endl;
cin >> input;
for(int num = 0; num < MAX_ACCOUNTS; num++)
{
while(num < MAX_ACCOUNTS && input != accounts[num].accountNum)
{
num++;
}
if(input == accounts[num].accountNum) // This if sees if an account matches what the user entered.
{
cout << "Account: " << accounts[num].accountNum << endl << "Balance is: " <<
accounts[num].accountBal << endl << "Interest rate is: " << accounts[num].annualInterest;
cout << endl << "Enter another account number or type 0 to quit." << endl;
found = 1;
cout << endl;
cin >> input;
}
if(found == 0)
{
cout << "Sorry that account doesn't exist. Enter another account number." << endl;
cin >> input;
}
}
}
In C++, classes and structs are exactly the same constructs. They are, in fact, one thing — a User-Defined Type.
There is a different that is invoked depending on whether you used the keyword struct or class to define your UDT, and that is that class-key defaults to private member access and private inheritance, whereas struct-key defaults to both being public.
Other than this syntax difference, you can use either without worrying about one being "simpler" than the other.
Anyway, your compiler error (please provide it next time) is probably due to a declaration/definition mismatch.
Your declaration:
void displayAccounts(BankAccount *accounts, const int QUIT);
Start of your definition:
void BankAccount :: displayAccounts(BankAccount *accounts) {
The start of the definition should be
void BankAccount::displayAccounts(BankAccount* accounts, const int QUIT) {
to match. I've also fixed your spacing to be nicer. :)
void displayAccounts(BankAccount *accounts, const int QUIT);
... looks different between declaration and definition. Second parameter is missing in the definition.
Not sure what your question is, but classes and structs in C++ are equivalent except that fields are public by default in structs, but private by default in classes.
In the struct’s displayAccounts() member declaration you have:
void displayAccounts(BankAccount *accounts, const int QUIT);
and when defining the method later:
void BankAccount :: displayAccounts(BankAccount *accounts)
You have just missed
const int QUIT
parameter for the member function definition.