Need Help please

Suppose the membership at a fitness club is $500 for individual, $800 for couple and $1200 for 4 people. Every month, the manager at the club says that guests will get discounts for one service per month. He provides you with the list of services that will get a discount:

Your program will be reading in a file which contains a list of services at a fitness club. The discount is determined by the type of service. For example, the file is as follows:

Cafe: Smoothies

SPA: Hair and Skin

Training: 90 day challenge

Aquatics: Kids free training session

Your program should read this file and offer this discount:

If the service is Cafe, then discount is 1% of annual fitness club fee.

If the service is SPA, then discount is 10% of the annual fitness club fee.

If the service is Training, then discount is 15% of the annual fitness club fee.

If the service is Aquatics, then no discount, but program should only offer it for the family memberships.

Your program should ask the user what type of membership they have. Based on the type of membership, your program should calculate the dollar amount discount per service and list all applicable services and benefits for that user.

Your program should be reading the file in and calculating the benefits based on the type of membership. Your program should also randomly pick among the 4 discounts or 3 depending on the type of membership and display which discount the member gets for this month.

Here is the file: services.txtPreview the document

Create an algorithm.
Program and save as FitnessMembershipYourName.cpp. Make sure to include the algorithm and comment your code! Make sure you meet all the requirements specified above.
Show minimum 3 test cases as screenshots in a PDF file: FitnessMembershipYourName.pdf

I have gotten this but it won't go any further than the first user answer. I don't understand what's wrong.

service.txt:

Cafe:Smoothies

SPA:Hair and Skin

Training:90 day challenge

Aquatics:Kids free training session

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76


#include "pch.h"
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <math.h>
#include <fstream>
#include <sstream>
using namespace std;
int main() {

	string line;
	ifstream myfile("services.txt");



	cout << "Enter the type of membership you have: " << endl;
	cout << " 1-for Individual" << endl;
	cout << " 2-for Couple" << endl;
	cout << " 3-for 4 people" << endl;

	int amnt, enter;
	cin >> enter;

	if (enter == 1)
		amnt = 500;
	else if (enter == 2)
		amnt = 800;
	else if (enter == 3)
		amnt = 1200;

	if (myfile.is_open())
	{
		while (getline(myfile, line))
		{
			cout << line << '\n';

			string delimiter = ":";

			int pos = 0;
			string token;
			while ((pos = line.find(delimiter)) != string::npos) {
				token = line.substr(0, pos);
				cout << token << endl;
				line.erase(0, pos + delimiter.length());
			}

			if (token == "Cafe")
				cout << "Scheme(1):- Discounts: " << (amnt*0.01) << " Service: " << token << " Benefits: " << line << endl;
			else if (token == "SPA")
				cout << "Scheme(2):- Discounts: " << (amnt*0.10) << " Service: " << token << " Benefits: " << line << endl;
			else if (token == "Training")
				cout << "Scheme(3):- Discounts: " << (amnt*0.15) << " Service: " << token << " Benefits: " << line << endl;
			else if (token == "Aquatics" && amnt == 1200)
				cout << "Scheme(4):- Discounts: " << " No discount" << " Service: " << token << " Benefits: " << line << endl;
		}
		myfile.close();

		if (amnt != 1200) {
			srand(time(NULL));
			int r = rand() % 3 + 1;
			cout << "You are going to get Scheme Number " << r << " this month" << endl;
		}
		else {
			srand(time(NULL));
			int r = rand() % 4 + 1;
			cout << "You are going to get Scheme Number " << r << " this month" << endl;
		}
	}

	return 0;
}
Last edited on
closed account (E0p9LyTq)
Duplicate topic
http://www.cplusplus.com/forum/windows/243959/
Hello nikkismith,

As FurryGuy has pointed out pick a forum. Based on your code I would say this is the best forum to use.

Your program is opening a file to read. Where is the file? If the file is large then a fair sample will work, say five to ten lines, this way anyone who works on the program will be able to run it and everyone will be using the same information.

At the top of your program "#include "pch.h" should be left out when posting code here. I believe this is a VS 2017 file and not everyone can use it.

"string.h" is a C header file and you are not using any C code that would require this file.

"math.h" should be "cmath". "cmath" includes "math.h", but puts everything in "math.h" in the standard namespace.

You have included the header file for string stream, but never make use of it. Inside the while loop would be a good place for this.

The if statement at line 36 is nice, but you include the entire program inside the if statement, bad form. Also there is no else to go with the if statement. How do you know if the file did not open?

Something like this is a better way to write th if statement:
1
2
3
4
5
6
7
8
9
10
11
if (!myFile)
{
    std::cout << "An Error message" << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(5));  // Requires header files "chrono" and "thread"
    return 1;
}

    //  The rest of your program here.

    return 0;
}  //  End of "main". 

Here the "return" will exit the program and the one will tell you there is a problem. In the second line the only thing you have to worry about is the 5. This is the number of seconds the code will pause or wait for. And do not forget to include the header files.

With out the input file it makes it hard to test the program. For now a guess would be that the second while loop is not leaving the correct value in "token". This while loop would make a good place to use a string stream.

Hope that helps,

Andy
The file only has this:

service.txt:

Cafe:Smoothies

SPA:Hair and Skin

Training:90 day challenge

Aquatics:Kids free training session
Hello nikkismith,

Thank you. That is a start and a help.

Andy

P.S. Does it have the blank lines in the file?

Edit:
Last edited on
Topic archived. No new replies allowed.