Hello basel07,
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
|
//#define RELEASE // <--- Uncomment when compiled for release.
#include <iostream>
#include <string>
#include <array>
#ifndef RELEASE // <--- Used for writing and testing program.
#include <conio.h> // <--- Used for _getch(). You could do something different. Also used for writing and testing program.
#endif // !RELEASE
void GetNames(std::string& name1, std::string& name2);
int main()
{
constexpr size_t MAXSIZE{ 3 }; // <--- Only need to change one number to affect three places.
std::array<size_t, MAXSIZE> student1{ 90, 85, 95 }; // <--- Leave empty for run
std::array<size_t, MAXSIZE> student2{ 89, 87, 94 }; // <--- Leave empty for run
std::string name1{ "Andy Gelpi" }, name2{ "Bob Smithe" }; // <--- Leave empty for run.
size_t studentPoints1{}, studentPoints2{};
//GetNames(name1, name2); // <--- Uncomment for run.
for (size_t lc = 0; lc < MAXSIZE; lc++)
{
std::cout << "Enter Lab " << lc + 1 << " grade For " << name1 << ": "; // <--- Changed prompt.
std::cin >> student1[lc];
std::cout << "Enter lab " << lc + 1 << " grade For " << name2 << ": "; // <--- Changed prompt.
std::cin >> student2[lc];
std::cout << std::endl;
if (student1[lc] < student2[lc])
studentPoints2++;
else if (student1[lc] > student2[lc])
studentPoints1++;
}
std::cout << "\n " << name1 << " has " << studentPoints1 << " point(s)." << "\n " << name2 << " has "
<< studentPoints2 << " point(s)." << std::endl;
#ifndef RELEASE // <--- Used for writing and testing program.
std::cout << "\n\n\n\n Press anykey to continue";
_getch();
#endif // !RELEASE
return 0;
} //End main
void GetNames(std::string& name1, std::string& name2)
{
std::cout << "\n Enter the name of student 1: ";
std::getline(std::cin, name1);
std::cout << "\n Enter the name of student 2: ";
std::getline(std::cin, name2);
}
|
Above main is a prototype of a function.This is not necessary, but the concept will come in handy in the future.If you are not to functions yet what is between the {} of the function can be placed inside main.
The beginning of main is different than yours. It is better to define each variable on one line. this makes things easier to find and to see what you are doing. Line 17 creates the variable "MAXSIZE" which is used in three places. this way you only have to change one number without changing that number everywhere in your program.
Lines 18 an 19 I used a std::array, but a C - style array will work just fine.For working with the program I initialized the array so I would not have to enter numbers each time the program runs.Use this while working with the program and leave the{} blank when you are finished.
The same concept applies to line 20.
The "size_t" is another name for an "unsigned int". I use this when the numbers that will be used are positive and small. Numbers that an "unsigned int" will hold.
The for loop is much the same as what you did although I changed the "cout" statements. Call it my quirk, but I like the way the output looks. The extra spaces read better and the name makes it more personal.
Thinking about the for loop, it works fine the way it is, but I would eigher break it up into two for loops, e.g., one to get the input and one to check the arrays. Or I would probably put each for loop in a function and use main to guide the program flow.
Lastly notice the variable names I used. You might use something similar for the variable names. The idea is that it makes the program easier to read now and especially in six month when come back to see what you did.
Hope that helps,
Andy