Thread: On the right track?

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    101

    On the right track?

    I am just starting to teach myself C++ and set a little task to input 6 employees and their wagerates and hours worked and then output their total wages plus split them down into the notes and coins needed to pay them.
    Would it be asking too much to ask one of the more experienced among you to have a quick look at the result and then criticise constructively taking into account I have not put any error checking in as yet but I just want to know I am on the right track regarding layout and indenting and maybe logic as well before I delve deeper.

    Thanks in advance.

    Code:
    #include <windows.h>
    #include <iostream>
    #include <string>
    #include <vector>
    #include "ShortColours.h"
    #include <iomanip>
    using namespace std;
    
    int main()
    {
        int fiftyp [7];
        int twentyp [7];
        int tenp [7];
        int fivep [7];
        int twop [7];
        int onep [7];
        int fiftyd [7];
        int twentyd [7];
        int tend [7];
        int fived [7];
        int twod [7];
        int oned [7];
        string employee [7];
        float payrate [7];
        float hoursworked [7];
        float totalpay [7];
    
        HANDLE hOut;
        hOut=GetStdHandle(STD_OUTPUT_HANDLE);
        SetConsoleTextAttribute (hOut, BWI | FBI);
    
        for (int x(1); x<7; x++)
            {
                cout<<"Employee name : ";
                cin>>employee[x];
                cout<<"Payrate       : œ";
                cin>>payrate[x];
                cout<<"Hours worked  : ";
                cin>>hoursworked[x];
                totalpay[x]=payrate[x] * hoursworked[x];
                cout<<endl;
    
                SetConsoleTextAttribute (hOut, BBI | FWI);
                cout<<"Employee No "<<x<<endl;
                cout<<"Employee : "<<employee[x]<<endl;
                cout<<"Payrate  : œ"<<payrate[x]<<endl;
                cout<<"Hours    : "<<hoursworked[x]<<endl;
                cout<<"Total Pay  : œ" <<totalpay[x]<<endl<<endl<<endl<<endl;
            }
    
        for (int x(1); x<7; x++)
            {
                cout<<"Change required for "<<employee[x]<<endl;
                cout<<" œ50"<<setw(6)<<"   œ20"<<setw(6)<<"   œ10"<<setw(6)<<"    œ5"<<setw(6)<<"    œ2";
                cout<<setw(6)<<"    œ1"<<setw(6)<<"   50p"<<setw(6)<<"   20p"<<setw(6)<<"   10p"<<setw(6);
                cout<<"    5p"<<setw(6)<<"    2p"<<setw(6)<<"    1p"<<setw(6)<<endl;
    
                float divide=totalpay[x]*100;
                fiftyp[x]=divide/5000;divide=divide-(fiftyp[x]*5000);
                twentyp[x]=divide/2000;divide=divide-(twentyp[x]*2000);
                tenp[x]=divide/1000;divide=divide-(tenp[x]*1000);
                fivep[x]=divide/500;divide=divide-(fivep[x]*500);
                twop[x]=divide/200;divide=divide-(twop[x]*200);
                onep[x]=divide/100;divide=divide-(onep[x]*100);
                fiftyd[x]=divide/50;divide=divide-(fiftyd[x]*50);
                twentyd[x]=divide/20;divide=divide-(twentyd[x]*20);
                tend[x]=divide/10;divide=divide-(tend[x]*10);
                fived[x]=divide/5;divide=divide-(fived[x]*5);
                twod[x]=divide/2;divide=divide-(twod[x]*2);
                oned[x]=divide/1;divide=divide-(oned[x]*1);
    
                cout<<setw(4)<<fiftyp[x]<<setw(6)<<twentyp[x]<<setw(6)<<tenp[x]<<setw(6)<<fivep[x]<<setw(6);
                cout<<twop[x]<<setw(6)<<onep[x]<<setw(6)<<fiftyd[x]<<setw(6)<<twentyd[x]<<setw(6)<<tend[x];
                cout<<setw(6)<<fived[x]<<setw(6)<<twod[x]<<setw(6)<<oned[x]<<setw(6)<<endl<<endl<<endl<<endl;
            }
    
        SetConsoleTextAttribute (hOut, BWI | FRI);
        cout<<"Total change required for all employees :"<<endl;
        cout<<" œ50"<<setw(6)<<"   œ20"<<setw(6)<<"   œ10"<<setw(6)<<"    œ5"<<setw(6)<<"    œ2";
        cout<<setw(6)<<"    œ1"<<setw(6)<<"   50p"<<setw(6)<<"   20p"<<setw(6)<<"   10p"<<setw(6);
        cout<<"    5p"<<setw(6)<<"    2p"<<setw(6)<<"    1p"<<setw(6)<<endl;
    
        int t50p=fiftyp[1]+fiftyp[2]+fiftyp[3]+fiftyp[4]+fiftyp[5]+fiftyp[6];
        int t20p=twentyp[1]+twentyp[2]+twentyp[3]+twentyp[4]+twentyp[5]+twentyp[6];
        int t10p=tenp[1]+tenp[2]+tenp[3]+tenp[4]+tenp[5]+tenp[6];
        int t5p=fivep[1]+fivep[2]+fivep[3]+fivep[4]+fivep[5]+fivep[6];
        int t2p=twop[1]+twop[2]+twop[3]+twop[4]+twop[5]+twop[6];
        int t1p=onep[1]+onep[2]+onep[3]+onep[4]+onep[5]+onep[6];
        int t50d=fiftyd[1]+fiftyd[2]+fiftyd[3]+fiftyd[4]+fiftyd[5]+fiftyd[6];
        int t20d=twentyd[1]+twentyd[2]+twentyd[3]+twentyd[4]+twentyd[5]+twentyd[6];
        int t10d=tend[1]+tend[2]+tend[3]+tend[4]+tend[5]+tend[6];
        int t5d=fived[1]+fived[2]+fived[3]+fived[4]+fived[5]+fived[6];
        int t2d=twod[1]+twod[2]+twod[3]+twod[4]+twod[5]+twod[6];
        int t1d=oned[1]+oned[2]+oned[3]+oned[4]+oned[5]+oned[6];
    
        cout<<setw(4)<<t50p<<setw(6)<<t20p<<setw(6)<<t10p<<setw(6)<<t5p<<setw(6)<<t2p<<setw(6);
        cout<<t1p<<setw(6)<<t50d<<setw(6)<<t20d<<setw(6)<<t10d<<setw(6)<<t5d<<setw(6)<<t2d;
        cout<<setw(6)<<t1d<<setw(6)<<endl<<endl;
    
        cout<<endl<<endl;
        SetConsoleTextAttribute (hOut, BRI);
        cout <<"                                                                                ";
        SetConsoleTextAttribute (hOut, BWI);
        cout <<"                                                                                ";
        SetConsoleTextAttribute (hOut, FWI | BBI);
        cout<<"            Thanks for using this fantastic C++ program from JaySoft            ";
        SetConsoleTextAttribute (hOut, BYI);
        cout <<"                                                                                ";
        SetConsoleTextAttribute (hOut, FWI | BBI);
        cout<<"                           Just press <enter> to exit                           ";
        SetConsoleTextAttribute (hOut, BWI);
        cout <<"                                                                                ";
        SetConsoleTextAttribute (hOut, BRI);
        cout <<"                                                                                ";
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    You seem not to understand that indices start at 0 in C++ (and many other languages). If you want to have an array for 6 employees, you declare that array this way:

    type name[6];

    Also, way to go through your array is from 0 to 5.

    You might want to try making only 1 instruction per line and having a little more spaces between characters. There are pieces of your code that are rendered quite difficult to read because of this, such as this piece:

    Code:
                fiftyp[x]=divide/5000;divide=divide-(fiftyp[x]*5000);
                twentyp[x]=divide/2000;divide=divide-(twentyp[x]*2000);
                tenp[x]=divide/1000;divide=divide-(tenp[x]*1000);
                fivep[x]=divide/500;divide=divide-(fivep[x]*500);
                twop[x]=divide/200;divide=divide-(twop[x]*200);
                onep[x]=divide/100;divide=divide-(onep[x]*100);
                fiftyd[x]=divide/50;divide=divide-(fiftyd[x]*50);
                twentyd[x]=divide/20;divide=divide-(twentyd[x]*20);
                tend[x]=divide/10;divide=divide-(tend[x]*10);
                fived[x]=divide/5;divide=divide-(fived[x]*5);
                twod[x]=divide/2;divide=divide-(twod[x]*2);
                oned[x]=divide/1;divide=divide-(oned[x]*1);
    Also I don't see why you would divide something by 1. x/1 always equals x.
    Last edited by Alexandre; 10-06-2011 at 12:48 PM.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    Quote Originally Posted by Alexandre View Post
    You seem not to understand that indices start at 0 in C++ (and many other languages). If you want to have an array for 6 employees, you declare that array this way:

    type name[6];

    Also, way to go through your array is from 0 to 5.

    You might want to try making only 1 instruction per line and having a little more spaces between characters. There are pieces of your code that are rendered quite difficult to read because of this, such as this piece:

    Code:
                fiftyp[x]=divide/5000;divide=divide-(fiftyp[x]*5000);
                twentyp[x]=divide/2000;divide=divide-(twentyp[x]*2000);
                tenp[x]=divide/1000;divide=divide-(tenp[x]*1000);
                fivep[x]=divide/500;divide=divide-(fivep[x]*500);
                twop[x]=divide/200;divide=divide-(twop[x]*200);
                onep[x]=divide/100;divide=divide-(onep[x]*100);
                fiftyd[x]=divide/50;divide=divide-(fiftyd[x]*50);
                twentyd[x]=divide/20;divide=divide-(twentyd[x]*20);
                tend[x]=divide/10;divide=divide-(tend[x]*10);
                fived[x]=divide/5;divide=divide-(fived[x]*5);
                twod[x]=divide/2;divide=divide-(twod[x]*2);
                oned[x]=divide/1;divide=divide-(oned[x]*1);
    Also I don't see why you would divide something by 1. x/1 always equals x.
    Thanks Alexandre; I accept your criticisms but would say I do understand that arrays normally start at 0; I guess I was being lazy as I wanted the employee number to be the same as the array index no.

    The dividing by one was also probably laziness as I just copied the lines down - should have changed the bottom line to oned[x]=divide.

    I will try to remember to space my statements out a little more - old habit from when memory was short many moons ago when I used BASIC.

    May I ask is it possible to have multi arrays eg making one such as employee[6,3] and having the name[x,1], payrate[x,2] and hours[x,3] for each employee in one array?

    I guess it's ok to go back to the books now and press on then?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it is.
    You declare it as:

    T var[x][y];

    where T is the type, x and y are the dimensions.
    You access it as:

    var[x][y]

    where once again x and y are dimensions (ie the element you wish to access).
    I'd rather see you use std::array or std::vector instead of C-style arrays, though. Buffer overruns are so common with them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    Quote Originally Posted by Elysia View Post
    I'd rather see you use std::array or std::vector instead of C-style arrays, though. Buffer overruns are so common with them.
    Is it really so bad to use C-style arrays; I understand them and their use from my BASIC days; I have lookled at std::array and std::vector and in comparison they seem quite convoluted; I especially can't get my head round how you can use vectors as arrays when they seem to write to the next available space and pull info off starting at the end rather than having random access to the info stored - but maybe I am reading it all wrongly - do you seriously think that I ought to take the time to try and learn std::array and std::vector rather than using C-syle arrays.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, I do. C-style arrays are legacy arrays. They don't integrate well into the library and are dangerous to use. Access on element wrong and you're in undefined territory. Debuggers aren't guaranteed to catch the error, either.
    std::array and std::vector solves all the above problems.
    std::vector is a dynamic array; that is, it can grow to make room for new elements you put in it. But you can access any element in it using standard array notation.
    std::array is a fixed-size array, which means you allocate space at the beginning and then use it as a standard array.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    I have lookled at std::array and std::vector and in comparison they seem quite convoluted
    What about them do you find convoluted ?
    In case of both of them, you can, in general forget about them .. and just use them as normal arrays. (functions of cstring won't work obviously, but there are alternatives)

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    Quote Originally Posted by manasij7479 View Post
    What about them do you find convoluted ?
    In case of both of them, you can, in general forget about them .. and just use them as normal arrays. (functions of cstring won't work obviously, but there are alternatives)
    I looked up std::array and am I correct in that you initialise it as
    array <int,6> for an array with 7 elements - but what I can't see is how do you name it - is it:
    fred = array <int,6> then use it as fred[0], fred[1] etc.

    If that is correct then it is easy as you say!

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, the correct syntax is:
    std::array<T, N> my_array;

    Where T is the type of the array and N is the number of elements.
    The equivalent C-style array syntax is:

    T my_array[N];

    Accessing elements is done in the usual way:
    my_array[0]
    my_array[1]
    ...
    my_array[N - 1]
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    I tried replacing float totalpay [6]; with std::array<float, 6> totalpay; but my compiler gave an error as follows:

    error: 'array' is not a member of 'std'
    error: expected primary-expression before 'float'

    Is there another #include I need to use this array - I tried #include <array> but got another error as follows:

    This file requires compiler and library support for the upcoming ISO C++ ...

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Perhaps if you told us your compiler we might help you more.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    Quote Originally Posted by Elysia View Post
    Perhaps if you told us your compiler we might help you more.
    I am using Code::Blocks and I think the compiler is Mingw - does that sound right?

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Perhaps. Code::Blocks can use many compilers, but there is a default installation with MinGW.
    Anyway, if you go into compiler options, there should be an option to enable C++0x (now known as C++11) support. Try ticking that and recompiling.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by JayCee++ View Post
    I am using Code::Blocks and I think the compiler is Mingw - does that sound right?
    Settings -> Compiler and Debugger -> Compiler Settings -> Compiler Flags

    There check the option to "Have g++ follow the upcoming ........[-std=c++0x]"

  15. #15
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    Thanks you two miracle workers - it compiles OK now so Elysia I will go through my program changing my arrays and see if it all works OK after that.

    Thanks for all the help, sorry to be a bit slow on the uptake but I was warned that having used BASIC for nearly 30 years it would be a hindrance in learning C++ - but we'll get there - eventually!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sector per track
    By sunil21 in forum C Programming
    Replies: 1
    Last Post: 09-13-2003, 11:10 PM
  2. Am I on the right track
    By romeoz in forum C++ Programming
    Replies: 12
    Last Post: 07-07-2003, 10:10 PM
  3. Here I am going to ramble about something, just tell me if I am on the right track.
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 04-03-2002, 03:57 PM
  4. not on right track
    By Dena in forum C++ Programming
    Replies: 1
    Last Post: 02-23-2002, 07:03 PM