Debugging I/O Assignment: Structure Logic & Solve Problems

  • Context:
  • Thread starter Thread starter WarGhSt
  • Start date Start date
  • Tags Tags
    Assignment
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 2K views
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: 151
Physics 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.