MHB Could use some help (Assignment related)

  • Thread starter Thread starter WarGhSt
  • Start date Start date
AI Thread Summary
The discussion revolves around a programming assignment that requires calculating the total number of swimming skills based on the highest skill level achieved by a student. The user inputs the student's name and swim level, and the program should sum the skills from all levels up to the specified swim level. The code provided has issues with logic and structure, particularly in how it handles the calculation of skills and the loop conditions. Participants suggest improvements, including ensuring valid swim level inputs and implementing a loop to accumulate skills correctly. The assignment is already late, prompting the user to seek feedback for achieving full marks.
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!
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top