Machine Problem for school activity

Write your question here.
Greetings!
I am student taking up a course in c++ programming, and i have an activity where in i need to solve this problem :

Write a C++ program that will ask for the following input from the user:
Student Name:
Program/Course:
Year Level: (should be integer type)
Number of Units Enrolled: (should be double or float data type)
Determine the corresponding year name of the student and rate per unit as follows:
Year Level Year Name Rate Per Unit
1 Freshman 1,500
2 Sophomore 1,800
3 Junior 2,000
4 or 5 Senior 2,300
Compute the tuition fee as follows:
Tuition fee = no. of units enrolled X rate per unit
Down payment = 30% of the tuition fee
Balance = tuition fee – down payment
Assume that the user will not enter an invalid value.

Sample Input:
Student Name : Maria Blanco
Program/Course : Bachelor of Science in Information Technology
Year Level : 4
No. of Units : 16
--------------------------------------------------------------------------------------------

Sample Output:
ENROLLMENT SLIP
Student Name : Maria Blanco
Program/Course : Bachelor of Science in Information Technology
Year Name : Senior
No. of Units : 16
Tuition Fee : 36800
Down Payment : 11040
Balance : 25760


Thanks All!
You should post what you have so far and ask a question if you have one.
Hello angeles1migs,

Welcome to the forum.

If your post is to say how do I get started? Then I would suggest breaking up the instructions:

Write a C++ program that will ask for the following input from the user:

Student Name:

Program/Course:

Year Level: (should be integer type)

Number of Units Enrolled: (should be double or float data type) double is preferred type.

Determine the corresponding year name of the student and rate per unit as follows:

Year Level Year Name Rate Per Unit
1 Freshman 1,500
2 Sophomore 1,800
3 Junior 2,000
4 or 5 Senior 2,300

Compute the tuition fee as follows:

Tuition fee = no. of units enrolled X rate per unit

Down payment = 30% of the tuition fee

Balance = tuition fee – down payment

Assume that the user will not enter an invalid value.

By breaking up the instructions yo see smaller parts to work on. Do the program in pieces and not all at once you will find it easier to work with.

I would start with defining the variables that I could followed by writing the code to get the user input. If you know about functions I would put this in a function.

The program could be written to work with one input at a time and save some information for later or you might consider creating a struct to hold all the information and put this into an array or vector for later use. This could also be useful for future modification of the program.

As SamuelAdams said work up some code and post it with your question. Please include any sample input that you use so everyone will have the same results.

Hope that helps,

Andy
Hello Handy Andy!

Thanks for your reply, it exhausted my brain power haha! as i am not really good at this thing, I'm currently taking a bachelors degree in information technology and it's my first to take this course, but I was finally able to figure it out. using the code below :


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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
string StName,ProgCourse;

    int YrLevel;
    double UnitEn;

        cout<<"Student Name     : ";
        getline(cin,StName);

        cout<<"Program/Course   : ";
        getline(cin,ProgCourse);

        cout<<"Year Level       : ";
        cin>>YrLevel;

        cout<<"Number of Units  : ";
        cin>>UnitEn;

        cout<<"\n";

        if(YrLevel == 1)
            {
                int fresh = 1500;
                int TF = UnitEn*fresh;
                double DP = TF*.30;
                int Bal = TF-DP;

                cout<<"\n";
                cout<<"***ENROLLMENT SLIP***";
                cout<<"\n";
                cout<<"\n";

                cout<<"Student Name     : "<<StName;
                cout<<"\n";
                cout<<"Program/Course   : "<<ProgCourse;
                cout<<"\n";
                cout<<"Year Name        : Freshman";
                cout<<"\n";
                cout<<"Number of Units  : "<<UnitEn;
                cout<<"\n";
                cout<<"Tuitio Fee       : "<<TF;
                cout<<"\n";
                cout<<"Downpayment      : "<<DP;
                cout<<"\n";
                cout<<"Balance          : "<<Bal;
                cout<<"\n";
            }
        else if (YrLevel == 2)
            {
                int sophomore = 1800;
                int TF = UnitEn*sophomore;
                double DP = TF*.30;
                int Bal = TF-DP;

                cout<<"\n";
                cout<<"***ENROLLMENT SLIP***";
                cout<<"\n";
                cout<<"\n";

                cout<<"Student Name     : "<<StName;
                cout<<"\n";
                cout<<"Program/Course   : "<<ProgCourse;
                cout<<"\n";
                cout<<"Year Name        : Sophomore";
                cout<<"\n";
                cout<<"Number of Units  : "<<UnitEn;
                cout<<"\n";
                cout<<"Tuitio Fee       : "<<TF;
                cout<<"\n";
                cout<<"Downpayment      : "<<DP;
                cout<<"\n";
                cout<<"Balance          : "<<Bal;
                cout<<"\n";  
            }
        else if (YrLevel == 3)
            {
                int junior = 2000;
                int TF = UnitEn*junior;
                double DP = TF*.30;
                int Bal = TF-DP;

                cout<<"\n";
                cout<<"***ENROLLMENT SLIP***";
                cout<<"\n";
                cout<<"\n";

                cout<<"Student Name     : "<<StName;
                cout<<"\n";
                cout<<"Program/Course   : "<<ProgCourse;
                cout<<"\n";
                cout<<"Year Name        : Junior";
                cout<<"\n";
                cout<<"Number of Units  : "<<UnitEn;
                cout<<"\n";
                cout<<"Tuitio Fee       : "<<TF;
                cout<<"\n";
                cout<<"Downpayment      : "<<DP;
                cout<<"\n";
                cout<<"Balance          : "<<Bal;
                cout<<"\n";
            }
        else if (YrLevel == 4)
            {
                int senior = 2300;
                int TF = UnitEn*senior;
                double DP = TF*.30;
                int Bal = TF-DP;

                cout<<"\n";
                cout<<"***ENROLLMENT SLIP***";
                cout<<"\n";
                cout<<"\n";

                cout<<"Student Name     : "<<StName;
                cout<<"\n";
                cout<<"Program/Course   : "<<ProgCourse;
                cout<<"\n";
                cout<<"Year Name        : Senior";
                cout<<"\n";
                cout<<"Number of Units  : "<<UnitEn;
                cout<<"\n";
                cout<<"Tuitio Fee       : "<<TF;
                cout<<"\n";
                cout<<"Downpayment      : "<<DP;
                cout<<"\n";
                cout<<"Balance          : "<<Bal;
                cout<<"\n";
            }
        else if (YrLevel == 5)
            {
                int senior = 2300;
                int TF = UnitEn*senior;
                double DP = TF*.30;
                int Bal = TF-DP;

                cout<<"\n";
                cout<<"***ENROLLMENT SLIP***";
                cout<<"\n";
                cout<<"\n";

                cout<<"Student Name     : "<<StName;
                cout<<"\n";
                cout<<"Program/Course   : "<<ProgCourse;
                cout<<"\n";
                cout<<"Year Name        : Senior";
                cout<<"\n";
                cout<<"Number of Units  : "<<UnitEn;
                cout<<"\n";
                cout<<"Tuitio Fee       : "<<TF;
                cout<<"\n";
                cout<<"Downpayment      : "<<DP;
                cout<<"\n";
                cout<<"Balance          : "<<Bal;
                cout<<"\n";
            }       
Last edited on
Hello angeles1migs,

Sorry for the late response. Friday I was tied up all day and Saturday I lost internet for most of the day.

Some observations on your program:

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


It is not always necessary to initialize your variables when they are defined, but a good idea. An empty set of {}s is all that is needed form C++11 on.

In your if/else if statements you are defining the same variables five times. these variables only need to be defined once. The reason it works is because of "scope".

Also you are writing the same "cout' statements five times. This makes it a good candidate for a function passing the variables that you need from each if/else if statement.

A variable like int fresh = 1500; is better defined as a constant at the beginning of the program. This way you have only one place to look and change the value in the future.

I offer this as an alternative for now and in the future:

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <string>
#include <limits>

// <--- One place to find and change when needed.
constexpr int FRESH{ 1500 };
constexpr int SOPHOMORE{ 1800 };
constexpr int JUNIOR{ 2000 };
constexpr int SENIOR{ 2300 };
constexpr double DPRATE{ 0.30 };

// <--- This could be done in the if/else if statements with a constant string like: "Year Name : Freshman" in the function call.
const std::string YEARNAMEF{ "Year Name : Freshman" };
const std::string YEARNAMES{ "Year Name : Sophomore" };
const std::string YEARNAMEJ{ "Year Name : Junior" };
const std::string YEARNAMESR{ "Year Name : Senior" };

// <--- Prototypes for the functions.
void GetInput(std::string& stName, std::string& progCourse, int& yrLevel, double& unitEn);
void DisplayResults(const std::string stName,
					const std::string progCourse,
					const std::string yearName,
					const double unitEn,
					const int tf,
					const double dp,
					const double bal);

int main()
{
	std::string stName, progCourse;

	int yrLevel{};  // <--- Initializes the variable to zero.
	double unitEn{};  //<--- Initializes the variable to 0.0.
	// <--- These only need to be defined once.
	int tf{};
	double dp{};
	int bal{};

	GetInput(stName, progCourse, yrLevel, unitEn);
	
	std::cout << "\n";

	if (yrLevel == 1)
	{
		// <--- Notice the use of "FREASH" and "DPRATE".
		tf = unitEn * FRESH;
		dp = tf * DPRATE;
		int bal = tf - dp;

		DisplayResults(stName, progCourse, YEARNAMEF, unitEn, tf, dp, bal);
		// <--- Or
		//DisplayResults(stName, progCourse, "Year Name : Freshman", unitEn, tf, dp, bal);

		// <--- Notice the constant string in place of "YEARNAMEF"
	}
	else if (yrLevel == 2)
	{
		// <--- Do not need to redefine these next 3 lines. Only needs defined once.
		tf = unitEn * SOPHOMORE;
		dp = tf * DPRATE;
		bal = tf - dp;

		DisplayResults(stName, progCourse, YEARNAMES, unitEn, tf, dp, bal);
		std::cout << "\n";
	}
	else if (yrLevel == 3)
	{
		tf = unitEn * JUNIOR;
		dp = tf  * DPRATE;
		bal = tf - dp;

		DisplayResults(stName, progCourse, YEARNAMEJ, unitEn, tf, dp, bal);

		std::cout << "\n";
	}
	else if (yrLevel == 4)
	{
		tf = unitEn * SENIOR;
		dp = tf * DPRATE;
		bal = tf - dp;

		DisplayResults(stName, progCourse, YEARNAMESR, unitEn, tf, dp, bal);

		std::cout << "\n";
	}
	else if (yrLevel == 5)
	{
		tf = unitEn * SENIOR;
		dp = tf * DPRATE;
		bal = tf - dp;

		DisplayResults(stName, progCourse, YEARNAMESR, unitEn, tf, dp, bal);

		std::cout << "\n";
	}

	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue";
	std::cin.get();

	return 0;
}

void GetInput(std::string& stName, std::string& progCourse, int& yrLevel, double& unitEn)
{
	std::cout << "Student Name : ";
	std::getline(std::cin, stName);

	std::cout << "Program/Course : ";
	std::getline(std::cin, progCourse);

	std::cout << "Year Level : ";
	// <--- You might consider:
	std::cout << "Year Level (Sophomore = 1): ";
	// <--- To show the user what need to be entered. Because some user may try to enter a string instead of a number.
	std::cin >> yrLevel;

	std::cout << "Number of Units : ";
	std::cin >> unitEn;
	// <--- Used after the last "cin" to clear the input buffer. And needed before the next "std::getline(...)".
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
}
void DisplayResults(const std::string stName,  // <--- All are "const" so they can not be changed.
					const std::string progCourse,
					const std::string yearName,
					const double unitEn,
					const int tf,
					const double dp,
					const double bal)
{
	std::cout << "\n";
	std::cout << "***ENROLLMENT SLIP***";
	std::cout << "\n";
	std::cout << "\n";

	std::cout << "Student Name : " << stName;
	std::cout << "\n";
	std::cout << "Program/Course : " << progCourse;
	std::cout << "\n";
	std::cout << yearName;  // <--- Changed. Acquired from the parameters in the function definition.
	std::cout << "\n";
	std::cout << "Number of Units : " << unitEn;
	std::cout << "\n";
	std::cout << "Tuitio Fee : " << tf;
	std::cout << "\n";
	std::cout << "Downpayment : " << dp;
	std::cout << "\n";
	std::cout << "balance : " << bal;
	std::cout << "\n";
}

Be sure to read the comments in the program. If you have any questions let me know.

The only thing I would have done different is to use a switch in place of the if/else if statements. Unless the point was to use if/else if statements.

Hope that helps,

Andy
hey Andy! Thank you so much for the response. I really appreciate it!
Hello angeles1migs,

You are welcome. If there is anything else I can do let me know.

Andy
Topic archived. No new replies allowed.