Opinions on Efficiency of Code

Hello all, I hope I'm posting this in the right forum.

I also hope you don't mind my posting of over 100 lines of code.

I'm just asking for your opinions on how well (or not well) written this particular program is. What it does is it asks for user input of a population size, number of years, and rate of growth and then calculates the estimated population size after the inputted number of years. The program also checks for bad input.

This isn't a homework assignment or anything, just me messing around and seeing what I can create.

I'm particularly new to programming in general, and don't know that much about C++, which is the first language I'm learning.

A few lines of this code I found browsing this forum, such as anything needing the #include <limits/string/windows.h> libraries or the use of a class to keep the console open and running (which I'm still not sure how it works, but I'll do my own research).

In particular, I used a system("CLS"); and I read that doing so is a bad idea, but all other ways I found to clear the screen were either silly or about 40 lines of code that I couldn't get to work. So I gave up and just took the easy way out, since I'm just experimenting by myself with no intention of releasing.

I also feel like while checking for bad input I didn't need to copy and paste the same blocks of code multiple times and rename the variables, is there some better way to do this?

All suggestions/opinions are appreciated.

Thanks!

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <limits>
#include <windows.h>
#include <string>

using namespace std;

class KeepRunning {
  public:
    ~KeepRunning() {
    	
    cout << "This is a program that will calculate a population after a certain\nnumber of years at a certain rate of growth.\nEnter a value of [-999] at any time to end the program.\n" << endl;
	
//\n's just to manually wrap text in my console.

	int nbr = 1;
	double pop1, pop2, rate, rateorig;
	int years;
	
//I'm sure there's a better way to keep this while loop running
//other than using "int nbr = 1".

	while (nbr = 1) {
		
		do {
		cout << "\nPlease enter the population: ";
		
			
		while(!(cin >> pop1)){
			cin.clear();
			cin.clear();
    	                cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
			cout << "\n\nPlease enter in a valid number for the population (non-negative integer)." << endl;
			cout << "\n\nPlease enter the population: ";
			
			}
			
			if (pop1 == -999) {
			cout << "\n\nSee you next time!\n" << endl;
			cout << "Press ENTER to continue...";
			cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
			cin.get();
			exit(0);	
			}
		
			else if (pop1 != int(pop1) || pop1 < 0) {
				cout << "\n\nPlease enter in a valid number for the population (non-negative integer).\n" << endl;
			}
			
		
		} while (pop1 != int(pop1) || pop1 < 0);
		
		do {
			
			cout << "\nPlease enter the number of years: ";
			
			while(!(cin >> years)){
			cin.clear();
			cin.clear();
    	        	cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
			cout << "\n\nPlease enter in a valid number for the number of years (non-negative integer).";
			cout << "\n\nPlease enter the number of years: ";
			}
			
			if (years == -999) {
			cout << "\n\nSee you next time!\n" << endl;
			cout << "Press ENTER to continue...";
			cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
			cin.get();
			exit(0);	
			}
			
			else if (years != int(years) || years < 0) {
				cout << "\n\nPlease enter in a valid number for the number of years (non-negative integer).\n";
			}
			
		} while (years != int(years) || years < 0);	
			
		do {	
		
		cout << "\nPlease enter the percent rate of growth: ";
			
		while(!(cin >> rate)){
			
		cin.clear();
   	        cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		cout << "\n\nPlease enter in a valid number for the rate of growth (some real number).";
		cout << "\n\nPlease enter the percent rate of growth: ";
		
		}
		
		if (rate == -999) {
			cout << "\n\nSee you next time!\n" << endl;
			cout << "Press ENTER to continue...";
			cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
			cin.get();
			exit(0);	
			}
		
		} while (rate == -999);
		
		pop2 = pop1;
		rateorig = rate;
		rate = rate / 100;
		
		for (int ind = 1 ; ind <= years ; ind++) {
			pop1 += pop1 * rate;
		}
		
		cout << setiosflags (ios::fixed) << setprecision (0);
		cout << "\nThe population of " << pop2 << " after " << years << " years " << " at a rate of growth of " << rateorig << "% will be " << pop1 << endl;
		cout << "\n\ndone\n\n\n\n";
		cout << "\nPress ENTER to continue...\n";
                cin.get();
		cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
		
        //This is me trying to avoid the use of system ("pause");
		cout << "\n\n";
		system ("CLS"); // This line bothers me because
                                // I heard it's bad practice to
                                // use system calls.

		cout << "This is a program that will calculate a population after a certain\nnumber of years at a certain rate of growth.\nEnter a value of [-999] at any time to end the program.\n" << endl;

       //Repeating this cout from the beginning made the
       //program work like I wanted it too but it seems
       //like there should be a better way.

  	 	}
    }
};

int main(  )
{
	KeepRunning kr;	
}
Last edited on
That's, a very interesting and unique way to make your program loop forever. I have never seen that done before. Well while (nbr = 1) isn't, but I've never seen someone put their program inside the destructor of a class. btw, while ( true ) does the same thing and doesn't need its own variable.

One thing I noticed is that you use cin.clear() twice in a row a lot. cin.clear() clears the error flags from cin. Calling it a second time immediately afterwards does nothing.
Last edited on
I saw somewhere that that would be one way to make the program loop, and make the use of system calls less "bad".

Thanks, I'll get rid of int nbr = 1 and just put true in the while loop, I'll also get rid of those extra cin.clear()'s.

Also, the way I have it set up now, after a successful calculation and cout of the answer, the user is prompted to hit enter, but what if the user tries to kill the program then by typing -999? Right now that and anything else the user typed would get flushed out and the program would restart. I tried to use if and else statements again but I couldn't get -999 to terminate the program if the user tried to enter anything after that prompt to hit enter. I mean I did say "at any time"
Last edited on
Topic archived. No new replies allowed.