How is my first bit of code?.. *GASP*

Good Afternoon Everyone,

I would like to start by saying hello to everyone out there. I am a college student, father of 2, and have just put together my first console application. The application is called QuadraticCMD, I just started this today, and feel that I am gaining a lot of valuable information. The code works but if anybody would not mind critiquing my work, I would greatly appreciate it. I am ultimately looking to find if I should be doing something differently, if there is a better way to organize the code, or any other piece of advice you may have. I will be starting C++ Classes in the next year or so and would like to get on the right path before hand and it seems there are many Gurus here. Thanks in advance and I look forward to learning as much as possible.

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
 
 1 #include <iostream>
 2  #include <cmath>
 3  #include <iomanip>
 4  using namespace std;
 5  //Create Function
 6  int main()
 7  {
 8  //Program Information
 9      cout << " ----------------------------------------------------------------\n";
10      cout << "| + -_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+ -|\n";
11      cout << "| -|                                                           |+|\n";
12      cout << "| +|                Welcome to QuadraticCMD                    |-|\n";
13      cout << "| -|                   Made by \CodeKwik\                        |+|\n";
14      cout << "| +|                  Created on 12-7-13                       |-|\n";
15      cout << "| -| _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |+|\n";
16      cout << "| + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + -|\n";
17      cout << " ----------------------------------------------------------------\n\n";
18      cout << "-   This program will determine the discriminant (square root value)\n";
19      cout << "     and calculate WHOLE NUMBER x value(s) where y = 0\n";
20      cout << "     of a 2nd Degree Polynomial Function.\n\n\n";
21  //User Input Variable(s)
22      double a, b, c, discrim, dcomplex, value1, value2, vcomplex1, vcomplex2;
23  //Console Input(s)/Output(s)
24      cout.setf(ios::fixed);
25      cout.setf(ios::showpoint);
26      cout.precision(5);
27  
28      cout << "Please enter your /A/ value\n\n";
29      cin >> a;
30      cout << "Please enter your /B/ value\n\n";
31      cin >> b;
32      cout << "Please enter your /C/ value\n\n";
33      cin >> c;
34  //VALUES
35      discrim = (pow(b, 2.0) - (4 * a * c));
36      dcomplex = ((pow(b, 2.0) - (4 * a * c))) / -1;
37      value1 = ( (-b) + sqrt(discrim)) / (2 * a);
38      value2 = ( (-b) - sqrt(discrim)) / (2 * a);
39      vcomplex1 = ( (-b) + sqrt(dcomplex)) /(2 * a);
40      vcomplex2 = ( (-b) - sqrt(dcomplex)) /(2 * a);
41  //PRINT RESULTS
42      cout << "\n\n\n\n\n\n ----------------------------------------------------------------\n";
43      cout << "| + -_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+ -|\n";
44      cout << "| -|                                                           |+|\n";
45      cout << "| +|             -----RESULTS for the Equation:-----           |-|\n";
46      cout << "| -|                                                           |+|\n";
47      cout << "               (" << a << "x^2) + (" << b << "x) + (" << c << ")\n";
48      cout << "| -| _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |+|\n";
49      cout << "| + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + -|\n";
50      cout << " ----------------------------------------------------------------\n\n";
51  //Discriminant Rules
52  if (discrim == 0)
53      {
54      cout << "The discriminant is " << discrim << " and this means the\nequation has only ONE ROOT " << endl;
55      cout << "\nTherefore y = 0 at x = " << value1 << endl;
56      }
57  else if (discrim < 0)
58      {
59      cout << "The discriminant is  -" << dcomplex << " and this means the\nequation has TWO COMPLEX ROOTS" << endl;
60      cout << "\nImaginary X Value #1 =" << vcomplex1 << "i" << endl;
61      cout << "Imaginary X Value #2 =" << vcomplex2 << "i" << endl;
62      }
63  else
64      {
65      cout << "The discriminant is " << discrim << " and this means the\nequation has TWO REAL ROOTS" << endl;
66      cout << "\nX Value #1 =" << value1 << endl;
67      cout << "X Value #2 =" << value2 << endl;
68      }
69      cout << "\n\n\nPlease press ENTER to solve another 2nd Degree Polynominal\n\n\n";
70  //End Opperation
71      cin.get();
72      cin.get();
73  
74      main();
75  
76  }
The using declarative in line 4 is sort of frowned upon because you cannot unset it later in the code. Larger applications will often qualify the whole namespace every time, or use a namespace macro i.e.
1
2
#define S std::
S cout << "S is for Swag" << S endl;


Mixing \n with std::endl is probably ok as long as there is an endl at the end. Had to in fact check myself what the exact difference is heh
http://www.cplusplus.com/forum/beginner/7350/

Finally regarding that glorious ascii art, remember when someone wants to utilize your program using command line parameters (you are using user input which is something different ok, but still) to calculate something and programmatically feed the result elsewhere he will now have a ton of garbage to browse through.
Last edited on
It's great for your first program.

It's not good to call main, as you do in line 74.

To fix it, after line 6, add: while ( true ) {
and then replace line 74 with: }

You can check out while and more here:
http://www.cplusplus.com/doc/tutorial/control/

Next, you want to move onto functions:
http://www.cplusplus.com/doc/tutorial/functions/

I am ultimately looking to find if I should be doing something differently

The biggest problem is indeed a call to main() at line 74. This is undefined behavior in C++ (main is a special function; it is called by the OS and never by the programmer)

Other problems compilers point out are the unknown escape sequences at line 13: backslash in a string literal should be followed by another backslash, like so: Made by \\CodeKwik\\

As for layout, your comment at line 21 says "User Input Variables", but line 22 constructs a lot more than just a, b, and c. There is no point of having, for example, discrim created at line 22 and unused all the way until line 35 where you finally have a value to put into it. I would trim line 22 to just double a,b,c; and make line 35
double discrim = (pow(b, 2.0) - (4 * a * c));
(the coresponding guideline in the C++ Coding Standards (the book) is item 18 "Declare variables as locally as possible."

The output could be made a bit more compact with, for example, line 65-67 written out as
1
2
3
4
std::cout << "The discriminant is " << discrim << " and this means the\n"
          << "equation has TWO REAL ROOTS" << "\n\n"
          << "X Value #1 =" << value1 << '\n'
          << "X Value #2 =" << value2 << '\n';

but it's really just personal preference - it doesn't change the way the program executes (well, except for the endl vs \n: endl is not needed anywhere in this program, and using it instead of \n makes it run a bit slower, but of course there's no noticable difference in the user experience - you can use whatever you want as long as you keep that in mind)

Another thing to keep in mind: you've compared a double with a zero using operator == at line 52. The type double has limited precision, and in future you may often find yourself in situations where the result of a formula is supposed to be exactly zero, but it is off by some very small amount, in which case == would return false. It's not a big deal for this program either.
Topic archived. No new replies allowed.