My First (class) C++ program.

Posts 13 of 3 · Page 1 of 1
My First (class) C++ program.
This is just a simple program I made to use classes. I will include the code below. Any way I can improve this? Remember that I'm a beginner.

Code:
#include <iostream>
using namespace std;

    class Guns {
          public:
                int GetNum() {return Num;};
                int SetNum(int number){int temp;
                temp=number;
                Num=temp;};
          private:
                 int Num;
};
int main(void) {


Guns subMG;
Guns Sniper;
bool donE=false;
bool first=true;
do {
    
    char d;
    int NuMiO, NuMiO2;
    
cout << "---------------------------------------\n";
cout << subMG.GetNum() << " is stored in subMG." << endl;
cout << Sniper.GetNum() << " is stored in Sniper." << endl;
cout << "Fill in subMG: ";
cin >> NuMiO;
cout << "Fill in Sniper: ";
cin >> NuMiO2;
subMG.SetNum(NuMiO);
Sniper.SetNum(NuMiO2);
if(first != true) {
cout << "Continue? (y-n): ";
cin >> d;
};
first=false;
if (d=='n') {donE=true;};
}while(donE != true);




return 0;
}
The class is fine. My one nitpick is why you put the Continue question at the bottom of the loop instead of the top. That way you can jump out of the loop before asking all those questions again. Just use break; to break out of the loop and return whenever b == 'n'
IDK. I'm still new to this and I couldn't think of another way. But thanks for the idea, I'll try to improve.
Posts 13 of 3 · Page 1 of 1

Post a Reply

Tags for this Thread

None

Need help?