First C++ Project.

So, I just started learning C++ about three days ago. I'm using Sam's Teach Yourself C++ in 21 Days. It seems to be a good reference so far.

I'm about to start Day 4, but thought I would upload my first "I did it myself" project so 1) I can pat myself on the back and 2) you more experienced code junkies can tell me how I can make it better and/or simpler as I get further into the book.

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
#include <iostream>

float Add(float alpha, float bravo)
{
using std::cout;
cout << "\nIn Add().\n";
cout << "Finding the sum of " << alpha << " and " << bravo << " ...\n";
return (alpha + bravo);
}

float Subt(float alpha, float bravo)
{
using std::cout;
cout << "\nIn Subt().\n";
cout << "Finding the difference of " << alpha << " and " << bravo << " ...\n";
return (alpha - bravo);
}

float Multi(float zulu, float yankee)
{
using std::cout;
cout << "\nIn Multi().\n";
cout << "Multiplying the sum of the two integers, " << zulu << " by their difference, " << yankee << " ...\n";
return (zulu * yankee);
}

float Div(float papa, float charlie)
{
using std::cout;
cout << "\nIn Div().\n";
cout << "I will now divide " << papa << " by your third input, " << charlie << " ...\n";
return (papa / charlie)
}

int main()
{
using namespace std;

float a, b, c, p, z, y, x;
cout << "Welcome!\n";
cout << "Here, I will perform four simple arithmetic equations based upon your input.\n";
cout << "Please input two numbers: ";
cin >> a;
cin >> b;

cout << "\nCalling the Add() function... \n";
z=Add(a,b);
cout << "The sum is " << z << ".\n";

cout << "\nExcellent! Now we will find the difference of your two numbers... \n";
y=Subt(a,b);
cout << "The difference is " << y << ".\n";

cout << "\nNow for the Multiplication part!\n";
cout << "Calling the Multi() function... \n";
p=Multi(z,y);
cout << "The product is " << p << " .\n";

cout << "\nLastly, I will divide the product by your third and final input.\n";
cout << "Please enter a third number: ";
cin >> c;
cout << "\nThank you. Transferring you to Div().\n";
x=Div(p,c);

cout << "\nProgram Complete!\n";
cout << "The answer is " << x << ".\n";
cout << "\nProgram Terminating...\n";

char response;
cin >> response;

return 0;
}


So, if you can tell, it takes two number provided by the user, adds them and subtracts them, multiplies the sum by the difference, then divides the product by a third user-defined number.

Anyways, I'm open to all criticism, so let me have it!
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
#include <iostream>
using namespace std;

float Add(float A, float B)
{
    cout << "Finding the result of " << A << " + " << B << "." << endl;
    return (A + B);
}

float Sub(float A, float B)
{
    cout << "Finding the result of " << A << " - " << B << "." << endl;
    return (A - B);
}

float Mult(float A, float B)
{
    cout << "Finding the result of " << A << " x " << B << "." << endl;
    return (A * B);
}

float Div(float A, float B)
{
    cout << "Finding the result of " << A << " / " << B << "." << endl;
    return (A / B);
}

main()
{
    float A = 0, B = 0, C = 0, D = 0; // Tip: You should initialize varibles to some value.
    cout << "Welcome!" << endl;
    cout << "Here, I will perform four simple arithmetic equations based upon your input." << endl;
    cout << "Please input two numbers: ";
    cin >> A >> B;

    cout << "\nCalling the Add() Function." << endl;
    C = Add(A, B);
    D = C;
    cout << "The result is " << C << "." << endl;

    cout << "\nCalling the Sub() Function." << endl;
    C = Sub(A, B);
    D += C;
    cout << "The result is " << C << "." << endl;

    cout << "\nCalling the Mult() Function." << endl;
    C = Mult(A, B);
    D += C;
    cout << "The result is " << C << "." << endl;

    cout << "\nCalling the Div() Function." << endl;
    C = Div(A, B);
    D += C;
    cout << "The result is " << C << "." << endl;

    cout << "\nThe program is now complete!" << endl;
    cout << "The answer is " << D << "." << endl;
    cout << "Program is now terminating." << endl;

    system("pause");
}


1. You want to initialize variables while declaring them.
2. You use too many variables, four is enough for this program.
3. Your scope calls are unnecessary, "using namespace std;" as the first line solves this.
4. Make indents, they help make your code look clean and easier to read.
5. Make comments, they are not needed for this, but for more complicated code it helps.

Need anymore help? I have been learning this since January of 2011. I'll help as much as possible.
Last edited on
No book that promises to teach you a programming language in "21 days" or in 10 minutes a day is a good reference. Get a real book. Some suggestions I've seen over the years are:

C++ Primer: 5th Edition
http://www.amazon.com/Primer-Plus-5th-Stephen-Prata/dp/0672326973/ref=sr_1_2?ie=UTF8&qid=1303687493&sr=8-2

(I might have the wrong author name on that one. Can someone verify?)

The C++ Programming Language by Bjarne Stroustrup
http://www.amazon.com/C-Programming-Language-Special/dp/0201700735/ref=sr_1_6?s=books&ie=UTF8&qid=1303687540&sr=1-6

(Who better to learn from than the creator of the language?)
Last edited on
Well expanding on what Khaltazar said.

1. Initializing variables is a coding standard in order to protect the integrity of your code. However, this more specifically applies to temporary variables e.g. variables instantiated inside 'for' loops for example that are destroyed at the end. If you don't initialize these variables it is pretty much allocated garbage or whatever is in the memory at the time which could lead to some crazy results. e.g.

Code:

1
2
3
4
5
6
7
8
int myAge;
printf ("This is my age: %d?\n", myAge);
    
for(int i = 0; i < 1; i++)
{
	int myAge2;
	printf ("This is my age: %d?\n", myAge2);
}


Output:

This is my age: 0?
This is my age: 2280816?


2. Correct. You should try to use as little variables as possible if you want to conserve memory.
This can be achieved either by reusing existing variables that are not needed later (reuse of C variable in Khaltazar's code) or by replacing variable calls with function calls. e.g.

cout << "The result is " << Add(A, B) << "." << endl;

3. Agreed. Declare your scope at the start of your code.

4 + 5. This is very important! Clean code is great code. As a programmer or a programmer to be working in the software industry. Your code will be reviewed by your seniors or peers. The more readable your code is the better it is for them and you to make changes to it. Get in the habit or making clear concise comments where necessary in your code to better get across the logic of it and make sure that you use indents. This will convey the different layers in your work. This will come in handy if you have to use Python which doesn't make use of parenthesis.
Last edited on
heres a really quick version of how i would have it layed out =P

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
#include <iostream>
#include <limits>
using namespace std;
float a, b, c, p, z, y, x;
class Function
{
    public:
    void welcome()
    {
        cout << "Welcome!\n";
        cout << "Here, I will perform four simple arithmetic equations based upon your input.\n";
        cout << "Please input two numbers: ";
    }
    float Add(float alpha, float bravo)
    {
    cout << "\nIn Add().\n";
    cout << "Finding the sum of " << alpha << " and " << bravo << " ...\n";
    return (alpha + bravo);
    }

    float Subt(float alpha, float bravo)
    {
    cout << "\nIn Subt().\n";
    cout << "Finding the difference of " << alpha << " and " << bravo << " ...\n";
    return (alpha - bravo);
    }

    float Multi(float zulu, float yankee)
    {
    cout << "\nIn Multi().\n";
    cout << "Multiplying the sum of the two integers, " << zulu << " by their difference, " << yankee << " ...\n";
    return (zulu * yankee);
    }

    float Div(float papa, float charlie)
    {
    cout << "\nIn Div().\n";
    cout << "I will now divide " << papa << " by your third input, " << charlie << " ...\n";
    return (papa / charlie);
    }
};
int main()
{
    Function func;
    func.welcome();
    cin >> a;
    cin >> b;
    cout << "\nCalling the Add() function... \n";
    z=func.Add(a,b);
    cout << "The sum is " << z << ".\n";

    cout << "\nExcellent! Now we will find the difference of your two numbers... \n";
    y=func.Subt(a,b);
    cout << "The difference is " << y << ".\n";

    cout << "\nNow for the Multiplication part!\n";
    cout << "Calling the Multi() function... \n";
    p=func.Multi(z,y);
    cout << "The product is " << p << " .\n";

    cout << "\nLastly, I will divide the product by your third and final input.\n";
    cout << "Please enter a third number: ";
    cin >> c;
    cout << "\nThank you. Transferring you to Div().\n";
    x=func.Div(p,c);

    cout << "\nProgram Complete!\n";
    cout << "The answer is " << x << ".\n";
    cout << "\nPress enter to continue...\n";
    cin.sync();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    return 0;
}
@metl wolf
No comments? tsk tsk :P

@syntax error
metl wolf has given you a good example of extendability by creating the class 'Function', even though he could of used a more descriptive name :P, this class can be reused in other projects if needed to. Extendability is very good coding practice.
@GodPyro
haha =P i said it was really quick. i actually expected more people to put in their own versions of his code. i thought i was just adding to the pool of examples. yeah your right, i should have put some comments in there describing classes and such.
Topic archived. No new replies allowed.