Server Help

Non-Subspace Related Coding - Bowling! (Calculating the score for a particular frame)

Yoink - Sun Oct 08, 2006 11:44 am
Post subject: Bowling! (Calculating the score for a particular frame)
Here's the program we've been assigned (yeah yeah go ahead and laugh at me, you elitists =[ ).

Anywho, I can't find any errors with it (if you find any sets of numbers that don't work, go ahead and tell me), but I want (aside from the lack of commenting) critique on poor style, better ways to do things - I'm pretty naive on the subject, and more than humble enough to realise that you guys should be able to tear this apart sa_tongue.gif.

Thanks for your time.
Code: Show/Hide
   #include <iostream>

using namespace std;

int main() {
    int throw1, throw2, throw3;
    int score;
   
    cout << "Please enter the score for the first throw:\n";
    cin >> throw1;
        if ( throw1 > 10 ) {
           cout << "You can't knock down more than 10 pins in one throw.\n";
           
           system("pause");
           return 1;
       
        }
   
    cout << "Please enter the score for the second throw:\n";
    cin >> throw2;
        if ( throw1 > 10 ) {
           cout << "You can't knock down more than 10 pins in one throw.\n";
           
           system("pause");
           return 1;
        }
        else if ( throw1 != 10 && throw1 + throw2 > 10 ) {
             cout << "You can't knock down more than 10 pins in one frame.\n";
             
             system("pause");
             return 1;
        }

    if ( throw1 == 10 || throw1 + throw2 == 10 ) {
         cout << "Please enter the score for the third throw:\n";
         cin >> throw3;
         
         if ( throw3 > 10 ) {
            cout << "You can't knock down more than 10 pins in one throw.\n";
           
            system("pause");
            return 1;
        }
        else if ( throw1 == 10 && throw2 != 10 && throw2 + throw3 > 10 ) {
              cout << "You can't knock down more than 10 pins in one frame.\n";
              
              system("pause");
              return 1;
        }
    }
   
    if ( throw1 == 10 || (throw1 + throw2) == 10 ) {
          score = throw1 + throw2 + throw3;
          cout << "Your score is: " << score << endl;
    }
    else {
       score = throw1 + throw2;
       cout << "Your score is: " << score << endl;
    }
   
    system("pause");
    return 1;
} // end of main()     

Smong - Sun Oct 08, 2006 1:10 pm
Post subject:
Can you briefly explain what the rules are?

This is probably preference, but I put my curly brackets at the same indentation level:
Code: Show/Hide
void func()
{
}

It also looks like they are teaching you C++, either way I also prefer to use printf and you can use scanf to read input: (as opposed to cin/cout)
Code: Show/Hide
int a;
char buf[256];

/* set initial values */
a = 0;
buf[0] = 0;

scanf("%d %255s", &a, buf);

Finally I'm not 100% certain on this but I think there's a convention to return 0 if the program was successful, anything else is some kind of error.
CypherJF - Sun Oct 08, 2006 2:28 pm
Post subject:
if the frame doesn't include a spare or a strike then the total for the frame is that of the 2 throws.

if the strike occurs on the first throw then it counts as 10 + the next 2 throws.

if the throw results in a spare then it is 10 + the next throw.

if on the last frame (10th) if you get a strike 2 you get 2 more throws, if you get a spare you get 1 more and you calculate from there.

thats if i can recall this correctly.. haven't done manual bowling calculation in years.

also don't forget about fouls, i think it counts as a miss?
Mine GO BOOM - Sun Oct 08, 2006 3:21 pm
Post subject:
Smong wrote:
It also looks like they are teaching you C++, either way I also prefer to use printf and you can use scanf to read input: (as opposed to cin/cout)

He is learning the language. Printf/scanf are horrible ways for him to learn. Yes, I use them much more often (well, printf, scanf almost never), but he doesn't know much about the conventions of programming that it is better to just deal with cin/cout.

My only recommendation that is different from others is to use loops to read in values from the user. Example:
Code: Show/Hide
while (1)
{
    cout << "Enter value: ";
    cin >> value;

    if ((value > too_small && value < too_big) || value == special_value)
        break;

    cout << "Value is invalid, please try again.\n";
}
Or:
Code: Show/Hide
cout << "Enter value: ";
cin >> value;

while (value < too_small || value > too_big || value != special_value)
{
    cout << "Value is invalid, please try again.\n";
    cout << "Enter value: ";
    cin >> value;
}

This is because invalid userdata could have just been mistyped by accident, and re-entering all previous values because you mistyped one thing is annoying as hell. Only exit a program early if you cannot correct a mistake, such as if a system call failed or you ran out of memory.
Yoink - Sun Oct 08, 2006 7:44 pm
Post subject:
CypherJF wrote:
if the frame doesn't include a spare or a strike then the total for the frame is that of the 2 throws.

if the strike occurs on the first throw then it counts as 10 + the next 2 throws.

if the throw results in a spare then it is 10 + the next throw.

if on the last frame (10th) if you get a strike 2 you get 2 more throws, if you get a spare you get 1 more and you calculate from there.

thats if i can recall this correctly.. haven't done manual bowling calculation in years.

also don't forget about fouls, i think it counts as a miss?

it's actually simpler - there are no fouls, and it's just a single frame.

yes it's c++.

my prof mentioned something horrendous about using return 1?
Bak - Sun Oct 08, 2006 10:21 pm
Post subject:
the return 0 convention is useful when you're making shell scripts with programs that you make.

multiple returns inside functions are ugly.

The System("PAUSE") is because I assume you're using msvc and when you press "debug" to run it the console window pops up and disappears instanty when the program quits. Instead of this, use debug -> start without debugging (ctrl + f5), and it will pause on it's own when the progam terminates so you don't have to sprinkle this throughout your code. In fact, using ctrl + f5 will save, build, and run your code all at once, which is a pretty common sequence so it's worth memorizing.

Like mgb said you check for an upper bound, but never for a lower bound (if they enter less than zero).
Cyan~Fire - Mon Oct 09, 2006 10:23 am
Post subject:
You could also just call getchar() (or, I think, cin.get()?) to wait for an enter key before continuing. Make sure you flush the input ahead of time, though, because there may already be characters waiting on the input buffer.
Yoink - Mon Oct 09, 2006 11:52 am
Post subject:
I'm using Bloodshed Dev-C++.

Anyway, I e-mailed my professor about the return 1; issue and he said it was a huge no-no, so I gotta fix that (pretty sure I know how).
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group