Table tennis scoring system

This is a discussion on Table tennis scoring system within the C++ Programming forums, part of the General Programming Boards category; hi guys, i wanted to write a piece of code that can be used for table tennis matches scoring. The ...

  1. #1
    C++Pandit
    Join Date
    Jul 2008
    Posts
    49

    Table tennis scoring system

    hi guys, i wanted to write a piece of code that can be used for table tennis matches scoring.
    The rules is
    1>a person wins a match if he reaches 11 points with a difference of 2,
    2>if it is10-10 the player who has a two point advantage wins.
    i have come up with this code
    Code:
    #include<iostream>
    #include<conio.h>
    #include<string>
    #include<iomanip>
    using namespace std;
    int main()
    {
        int point;
        int player1=0,player2=0;
        string name1,name2;
        cout<<"Enter first player name ";
        cin>>name1;
        cout<<"Enter the second player name ";
        cin>>name2;
        system("cls");
        
        cout<<"If "<<name1<<" wins a point press 1";
        cout<<"\nIf "<<name2<<" wins a point press 2";
        getch();
        
        system("cls");
        cout<<"Scoreboard"<<endl;
        cout<<name1<<setw(5)<<name2<<endl;
        cout<<player1<<setw(5)<<player2<<endl;
        getch();
        
        getch();
        system("cls");
        while((player1==11&&player2<10)||(player2==11&&player2<10));
        {
        cout<<"Enter point won by ";
        cin>>point;
        if(point==1)
        player1++;
        if(point==2)
        player2++;   
        cout<<"Scoreboard"<<endl;
        cout<<name1<<setw(5)<<name2<<endl;
        cout<<player1<<setw(5)<<player2<<endl;
        }
    getch();
    }
    what should i do further?
    I would rather be hated for who I am than be loved for who I am not!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably should remove all those "extra" getch() that you have in there.

    Also, I see nothing in your code to cope with the "first to get two points ahead after 11 points".

    Edit:
    Code:
    (player2==11&&player2<10)
    can never be true - as player2 is EITHER 11 or less than 10, never both at the same time [1]

    [1] Obviously, in theory we could read different values for each of those tests if there is a second thread changing the player2 values - but there is no sign of threads in the above code, and the "hole" to achieve that would be very small indeed [in fact, it's unlikely that the compiler given the above code would re-read player2 before comparing with 10 anyways].

    --
    Mats
    Last edited by matsp; 10-20-2008 at 06:10 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++Pandit
    Join Date
    Jul 2008
    Posts
    49

    correct code

    Code:
    #include<iostream>
    #include<conio.h>
    #include<string>
    #include<iomanip>
    using namespace std;
    int main()
    {
        int point;
        int player1=0,player2=0;
        string name1,name2;
        cout<<"Enter first player name ";
        cin>>name1;
        cout<<"Enter the second player name ";
        cin>>name2;
        system("cls");
        
        cout<<"If "<<name1<<" wins a point press 1";
        cout<<"\nIf "<<name2<<" wins a point press 2";
        getch();
        
        system("cls");
        cout<<"Scoreboard"<<endl;
        cout<<name1<<setw(5)<<name2<<endl;
        cout<<player1<<setw(5)<<player2<<endl;
        getch();
        
        getch();
        system("cls");
        while((player1==11&&player2<10)||(player2==11&&player1<10));
        {
        cout<<"Enter point won by ";
        cin>>point;
        if(point==1)
        player1++;
        if(point==2)
        player2++;   
        cout<<"Scoreboard"<<endl;
        cout<<name1<<setw(5)<<name2<<endl;
        cout<<player1<<setw(5)<<player2<<endl;
        }
    getch();
    }
    sorry for the silly mistake
    and anyway the program terminates after i enter the first point!
    is there anything else i should add or subtract to get a good output
    I would rather be hated for who I am than be loved for who I am not!

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Better (correct) Indentation would be good.

    If your code exits when you enter the first point, it's OBVIOUSLY not correct, so you probably should fix that.

    You also should fix the problem with "less than two point difference when first player reaches 11". If you do not cope with this, your program does not satisfy ALL of the requirements, and that would certainly reduce your total "score" for the program.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Duplicate at Hash Table
    By DarrenY in forum C Programming
    Replies: 1
    Last Post: 05-10-2007, 02:31 AM
  2. MUD Concept Question
    By mrpickle in forum Game Programming
    Replies: 3
    Last Post: 12-01-2003, 11:45 AM
  3. progarm doesnt compile
    By kashifk in forum Linux Programming
    Replies: 2
    Last Post: 10-25-2003, 05:54 PM
  4. extra word printing
    By kashifk in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2003, 04:03 PM
  5. inputting words from a file
    By kashifk in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2003, 07:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21