Thread: is my programming flawless? i need to reach that level of skill

  1. #31
    Registered User
    Join Date
    Apr 2013
    Posts
    103
    oh k and this is my 1st forum i am using

  2. #32
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Mukul Kumar View Post
    @elysia whoose avoiding it? read post#16
    To not know something is acceptable. No one knows everything.
    But that is not an excuse. I have yet to see you even attempt to use it.
    If you attempt it, even if you fail, we would be more encouraged to help you.

    So take some time to implement the feedback so far (including std::string!).
    Then make another attempt to post what you have.
    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.

  3. #33
    Registered User
    Join Date
    Apr 2013
    Posts
    103
    oh kay
    here is the change
    Code:
    #include<iostream>
    #include<conio.h>
    #include<stdlib.h>
    #include<time.h>
    #include<string.h>
    class Bowler{//******************************class bowler***********************
        int overs,povers,maidovers,runsgiv,wictaken;
        public:
            void disp()
            {
                std::cout<<"\n\t   BOWLER\n";
                std::cout<<"\novers played       : "<<povers;
                std::cout<<"\nmaiden overs       : "<<maidovers;
                std::cout<<"\nruns given         : "<<runsgiv;
                std::cout<<"\nwickets taken      : "<<wictaken;
            }
            int giv(char *choose)
            {
                if(strcmp(choose,"overs")==0)
                {
                    return(overs);
                }
                else if(strcmp(choose,"maidovers")==0)
                {
                    return(maidovers);
                }
                else if(strcmp(choose,"runsgiv")==0)
                {
                    return(runsgiv);
                }
                else if(strcmp(choose,"wictaken")==0)
                {
                    return(wictaken);
                }
            }
            void over()
            {
                std::cout<<"enter number of overs : ";
                std::cin>>overs;
            }
            void update(int run,int iout,int j,int balls[])
            {
                int l=0,m;
                if(j==5)
                {
                    povers++;
                    for(m=0;m<6;m++)
                    {
                        if(balls[m]==0)
                            l++;
                    }
                }
                if(l==6)
                {
                    maidovers++;
                    for(m=0;m<6;m++)
                    {
                        balls[m]=1;
                    }
                }
                if(run!=5)
                    runsgiv=run;
                else
                    runsgiv=0;
                if(!iout)
                {
                    wictaken++;
                }
            }
    };
    
    class Batsman{//********************class Batsman*******************************
        int runs,iout,run,mod;
        char name[10],*mout;
        static int count;
        public:
            Batsman()
            {
                iout=1;
            }
            void updatecount()
            {
                count++;
            }
            int giv(char *choose)
            {
                if(strcmp(choose,"iout")==0)
                {
                    return(iout);
                }
                else if(strcmp(choose,"runs")==0)
                {
                    return(runs);
                }
                else if(strcmp(choose,"run")==0)
                {
                    return(run);
                }
            }
            void init(int &a)
            {
                system("cls");
                if(a!=0)
                {
                    std::cout<<"the batsman #"<<count-1<<" is out now out\n";
                }
                std::cout<<"enter name of batsman #"<<count<<" : ";
                std::cin>>name;
                iout++;
                runs=0;
            }
            void disp()
            {
                std::cout<<"\n\t   BATSMAN #"<<count<<"\n";
                std::cout<<"\nName               : "<<name;
                std::cout<<"\nruns made          : "<<runs;
                std::cout<<"\nstatus             : ";
                switch(mod)
                {
                    case 0:strcpy(mout,"Catch-out");;break;
                    case 1:strcpy(mout,"Run-Out");break;
                    case 2:strcpy(mout,"Wicket");break;
                    case 3:strcpy(mout,"Bold");break;
                    case 4:strcpy(mout,"Hit-Wicket");break;
                    default :;
                }
                if(!iout)
                {
                    std::cout<<"Out\n";
                    std::cout<<"mode by which out  : "<<mout;
                }
                else if(iout)
                {
                    std::cout<<"Not out";
                }
            }
            void update()
            {
                srand(time(NULL));
                run=rand()%7;
                if(run==5)
                {
                    iout--;
                    mod=rand()%5;
                }
                else
                {
                    runs+=run;
                }
            }
    };
    
    int Batsman :: count=1;
    
    int main()
    {
        Bowler ball;
        Batsman bats[10];
        ball.over();
        system("cls");
        char place[3];
        int ov,i=0,balls[6],j,l=0,k;
        bats[i].init(i);
        std::cout<<"the score board is ready to be used\n";
        getch();
        system("cls");
        ov=(ball.giv("overs"));
        for(k=0;k<ov;k++)
        {
            if(l)
            {
                break;
            }
            for(j=0;j<6;j++)
            {
                balls[j]=bats[i].giv("run");
                bats[i].update();
                ball.update(bats[i].giv("run"),bats[i].giv("iout"),j,balls);
                if(k+1==1)
                {
                    strcpy(place,"st");
                }
                else if(k+1==2)
                {    
                    strcpy(place,"nd");
                }
                else if(k+1==3)
                {
                    strcpy(place,"rd");
                }
                else
                {
                    strcpy(place,"th");;
                }
                if(j==6)
                {
                    system("cls");
                    std::cout<<"the "<<k+1<<place<<" over is finished";
                    std::cout<<"\n";
                }
                bats[i].disp();
                ball.disp();
                std::cout<<"\n\nnow the bowler throws the ball...\n";
                std::cout<<"press enter to update the score board...";
                getch();
                system("cls");
                if(!bats[i].giv("iout"))
                {
                    if(i<10)
                    {
                        i++;
                    }
                    else if (i>10)
                    {
                        l++;
                        j=7;
                        break;
                    }
                    bats[i].updatecount();
                    bats[i].init(i);
                }
            }//end of loop of balls
        }//end of loop of overs
        system("cls");
        if(k==ov)
        {
            std::cout<<"overs finished";
        }
        else if(l)
        {
            std::cout<<"all batsmen out !";
        }    
        std::cout<<"\nend result :-";
        bats[i].disp();
        ball.disp();
    getch();
    return 0;
    }//end of main()

  4. #34
    Registered User
    Join Date
    Apr 2013
    Posts
    103
    gonna use std::string just wait

  5. #35
    Registered User
    Join Date
    Apr 2013
    Posts
    103
    how am i supposed to initilize these string variables?
    i am getting errors
    Code:
    #include<iostream>
    #include<conio.h>
    #include<stdlib.h>
    #include<time.h>
    #include<string.h>
    class Bowler{//******************************class bowler***********************
        int overs,povers,maidovers,runsgiv,wictaken;
        public:
            void disp()
            {
                std::cout<<"\n\t   BOWLER\n";
                std::cout<<"\novers played       : "<<povers;
                std::cout<<"\nmaiden overs       : "<<maidovers;
                std::cout<<"\nruns given         : "<<runsgiv;
                std::cout<<"\nwickets taken      : "<<wictaken;
            }
            int giv(std::string choose)
            {
                if(strcmp(choose,"overs")==0)
                {
                    return(overs);
                }
                else if(strcmp(choose,"maidovers")==0)
                {
                    return(maidovers);
                }
                else if(strcmp(choose,"runsgiv")==0)
                {
                    return(runsgiv);
                }
                else if(strcmp(choose,"wictaken")==0)
                {
                    return(wictaken);
                }
            }
            void over()
            {
                std::cout<<"enter number of overs : ";
                std::cin>>overs;
            }
            void update(int run,int iout,int j,int balls[])
            {
                int l=0,m;
                if(j==5)
                {
                    povers++;
                    for(m=0;m<6;m++)
                    {
                        if(balls[m]==0)
                            l++;
                    }
                }
                if(l==6)
                {
                    maidovers++;
                    for(m=0;m<6;m++)
                    {
                        balls[m]=1;
                    }
                }
                if(run!=5)
                    runsgiv=run;
                else
                    runsgiv=0;
                if(!iout)
                {
                    wictaken++;
                }
            }
    };
    
    class Batsman{//********************class Batsman*******************************
        int runs,iout,run,mod;
        char name[10],*mout;
        static int count;
        public:
            Batsman()
            {
                iout=1;
            }
            void updatecount()
            {
                count++;
            }
            int giv(std::string choose)
            {
                if(strcmp(choose,"iout")==0)
                {
                    return(iout);
                }
                else if(strcmp(choose,"runs")==0)
                {
                    return(runs);
                }
                else if(strcmp(choose,"run")==0)
                {
                    return(run);
                }
            }
            void init(int &a)
            {
                system("cls");
                if(a!=0)
                {
                    std::cout<<"the batsman #"<<count-1<<" is out now out\n";
                }
                std::cout<<"enter name of batsman #"<<count<<" : ";
                std::cin>>name;
                iout++;
                runs=0;
            }
            void disp()
            {
                std::cout<<"\n\t   BATSMAN #"<<count<<"\n";
                std::cout<<"\nName               : "<<name;
                std::cout<<"\nruns made          : "<<runs;
                std::cout<<"\nstatus             : ";
                switch(mod)
                {
                    case 0:strcpy(mout,"Catch-out");;break;
                    case 1:strcpy(mout,"Run-Out");break;
                    case 2:strcpy(mout,"Wicket");break;
                    case 3:strcpy(mout,"Bold");break;
                    case 4:strcpy(mout,"Hit-Wicket");break;
                    default :;
                }
                if(!iout)
                {
                    std::cout<<"Out\n";
                    std::cout<<"mode by which out  : "<<mout;
                }
                else if(iout)
                {
                    std::cout<<"Not out";
                }
            }
            void update()
            {
                srand(time(NULL));
                run=rand()%7;
                if(run==5)
                {
                    iout--;
                    mod=rand()%5;
                }
                else
                {
                    runs+=run;
                }
            }
    };
    
    int Batsman :: count=1;
    
    int main()
    {
        Bowler ball;
        Batsman bats[10];
        ball.over();
        system("cls");
        std::string place;
        int ov,i=0,balls[6],j,l=0,k;
        bats[i].init(i);
        std::cout<<"the score board is ready to be used\n";
        getch();
        system("cls");
        ov=(ball.giv("overs"));
        for(k=0;k<ov;k++)
        {
            if(l)
            {
                break;
            }
            for(j=0;j<6;j++)
            {
                balls[j]=bats[i].giv("run");
                bats[i].update();
                ball.update(bats[i].giv("run"),bats[i].giv("iout"),j,balls);
                if(k+1==1)
                {
                    strcpy(place,"st");
                }
                else if(k+1==2)
                {    
                    strcpy(place,"nd");
                }
                else if(k+1==3)
                {
                    strcpy(place,"rd");
                }
                else
                {
                    strcpy(place,"th");;
                }
                if(j==6)
                {
                    system("cls");
                    std::cout<<"the "<<k+1<<place<<" over is finished";
                    std::cout<<"\n";
                }
                bats[i].disp();
                ball.disp();
                std::cout<<"\n\nnow the bowler throws the ball...\n";
                std::cout<<"press enter to update the score board...";
                getch();
                system("cls");
                if(!bats[i].giv("iout"))
                {
                    if(i<10)
                    {
                        i++;
                    }
                    else if (i>10)
                    {
                        l++;
                        j=7;
                        break;
                    }
                    bats[i].updatecount();
                    bats[i].init(i);
                }
            }//end of loop of balls
        }//end of loop of overs
        system("cls");
        if(k==ov)
        {
            std::cout<<"overs finished";
        }
        else if(l)
        {
            std::cout<<"all batsmen out !";
        }    
        std::cout<<"\nend result :-";
        bats[i].disp();
        ball.disp();
    getch();
    return 0;
    }//end of main()

  6. #36
    Registered User
    Join Date
    Apr 2013
    Posts
    103
    now i'll reply after 12-20hrs later i'll sleep now

  7. #37
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    aah mann i am paralyzed from one side ......in my school my teacher teaches us 1980's programming and we use turbo C++ 3.0
    gonna use std::string just wait
    Just don't be surprised when Turbo-C doesn't know what a string is.

    Jim

  8. #38
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'm not really understanding what is so difficult.

    You might want to skip to the chapter that talks about using strings, or if it doesn't talk about std::string, skip to the one that talks about using objects. That will give you knowledge which you need to start using objects and start using the std::string class. If your book doesn't talk about any of this, your book needs to be a better one.

    std::string is recommended because:
    a) it is easy to use. The object contains all the information for your string, so all you need to do to modify the string is call member functions of the class.
    b) because it is easier to use, you have to understand a lot less in order to do anything constructive.
    c) C strings are only there because C++ can compile C. C++ can compile C because that played a part in making C++ popular way back in the 70s.

    how am i supposed to initilize these string variables?
    You can initialize a string by calling a constructor.
    Code:
    std::string s = "Notice the syntax here, which works because the string I want to use is the constructors only argument.";
    std::string s2("You can also initialize a substring, for example");
    srd::string substring(s2, 23);
    A useful tutorial is here. A useful reference is here. Programmers, more than anything else, do a lot of reading. In my experience, being capable of doing research is more important than actually trying to remember everything, like someone who is "perfect".
    Last edited by whiteflags; 05-14-2013 at 01:45 PM.

  9. #39
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Move this
    Code:
    srand(time(NULL));
    from the update function to somewhere at the top of main().

    And no, perfection is very much out of reach here. One first has to define the criteria for perfection itself, which would take a lot longer than revising the C++ standard.
    You likely won't even attain perfection on one aspect of a program, let alone all of them.
    Some areas where you are a long way from perfection:
    Lack of whitespace.
    Length of main.
    Lack of factoring methods out like that which generates strings like 1st, 2nd etc.
    Lack of using constants.
    Lack of commenting.
    Lack of const-correctness.
    Lack of portability (not tested on other compilers).

    Ah crap I gotta go to work, don't have time to sit and write this list all day...
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #40
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by Mukul Kumar View Post
    @elysia one day i'll show you that nothing is impossible
    Perfection is impossible - and that's one of the great joys of programming. No matter how long you've been doing this, whether it be five days or five decades, there's always something new that you can learn and that can sharpen your skill even more.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  11. #41
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    @elysia one day i'll show you that nothing is impossible
    O_o

    Well, we will all be waiting, but I'll tell you something that will put you on the right path.

    Plus don't tell me that what i am ....tell me what should i do!!!
    None of us here is perfect (*) so you will surely not dream the impossible dream by building your programs piecemeal from the input you receive.

    Soma

    (*) Yep, not even Salem is perfect, and I'm pretty sure he/she is a robot.

  12. #42
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by whiteflags View Post
    C++ can compile C because that played a part in making C++ popular way back in the 70s.
    Stroustrup started working on the language in 1979, and the C++ name was actually adopted in 1983. Your point remains valid, even if you were off by a decade or so.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  13. #43
    Registered User
    Join Date
    Apr 2013
    Posts
    103
    oh k now all warnings are gone
    please check out my code
    Code:
    #include<iostream>
    #include<conio.h>//for getch()
    #include<stdlib.h>//for system("")
    #include<time.h>//for time(NULL)
    #include<string.h>//for strcpy()
    class Bowler{//******************************class bowler***********************
        int overs,povers,maidovers,runsgiv,wictaken;
        public:
            Bowler()
            {
                povers=0;
                maidovers=0;
                runsgiv=0;
                wictaken=0;
            }
            void disp()//to display the status of bowler
            {
                std::cout<<"\n\t   BOWLER\n";
                std::cout<<"\novers played       : "<<povers;
                std::cout<<"\nmaiden overs       : "<<maidovers;
                std::cout<<"\nruns given         : "<<runsgiv;
                std::cout<<"\nwickets taken      : "<<wictaken;
            }
            int give(std::string choose)
            {
                if(choose=="overs")
                {
                    return(overs);
                }
                else if(choose=="maidovers")
                {
                    return(maidovers);
                }
                else if(choose=="runsgiv")
                {
                    return(runsgiv);
                }
                else if(choose=="wictaken")
                {
                    return(wictaken);
                }
            }
            void over()//to get overs
            {
                std::cout<<"enter number of overs : ";
                std::cin>>overs;
            }
            void update(int run,int iout,int j,int balls[])//to update the status of bowler
            {
                int l=0,m;
                if(j==5)//if it is last ball of over
                {
                    povers++;
                    for(m=0;m<6;m++)
                    {
                        if(balls[m]==0)
                            l++;
                    }
                }
                if(l==6)//only if l==6
                {
                    maidovers++;//if all ball[] are 0 then incriment
                    for(m=0;m<6;m++)
                    {
                        balls[m]=1;//ball[] are initialized with any value other than 0
                    }
                }
                if(run==5)//if the 5 comes then no runs given (refer to line 145)
                    runsgiv=0;
                else
                    runsgiv=run;
                if(iout==0)//if out then wickets incriment in bowler's status 
                {
                    wictaken++;
                }
            }
    };
    
    class Batsman{//********************class Batsman*******************************
        int runs,iout,run,mod;
        std::string name,mout;
        static int count;
        public:
            Batsman()//all the 11 batsman should be not out
            {
                iout=1;
            }
            void updatecount()//to incriment static count
            {
                count++;
            }
            int give(std::string choose)//will be used in scoreboard() to get any value
            {
                if(choose=="iout")
                {
                    return(iout);
                }
                else if(choose=="runs")
                {
                    return(runs);
                }
                else if(choose=="run")
                {
                    return(run);
                }
            }
            void init(int &a)//initialize the next batsman with name
            {
                system("cls");
                if(a!=0)
                {
                    std::cout<<"the batsman #"<<count-1<<" is out now out\n";
                }
                std::cout<<"enter name of batsman #"<<count<<" : ";
                std::cin>>name;
                iout++;
                runs=0;
            }
            void disp()//to display the data for batsman's performance
            {
                std::cout<<"\n\t   BATSMAN #"<<count<<"\n";
                std::cout<<"\nName               : "<<name;
                std::cout<<"\nruns made          : "<<runs;
                std::cout<<"\nstatus             : ";
                switch(mod)
                {
                    case 0:mout="Catch-out";break;
                    case 1:mout="Run-Out";break;
                    case 2:mout="Wicket";break;
                    case 3:mout="Bold";break;
                    default :mout="Hit-Wicket";break;
                }
                if(iout==0)//if out then display out and its reason(which is random)
                {
                    std::cout<<"Out\n";
                    std::cout<<"mode by which out  : "<<mout;
                }
                else if(iout==1)//if check if out variable is 1 then display not out
                {
                    std::cout<<"Not out";
                }
            }
            void update()
            {
                run=rand()%7;//any random number from 0-6
                if(run==5)//the number 5 is for out
                {
                    iout--;//the variable to check if out is 0
                    mod=rand()%5;//mod is now assigned with some random value
                                 //refer to line 125
                }
                else//if not out then add runs in this ball to total runs
                {
                    runs+=run;
                }
            }
    };
    
    int Batsman :: count=1;
    
    void scoreboard()//scoreboard function to reduce main()
    {
        Bowler ball;//one bowler
        Batsman bats[11];//11 batsman
        ball.over();//to enter the numbet of overs in this match
        system("cls");
        std::string place;//to give place on line 200
        int ov,i=0,balls[6],j,l=0,k;//k for loop of overs,j for loop of balls,
                                    //l for condition if out,i is subscript of bats
                                    //of class Batsman, and ov to fetch overs
        bats[i].init(i);//initialize 1st batsman
        std::cout<<"the score board is ready to be used\n";
        getch();
        system("cls");
        ov=(ball.give("overs"));//get overs
        for(k=0;k<ov;k++)//loop for overs
        {
            if(l)//the condition inside the loop of balls of out ,if l++ then break
            {
                break;
            }
            for(j=0;j<6;j++)//loop for 6 balls in each over
            {
                //updating***
                bats[i].update();
                ball.update(bats[i].give("run"),bats[i].give("iout"),j,balls);
                //recording the run given in last ball
                balls[j]=bats[i].give("run");
                //to give st,nd,rd or th to place variable
                switch((k+1))
                {
                    case 1:place="st";
                    case 2:place="nd";
                    case 3:place="rd";
                    default :place="th";
                }
                if(j==6)
                {
                    system("cls");
                    std::cout<<"the "<<k+1<<place<<" over is finished";
                    std::cout<<"\n";
                }
                //displaying the result of last ball
                bats[i].disp();
                ball.disp();
                std::cout<<"\n\nnow the bowler throws the ball...\n";
                std::cout<<"press enter to update the score board...";
                getch();
                system("cls");
                if(!bats[i].give("iout"))//to check if current batsman is out
                {
                    if(i<=10)//if not the last batsman
                    {
                        i++;
                    }
                    else if (i>10)//if the last one
                    {
                        l++;
                        break;
                    }
                    bats[i].updatecount();//updating count
                    bats[i].init(i);//initialize next batsman and getting his name
                }
            }//end of loop of balls
        }//end of loop of overs
        system("cls");
        if(k==ov)//check of all overs finished
        {
            std::cout<<"overs finished";
        }
        else if(l)//check for all out
        {
            std::cout<<"all batsmen out !";
        }
        //display of end result
        std::cout<<"\nend result :-";
        bats[i].disp();
        ball.disp();
    }
    
    int main()
    {
        srand(time(NULL));
        scoreboard();
        getch();
        return 0;
    }//end of main()
    Last edited by Mukul Kumar; 05-15-2013 at 12:07 PM.

  14. #44
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Very good. Now fix those pesky C arrays: https://sourceforge.net/apps/mediawi..._arrays_in_Cpp
    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.

  15. #45
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Good effort.
    Now get rid of conio.h, getch and ststem("cls"), they are only making your program cumbersome to use.

    Btw, have you learned about enums yet ?
    If not, look them up.
    It'll simplify your code a great deal.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. game development & programming skill required
    By newprog82 in forum Game Programming
    Replies: 2
    Last Post: 11-02-2010, 11:29 AM
  2. API Programming in network level
    By rchiu5hk in forum C++ Programming
    Replies: 0
    Last Post: 09-07-2009, 10:42 PM
  3. low level programming book
    By CChakra in forum C Programming
    Replies: 5
    Last Post: 09-08-2008, 01:23 PM
  4. Ways to better programming skill
    By Unferth in forum C++ Programming
    Replies: 9
    Last Post: 09-23-2003, 05:35 PM
  5. Low level programming question
    By phooey in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2003, 08:59 AM