Debugging I/O Assignment: Structure Logic & Solve Problems

  • MHB
  • Thread starter WarGhSt
  • Start date
  • Tags
    Assignment
In summary, the programmer is having difficulty solving logical problems and is running into errors while working on an I/O assignment. They were wondering if they could receive a quick summary of what they are supposed to be doing instead.
  • #1
WarGhSt
15
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: 57
Technology news on Phys.org
  • #2
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.
 
  • #3
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.
 

Related to Debugging I/O Assignment: Structure Logic & Solve Problems

1. What is debugging?

Debugging is the process of identifying and fixing errors or bugs in a computer program. It involves systematically finding and correcting errors in the code to ensure that the program runs smoothly and as intended.

2. Why is debugging important?

Debugging is important because it helps to ensure that a program is functioning correctly and producing the desired results. It also saves time and effort by identifying and fixing errors early on in the development process.

3. What is I/O assignment in debugging?

I/O assignment refers to the input and output (I/O) operations in a computer program. These operations involve receiving and processing data from external sources (input) and sending data back to the user or other devices (output). In debugging, I/O assignment is used to identify and fix errors related to data input and output.

4. How can structure logic help in debugging?

Structure logic involves organizing the code in a logical and systematic manner. This can help in debugging by making it easier to identify and isolate errors. With a well-structured code, it is also easier to track the flow of data and logic, making it easier to identify and fix errors.

5. What are some common problems that can occur during I/O assignment?

Some common problems that can occur during I/O assignment include incorrect or missing data input, faulty data processing, and errors in data output. These problems can lead to unexpected results or crashes in the program. It is important to thoroughly test and debug I/O assignments to ensure the program runs smoothly.

Similar threads

Replies
10
Views
977
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top