Debugging I/O Assignment: Structure Logic & Solve Problems

  • Context: MHB 
  • Thread starter Thread starter WarGhSt
  • Start date Start date
  • Tags Tags
    Assignment
Click For Summary
SUMMARY

The discussion centers on debugging an I/O assignment involving a C++ program that manages membership services. The user is encountering logic errors related to displaying services based on membership types and linking discounts to these memberships. The program reads from a "services.txt" file, but the implementation for displaying services and managing service values requires refinement. Key issues include improper handling of service values and the need for better data management in the displayServices function.

PREREQUISITES
  • Understanding of C++ programming, specifically functions and file I/O.
  • Familiarity with conditional statements and switch-case constructs in C++.
  • Basic knowledge of data structures for managing service information.
  • Experience with debugging techniques in C++ to resolve logic errors.
NEXT STEPS
  • Refactor the displayServices function to improve service value management and streamline output.
  • Implement a method to link membership types to discount values effectively.
  • Explore the use of arrays or vectors to store service names and values for better data handling.
  • Learn about the srand function in C++ for generating random discounts and how to integrate it into the program.
USEFUL FOR

Students and developers working on C++ I/O assignments, particularly those focusing on file handling and membership management systems. This discussion is beneficial for anyone looking to enhance their debugging skills and improve logical structuring in programming.

WarGhSt
Messages
15
Reaction score
0
Working on an I/O assignment and I'm running into some logic errors. I was wondering if I could get a quick break down (not necessarily code, but the pseudo code behind some of these points I'm supposed to be working on. That way I don't get spoiled with the answers, but have enough to go on compared to what I'm doing right now which is banging my head against a very solid wall.

Running this with multiple functions as I was advised to try in my last program (Didn't end up using it there, but I'm running with it now)

View attachment 5469

services.txt
Code:
Cafe:Smoothies
SPA:Hair and Skin
Training:90 day challenge
Aquatics:Kids free training session

code for my program
Code:
#include <iostream>
#include <fstream>
#include <time.h>
using namespace std;

int getMenuChoice();
void displayServices(int x);
int discountValue(int y);

//main function
int main(){
    int whatTheyAre;

    whatTheyAre = getMenuChoice();

    while(whatTheyAre != 4){
        switch(whatTheyAre){
            case 1:
                cout << "You are paying for the individuals membership, which is $500." << endl;
                cout << "And you are entitled to the following services: " << endl;
                displayServices(1);
                break;
            case 2:
                cout << "You are paying for the couples membership, which is $800." << endl;
                cout << "And you are entitled to the following services: " << endl;
                displayServices(2);
                break;
            case 3:
                cout << "You are paying for the family membership, which is $1200." << endl;
                cout << "And you are entitled to the following services: " << endl;
                displayServices(3);
                break;
        }
    //another chance to choose a new choice
       whatTheyAre = getMenuChoice();
    }

}

//getWhatTheyWant function
int getMenuChoice(){
    int menuChoice;

    cout << "Enter 1 - If you are paying for an individual membership" << endl;
    cout << "Enter 2 - If you are paying for a couples membership" << endl;
    cout << "Enter 3 - If you are paying for a family membership" << endl;
    cout << "Enter 4 - To quit program" << endl;

    cin >> menuChoice;
    return menuChoice;
}

//display Services function
void displayServices(int x){

    ifstream objectFile("services.txt");
    string name;
    int serviceValue;

    if(x == 1){
        while(objectFile >> name >> serviceValue){
            if(serviceValue == 1){
                cout << name << endl;
            }
        }
    }

    if(x == 2){
        while(objectFile >> name >> serviceValue){
            if(serviceValue == 1){
                cout << name << endl;
            }
        }
    }

    if(x == 3){
        while(objectFile >> name >> serviceValue){
            if(serviceValue <= 2){
                cout << name << endl;
            }
        }
    }
}

I've modified my services.txt to carry the serviceValue of 1 for everything except swimming which has a value of 3 so that the display when the membership is Family it will return the kids swimming option but it's a really awful way of doing things. And won't work for the final program.

After that task, I'm having a hard time linking the membership value (individual, couples, family) to the discounts. Is it possible to get that done with the help of one of the three functions I have now?

Finally, and I'm sure this task will be easy once I've solved the problem above, the srand to apply a random discount to the gym member.
 

Attachments

  • Assignment2.png
    Assignment2.png
    7.4 KB · Views: 127
Technology news on Phys.org
Since this is turning out to be like my last post and garnering few if any replies, I'll try breaking it down to what I'm currently working on again.

In my input from file function I would like to take the line from each service and store the valuable as a string (separate string for each). How might I do that? Right now it stores the names of every service as one string or each new space causes it to be considered as a new name.
 
Code:
void displayServices(int x){
    ifstream objectFile("services.txt");
    string service1;
    string service2;
    string service3;
    string service4;
    int serviceValue;

    if (objectFile.is_open())
    {
        while (!objectFile.eof())
        {
            getline(objectFile, service1, '\n');
            cout << service1 << endl;
            getline(objectFile, service2, '\n');
            cout << service2 << endl;
            getline(objectFile, service3, '\n');
            cout << service3 << endl;
            getline(objectFile, service4, '\n');
            cout << service4 << endl;
        }
        objectFile.close();
    }
    else cout << "Unable to open file";
}

Is storing them, but every time I "solve" one problem I'm met by another. I'd like to use each of the service strings I've stored in my first function from my first post now to display the services available for the memberships.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
10
Views
2K
  • · Replies 15 ·
Replies
15
Views
4K