Author

Topic: Total beginner at programming here. (C++) (Read 2344 times)

newbie
Activity: 14
Merit: 0
September 07, 2012, 07:31:31 PM
#15
hello
for money, you should better use a special money class due to reasons explained by kind posters
an example may be
http://www.di-mare.com/adolfo/p/src/money.h

cheers
full member
Activity: 167
Merit: 100
September 06, 2012, 08:29:49 AM
#14
Just to clarify, I wasn't criticizing your commenting (it looks good to me), I got lazy with commenting after a while and it cost me a lot of time later on, just a heads up.
It's not uncommon, at least for me, to look back at code written just a month or two ago and be completely baffled about what it was doing.


It's even worse looking back at your own code written in a write only language (eg: Perl)
legendary
Activity: 1596
Merit: 1012
Democracy is vulnerable to a 51% attack.
September 05, 2012, 08:12:15 PM
#13
Just to clarify, I wasn't criticizing your commenting (it looks good to me), I got lazy with commenting after a while and it cost me a lot of time later on, just a heads up.
It's not uncommon, at least for me, to look back at code written just a month or two ago and be completely baffled about what it was doing.
legendary
Activity: 1890
Merit: 1086
Ian Knowles - CIYAM Lead Developer
September 05, 2012, 10:05:46 AM
#12
When it comes to adding comments I would suggest something along the line of the following:

1) If the code is obvious do not add a comment as it's not necessary (variable and function naming, however, is something you should spend a lot of time thinking about).

2) If the comment is just describing a typical comparison or mathematical operation then don't add a comment as it's not necessary.

3) If the comment is just describing a typical looping construct or an obvious algorithm (for algos better to use functional programming techniques) then don't add a comment as it's not necessary.

In my first job I worked on a very large C project (it included its own complete p-machine style 4GL and networking platform) which at one stage employed over 100 programmers.

One day I was having troubles with a particular section of code and I called over an older "guru" to help me out. I explained what the problem was that I was having and why I didn't understand how the code was not working as I thought it should be.

He asked a simple question:

"Why do you think the code should be doing what you say it should be doing?"

and I answered:

"Because that's what these comments say."

His answer, and one of the best (and at the time one of the most embarrassing) lessons I ever learned in my career, was "The compiler doesn't read those - you need to read the code Ian.".

Smiley
sr. member
Activity: 336
Merit: 250
September 05, 2012, 09:07:48 AM
#11
Here is an old small program that I found that illustrates the kind of problems you get with using the double type:

Code:
#define test( v1, op, v2 ) if( v1 op v2 ) printf( #v1 " " #op " " #v2 "\n" )

#include
#include

int main( )
{
   volatile double a, b, c;
   char buf[ 50 ];

   a = 13.06;
   b = 0.54;
   sprintf( buf, "%lf", a + b );
   c = atof( buf );

   printf( "a = %lf, b = %lf, c = %lf\n", a, b, c );       

   test( c, >, 13.60 );
   test( c, ==, 13.60 );
   test( c, <, 13.60 );
   test( a + b, >, 13.60 );
   test( a + b, ==, 13.60 );
   test( a + b, <, 13.60 );

   return 0;
}

When I run this (compiled with VC++ under Win32) I get the following output:
Code:
a = 13.060000, b = 0.540000, c = 13.600000
c == 13.60
a + b > 13.60

The first two lines of output are fine but note that 13.06 + 0.54 is > 13.60 due to the use of the double type here.


I see. That's very interesting. Thanks for digging that up CIYAM.

What's all those pointy arrow things? Why is it all in lower case?? How can it function without GOTO's???

Nice work, good going for 3 weeks and you seem to be getting some great advice here.

All I'd like to add is try stuff. If you have an idea that seems weird try it out and see what happens and have a look at different languages, there are some really cool ones. It took me ages to get to grips with even really simple programming and OO still makes no sense to me, fortran and assembler just clicked though (some ba$*@rd got me learning basic when I was young). Try not to forget to comment the code to, its surprising how little sense stuff makes when you dig it out years later.

Good luck with it.

Will do. I've been making a point of commenting, will do even more in the future. Ha, in 6th grade I taught myself a little bit of qBasic, it's all coming back to me now. I remember all those messy GOTO loops I made. I never really thought much more about programming until pretty recently. Well, besides TI calculators.
legendary
Activity: 1890
Merit: 1086
Ian Knowles - CIYAM Lead Developer
September 04, 2012, 03:59:08 AM
#10
Actually the first software program that I ever wrote was in machine language in the very early 80's on something that looked at lot like this:



(yup - it had a magnetic strip to store your program on!)
legendary
Activity: 1890
Merit: 1086
Ian Knowles - CIYAM Lead Developer
September 04, 2012, 03:22:04 AM
#9
Here is an old small program that I found that illustrates the kind of problems you get with using the double type:

Code:
#define test( v1, op, v2 ) if( v1 op v2 ) printf( #v1 " " #op " " #v2 "\n" )

#include
#include

int main( )
{
   volatile double a, b, c;
   char buf[ 50 ];

   a = 13.06;
   b = 0.54;
   sprintf( buf, "%lf", a + b );
   c = atof( buf );

   printf( "a = %lf, b = %lf, c = %lf\n", a, b, c );        

   test( c, >, 13.60 );
   test( c, ==, 13.60 );
   test( c, <, 13.60 );
   test( a + b, >, 13.60 );
   test( a + b, ==, 13.60 );
   test( a + b, <, 13.60 );

   return 0;
}

When I run this (compiled with VC++ under Win32) I get the following output:
Code:
a = 13.060000, b = 0.540000, c = 13.600000
c == 13.60
a + b > 13.60

The first two lines of output are fine but note that 13.06 + 0.54 is > 13.60 due to the use of the double type here.
legendary
Activity: 1890
Merit: 1086
Ian Knowles - CIYAM Lead Developer
September 04, 2012, 02:44:30 AM
#8
I'm assuming this is because they're only 32 bit integers? Or is it because of the inaccurate way that these data types store decimals?

Yup - because they are binary floating point types the problem is indeed due to the representation of certain decimal values (same as 1/3 can't be expressed without recurring digits in decimals).

These type of issues are not always seen when doing fairly trivial calculations but can cause real problems when you are dealing with dollars and cents (especially when it comes to auditors Smiley). Unfortunately I had the bad luck to work on a project that was involving decimal calculations and had chosen to work with the double type - as they didn't want to change the choice of type the resulting code ended up with rounding functions being called all over the place (a horrible mess).
sr. member
Activity: 336
Merit: 250
September 04, 2012, 02:27:02 AM
#7
Another thing to at least be aware of (in terms of bad habits) is that the use of float or double types for any professional financial software would be considered a very bad idea (unfortunately though Standard C++ doesn't provide an alternative type for this).

I'm assuming this is because they're only 32 bit integers? Or is it because of the inaccurate way that these data types store decimals?
sr. member
Activity: 336
Merit: 250
September 04, 2012, 02:22:23 AM
#6
That's pretty damn impressive for 3 weeks.

Thank you! We're moving at a brisk pace so I'm having to overtime it to keep up.

Not a bad start - just a couple of minor problems:

1) void main( ) is not Standard C++ - you need to use int main( ) (although you don't actually need to code a return statement in main).

I see. My teacher has been having us start with void main(), just today we started learning about functions. That makes a lot of sense, because the linux compiler I use won't run void main() or system("Pause"), so I've been writing all my programs in a computer lab. That's very helpful to know.

Quote
system("Pause");

Whoa! I almost ran your program to test it. Fortunately, I caught that before I did. On my computer, "Pause" is the command to pause the cooling system on my home nuclear reactor.

There's no need for that. You are writing a console program and it writes to the console. And it's a habit you should break as soon as possible, because when you write programs used as pipelines or redirected to files, that will break things.

http://www.gidnetwork.com/b-61.html


I'll start doing something like CIYAM said from now on. Thanks for showing me that! Our teacher has been having us use system("Pause") to keep the screen from disappearing.

Glad you caught the mistake before the meltdown initiated, Joel. Thanks for the link. He did show us cin.get() on like the first day of class, but he seems to like "Pause." No wonder my compiler didn't know what to do with that.

It's so helpful to get feedback from you guys.
legendary
Activity: 1890
Merit: 1086
Ian Knowles - CIYAM Lead Developer
September 03, 2012, 10:48:32 PM
#5
There's no need for that. You are writing a console program and it writes to the console. And it's a habit you should break as soon as possible, because when you write programs used as pipelines or redirected to files, that will break things.

I have to agree that even rather than the at least more standard C++ approach I suggested it would be better to just change your console settings so that the console window doesn't disappear when the program has finished (assuming I guessed correctly as to why you had coded this).

Another thing to at least be aware of (in terms of bad habits) is that the use of float or double types for any professional financial software would be considered a very bad idea (unfortunately though Standard C++ doesn't provide an alternative type for this).
legendary
Activity: 1596
Merit: 1012
Democracy is vulnerable to a 51% attack.
September 03, 2012, 10:44:50 PM
#4
Quote
system("Pause");

Whoa! I almost ran your program to test it. Fortunately, I caught that before I did. On my computer, "Pause" is the command to pause the cooling system on my home nuclear reactor.

There's no need for that. You are writing a console program and it writes to the console. And it's a habit you should break as soon as possible, because when you write programs used as pipelines or redirected to files, that will break things.

http://www.gidnetwork.com/b-61.html
legendary
Activity: 1890
Merit: 1086
Ian Knowles - CIYAM Lead Developer
September 03, 2012, 10:43:09 PM
#3
Not a bad start - just a couple of minor problems:

1) void main( ) is not Standard C++ - you need to use int main( ) (although you don't actually need to code a return statement in main).

2) I'm not quite sure what the point of the system( "Pause" ) - is this because you are using Windows and are trying to prevent a command prompt (or DOS box as still sometimes called) from disappearing?

If so then for a more Standard C++ approach maybe use "cin" with something like the following:

Code:
...
   while( true )
   {
... original code here ...
      char ch;
      cout << "Again or Quit? [aq]";
      cin >> ch;
      if( ch == 'Q' || ch == 'q' )
         break;
   }
...
hero member
Activity: 728
Merit: 500
September 03, 2012, 10:40:08 PM
#2
That's pretty damn impressive for 3 weeks.
sr. member
Activity: 336
Merit: 250
September 03, 2012, 09:56:57 PM
#1
This is the third week of my first programming class, so I thought I'd share my latest project. Total noob stuff here. I spent 3 hours on this and I'm kind of proud of it, mostly just because it works. Bitcoin is a huge part of what inspired me to study computer science, so it's a little bit exciting for me to post this here.

The assignment was to create a program that displays a payment plan for a credit card consumer. It only had to work for positive balances.

Code:
#include
#include
using namespace std;
void main()
{
    double bal; // card balance
    double r;  // interest rate
    double mc; // monthly charge
    double mp; // monthly payment
    double fc; // finance charge
    double lp; // last payment
    double negbal = 1.0; // to be used when bal is <= 0
    int month = 0; // month counter
    bool hasHeader = false; // I used this variable so that I would not display the column titles in the case that the card cannot be paid off
    cout.precision(2);
    cout.setf(ios_base::fixed);
    cout << "Initial balance? ";
    cin >> bal;
    cout << "Interest rate?\t "; // asking for a % APR
    cin >> r;
    cout << "Monthly charge?\t ";
    cin >> mc;
    cout << "Monthly payment? ";
    cin >> mp;
    r = r / 12 / 100; // convert APR to MPR
    double orbal = bal; // orbal will later represent the original balance, to test whether balance increases over time (and thus cannot be paid off)
    do
    {
        month++;
        fc = bal*r;
        bal = (fc + mc + bal) - mp;
        if(orbal < bal)
        {
            cout << endl << "Card cannot be paid off." << endl;
            bal = 0.0;
        }
        else
        {
            if (hasHeader == false)
            {
                cout << endl << "Month     " << "F/C     " << "Balance" << endl;
                hasHeader = true;
            }
            if(bal <= 0)
            {
                negbal = bal;
                bal = 0.0;
            }
            cout << setw(3) << month << setw(10) << fc << setw(12) << bal << endl;
            if (bal <= 0) // loop to determine last payment
            {
                lp = mp + negbal;
                cout << endl << "Your last payment was " << lp << endl;
            }
        }
    }
    while(bal > 0);
    system("Pause");
}
Jump to: