Results 1 to 3 of 3
  1. #1
    kensclark13's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Posts
    10
    Reputation
    8
    Thanks
    0
    My Mood
    Angelic

    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;
    }

  2. #2
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    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'

    "Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."
    - Dwight D. Eisenhower

  3. #3
    kensclark13's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Posts
    10
    Reputation
    8
    Thanks
    0
    My Mood
    Angelic
    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.