Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7601281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:07:47+00:00 2026-05-30T23:07:47+00:00

I have a question about variable scope in a C++ class. The problem I’m

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T23:07:48+00:00Added an answer on May 30, 2026 at 11:07 pm
    double Drinks::input_money(int c) 
        // ^^^^^^^^ forgot this
    

    You forgot the class name while providing the implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about best practices for Ruby variable-scope using class_eval . In
I have a question about variable scope and memory management in C. I am
I had a question about Java Class Path variables. If I have multiple jars
This question is a follow-up on a question about Python variable scope . Additional
In this question I found an interesting detail about scope of a final variable
I have couple of questions about AS3 variables handling by AVM/compiler/scope .1. This code
I'm currently developing some things in Python and I have a question about variables
I have a question about ruby on rails and the process of assigning variables
I have question about NSView: Imagine a Custom View where the mouseDown, mouseDrag and
I have question about normalization. Suppose I have an applications dealing with songs. First

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.