MHB Debugging I/O Assignment: Structure Logic & Solve Problems

  • Thread starter Thread starter WarGhSt
  • Start date Start date
  • Tags Tags
    Assignment
AI Thread Summary
The discussion revolves around resolving logic errors in an I/O assignment related to a membership program that utilizes multiple functions. The user seeks guidance on pseudo code to better understand the program's flow without receiving direct answers. The program reads from a "services.txt" file containing different services associated with membership types: individual, couples, and family. Key issues include linking membership types to corresponding discounts and effectively managing service values for display based on membership. The user has modified the service values in the text file but acknowledges that the current implementation is not optimal for the final program. They express difficulty in storing service names as separate strings and retrieving them correctly for display. The user is also interested in implementing a random discount feature using `srand`. They are looking for advice on how to structure their code to efficiently handle these tasks while overcoming the challenges they face with file input and data management.
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: 110
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top