DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Adam Guest

    Comletly stuck need help!!


    hey i am writing a blackjack program for my cs class and everytime i test
    this i get ridiculous answers like in the 5000's. i cant understand why since
    it is just arithmatic....here is the code...(also any other tips or errors
    i have in here..if you could point them out that would be great)



    #include <iostream.h>
    #include <iomanip.h>
    #include <stdlib.h>
    #include <time.h>

    int main()
    {
    int hit1, hit2, hit3, hit4, hit5, total1, total2;

    cout << "please enter in your cards with an ace for 1 and picture cards
    as 10" << endl;
    cout << "Enter your first card: ";
    cin >> hit1;
    cout << "Enter your second card: ";
    cin >> hit2;

    total1 = hit1 + hit2;
    total2 = total1 + hit3;

    if(total1 < 16 ) {
    cout << "you must take another hit" << endl;
    cout << "please input your next card: ";
    cin >> hit3;
    }
    else {
    if((total1 > 16) || (total1 < 21)) {
    cout << "very good you got a " << total1 << endl;
    }
    else {
    if(total2 < 16) {
    cout << "you must take another hit" << endl;
    cout << "please input your next card: ";
    cin >> hit4;
    }
    else {
    if(total2 > 21) {
    cout << "you lose, here is your final score: " << total2 << endl;
    }
    else {
    if((total2 > 16) || (total2 < 21)) {
    cout << "very good you got a " << total2 << endl;
    }
    else {
    }
    }
    }
    }
    }

    return 0;
    }

  2. #2
    Danny Kalev Guest

    Re: Comletly stuck need help!!

    you're using uninitialized variables.

    Instead of
    > int hit1, hit2, hit3, hit4, hit5, total1, total2;

    Try
    int hit1=0, hit2=0, hit3=0...

    Danny

    Adam wrote:
    >
    > hey i am writing a blackjack program for my cs class and everytime i test
    > this i get ridiculous answers like in the 5000's. i cant understand why since
    > it is just arithmatic....here is the code...(also any other tips or errors
    > i have in here..if you could point them out that would be great)
    >
    > #include <iostream.h>
    > #include <iomanip.h>
    > #include <stdlib.h>
    > #include <time.h>
    >
    > int main()
    > {
    > int hit1, hit2, hit3, hit4, hit5, total1, total2;
    >
    > cout << "please enter in your cards with an ace for 1 and picture cards
    > as 10" << endl;
    > cout << "Enter your first card: ";
    > cin >> hit1;
    > cout << "Enter your second card: ";
    > cin >> hit2;
    >
    > total1 = hit1 + hit2;
    > total2 = total1 + hit3;
    >
    > if(total1 < 16 ) {
    > cout << "you must take another hit" << endl;
    > cout << "please input your next card: ";
    > cin >> hit3;
    > }
    > else {
    > if((total1 > 16) || (total1 < 21)) {
    > cout << "very good you got a " << total1 << endl;
    > }
    > else {
    > if(total2 < 16) {
    > cout << "you must take another hit" << endl;
    > cout << "please input your next card: ";
    > cin >> hit4;
    > }
    > else {
    > if(total2 > 21) {
    > cout << "you lose, here is your final score: " << total2 << endl;
    > }
    > else {
    > if((total2 > 16) || (total2 < 21)) {
    > cout << "very good you got a " << total2 << endl;
    > }
    > else {
    > }
    > }
    > }
    > }
    > }
    >
    > return 0;
    > }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links