Could use some help (Assignment related)

  • Context: MHB 
  • Thread starter Thread starter WarGhSt
  • Start date Start date
Click For Summary
SUMMARY

The discussion focuses on a C++ program designed to calculate the total number of swimming skills a student has based on their highest completed swim level. The program utilizes a while loop to validate user input for swim levels (1, 101, 201, 301, 401, 501, 601) and computes the total skills using a switch statement. Key constants for skill levels are defined, and the program outputs the total skills for the specified swim level. The user is prompted for a student name, and the program terminates when a zero is entered.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with control flow statements (if, while, switch)
  • Knowledge of variable declaration and initialization in C++
  • Basic understanding of user input handling in C++
NEXT STEPS
  • Implement error handling for invalid user inputs in C++
  • Explore C++ arrays for managing multiple swim levels
  • Learn about functions in C++ to modularize the skill calculation logic
  • Research best practices for user input validation in console applications
USEFUL FOR

C++ beginners, educators teaching programming concepts, and developers interested in user input validation and control flow in console applications.

WarGhSt
Messages
15
Reaction score
0
2nejeVa.png


Code:
#include <iostream>
#include <string>

using namespace std;

int main(){
//    int i = 0;         // Loop counter iterates numRolls times
//    int j = 0;       // loop counter for histogram
    string studentName;  // User defined number of rolls
    int swimLevel = 0;  // Tracks number of 6s found
//    int const classOne = 8; // Entry level Class 8 Skills
//    int const classTwo = 9;      // Class 101 9 Skills
//    int const classThree = 8;      // Class 201 8 Skills
//    int const classFour = 6; // Class 301 6 Skills
//    int const classFive = 7;   // Class 401 7 Skills
//    int const classSix = 7;   // Class 501 7 Skills
//    int const classSeven  = 7;   // Class 601 7 Skills
//    int classUnknown = 10;    cout << "Please enter the name of the student:" << endl;
    cin >> studentName;

    // DEBUG COUT
    //    cout << "Printing student name: " << studentName << "." << endl;

    cout << "Please enter your students highest swim level completed: 1, 101, 201, 301, 401, 501, 601 or '0' to quit" << endl;
    cin >> swimLevel;

    while (swimLevel != 0) {
//    while ((swimLevel == 1) || (swimLevel == 101) || (swimLevel == 201) || (swimLevel == 301) || (swimLevel == 401) || (swimLevel == 501) || (swimLevel == 601)) {

        // DEBUG COUT
    //    cout << "Printing swim level: " << swimLevel << "." << endl;

//        for (i = 0; i < 1; ++i) {
                if (swimLevel == 1) {
                    swimLevel = swimLevel + 1;
                    cout << studentName << " has a total of 0 skills up to level 1!" << endl;
                }
                else if (swimLevel == 101) {
                    swimLevel = swimLevel + 1;
                    cout << studentName << " has a total of 8 skills up to level 101!" << endl;
                }
                else if (swimLevel == 201) {
                    swimLevel = swimLevel + 1;
                    cout << studentName << " has a total of 17 skills up to level 201!" << endl;
                }
                else if (swimLevel == 301) {
                    swimLevel = swimLevel + 1;
                    cout << studentName << " has a total of 25 skills up to level 301!" << endl;
                }
                else if (swimLevel == 401) {
                    swimLevel = swimLevel + 1;
                    cout << studentName << " has a total of 31 skills up to level 401!" << endl;
                }
                else if (swimLevel == 501) {
                    swimLevel = swimLevel +1;
                    cout << studentName << " has a total of 38 skills up to level 501!" << endl;
                }
                else if (swimLevel == 601) {
                    swimLevel = swimLevel +1;
                    cout << studentName << " has a total of 45 skills up to level 601!" << endl;
                }
                else if ((swimLevel != 1) || (swimLevel != 101) || (swimLevel != 201) || (swimLevel != 301) || (swimLevel != 401) || (swimLevel != 501) || (swimLevel != 601))
                      cout << "Remember 1, 101, 201, 301, 401, 501, 601, or 0 to quit!" << endl;
                      cin >> swimLevel;
}
}

My code displays the final result without fail, but does not use the steps for bullet-point 3 & 4 Your program logic must calculate the total number of skill sets inside a loop. For example, if the skill level = 101, then the total will start at 0, then 9 would be added for level 101 and then the 8 would be added in for level 1. If the highest skill level = 201, then the total will start at 0, then 8 would be added for level 201, then 9 would be added for level 101 and then the 8 would be added for level 1 would get added.
The sentinel (or stopping condition of your loop) must be when the skill level goes below 0.


The assignment is already late, so I'm taking a grade penalty there: I'd love to pull off full marks otherwise, so I'm opening my code up to criticism in order to get some help. Also, it should be pointed out that this is the assignment right before learning arrays, so it must be done with while/for loops.
 
Technology news on Phys.org
Copy pasted some of the variable definitions from another program at one point - that's why the comments following them don't match up with what the program is doing. Lol
 
Okay, I would begin by defining the needed constants and variables:

Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
	const int Level1 = 8;
	const int Level101 = 9;
	const int Level201 = 8;
	const int Level301 = 6;
	const int Level401 = 7;
	const int Level501 = 7;
	const int Level601 = 7;
	string studentName;
	int swimLevel;
	int quit = 0;

	return 0;
}

Okay, now we are going to want a while loop that executes as long as [M]!quit[/M] (we will set [M]quit = 1[/M] when a zero is input for the swim level), and within this loop we will first ask for the swim level, because it makes no sense to first ask for a name when the user is going to terminate the program by entering a zero, so let's add that:

Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
	const int Level1 = 8;
	const int Level101 = 9;
	const int Level201 = 8;
	const int Level301 = 6;
	const int Level401 = 7;
	const int Level501 = 7;
	const int Level601 = 7;
	string studentName;
	int swimLevel;
	int swimLevelValid;
	int quit = 0;

	while (!quit)
	{
		swimLevelValid = 0;
		while (!swimLevelValid)
		{
			cout << "Please enter your students highest swim level completed: 1, 101, 201, 301, 401, 501, 601 or '0' to quit" << endl;
			cin >> swimLevel;
			if (swimLevel == 0 || swimLevel == 1 || swimLevel == 101 || swimLevel == 201 || swimLevel == 301 || swimLevel == 401 || swimLevel == 501 || swimLevel == 601)
			{
				swimLevelValid = 1;
			}
			else
			{
				cout << "Invalid swim level completed!" << endl;
			}
		}
		if (!swimLevel)
		{
			quit = 1;
		}
		else
		{
		}
	}

	return 0;
}

Okay, now we have ensured a valid number is entered, and the program will terminate if a zero is entered, so within the else clause we now need to prompt for the student name, and then begin calculating the total number of skills learned based on the input [M]swmiLevel[/M]. Are you with me so far? :)
 
Yes, I'm following.
 
Okay, the next thing we need to do is prompt the user for the student's name, compute the total skills, and print the results:

Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
	const int Level1 = 8;
	const int Level101 = 9;
	const int Level201 = 8;
	const int Level301 = 6;
	const int Level401 = 7;
	const int Level501 = 7;
	const int Level601 = 7;
	string studentName;
	int swimLevel;
	int swimLevelValid;
	int quit = 0;
	int i;
	int totalSkills;

	while (!quit)
	{
		swimLevelValid = 0;
		totalSkills = 0;
		while (!swimLevelValid)
		{
			cout << "Please enter your students highest swim level completed: 1, 101, 201, 301, 401, 501, 601 or '0' to quit" << endl;
			cin >> swimLevel;
			if (swimLevel == 0 || swimLevel == 1 || swimLevel == 101 || swimLevel == 201 || swimLevel == 301 || swimLevel == 401 || swimLevel == 501 || swimLevel == 601)
			{
				swimLevelValid = 1;
			}
			else
			{
				cout << "Invalid swim level completed!" << endl;
			}
		}
		if (!swimLevel)
		{
			quit = 1;
		}
		else
		{
			cout << "Please enter the name of the student:" << endl;
			cin >> studentName;
			for (i = (swimLevel - 1)/100; i >= 0; i--)
			{
				switch (i)
				{
					case 6:
						totalSkills += Level601;
						break;
					case 5:
						totalSkills += Level501;
						break;
					case 4:
						totalSkills += Level401;
						break;
					case 3:
						totalSkills += Level301;
						break;
					case 2:
						totalSkills += Level201;
						break;
					case 1:
						totalSkills += Level101;
						break;
					case 0:
						totalSkills += Level1;
						break;
				}
			}
			cout << studentName << " has a total of " << totalSkills << " skills up to level " << swimLevel << "!" << endl;
		}
	}

	return 0;
}
 
Going backwards, I can see exactly how all this fits together. Coming up with it on my own, however, is still a long way out!

Thank you very much for the time you spent helping me out!
 

Similar threads

Replies
12
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K