Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
Bowling! (Calculating the score for a particular frame)

 
Post new topic   Reply to topic Printable version
 View previous topic  PHP/SQL: Partial data :S Post :: Post Error in code.  View next topic  
Author Message
Yoink
Novice


Joined: May 23 2006
Posts: 25
Offline

PostPosted: Sun Oct 08, 2006 11:44 am    Post subject: Bowling! (Calculating the score for a particular frame) Reply to topic Reply with quote

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()     
Back to top
View users profile Send private message Add User to Ignore List
Smong
Server Help Squatter


Joined: 1043048991
Posts: 0x91E
Offline

PostPosted: Sun Oct 08, 2006 1:10 pm    Post subject: Reply to topic Reply with quote

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.
_________________
ss news
Back to top
View users profile Send private message Add User to Ignore List Visit posters website MSN Messenger
CypherJF
I gargle nitroglycerin


Gender:Gender:Male
Joined: Aug 14 2003
Posts: 2579
Location: USA
Offline

PostPosted: Sun Oct 08, 2006 2:28 pm    Post subject: Reply to topic Reply with quote

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?
_________________
Performance is often the art of cheating carefully. - James Gosling
Back to top
View users profile Send private message Add User to Ignore List
Mine GO BOOM
Hunch Hunch
What What
Hunch Hunch<br>What What


Age:36
Gender:Gender:Male
Joined: Aug 01 2002
Posts: 3606
Location: Las Vegas
Offline

PostPosted: Sun Oct 08, 2006 3:21 pm    Post subject: Reply to topic Reply with quote

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.
Back to top
View users profile Send private message Add User to Ignore List Send email
Yoink
Novice


Joined: May 23 2006
Posts: 25
Offline

PostPosted: Sun Oct 08, 2006 7:44 pm    Post subject: Reply to topic Reply with quote

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?
Back to top
View users profile Send private message Add User to Ignore List
Bak
?ls -s
0 in


Age:20
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Sun Oct 08, 2006 10:21 pm    Post subject: Reply to topic Reply with quote

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).
_________________
SubSpace Discretion: A Third Generation SubSpace Client
Back to top
View users profile Send private message Add User to Ignore List AIM Address
Cyan~Fire
I'll count you!
I'll count you!


Age:31
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Mon Oct 09, 2006 10:23 am    Post subject: Reply to topic Reply with quote

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.
_________________
This help is informational only. No representation is made or warranty given as to its content. User assumes all risk of use. Cyan~Fire assumes no responsibility for any loss or delay resulting from such use.
Wise men STILL seek Him.
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
Yoink
Novice


Joined: May 23 2006
Posts: 25
Offline

PostPosted: Mon Oct 09, 2006 11:52 am    Post subject: Reply to topic Reply with quote

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).
Back to top
View users profile Send private message Add User to Ignore List
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> Non-Subspace Related Coding All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 244 page(s) served in previous 5 minutes.

phpBB Created this page in 0.201544 seconds : 33 queries executed (24.9%): GZIP compression disabled