input output?

I'm getting a ton of errors, but don't know why could someone take a look at my code and offer me advice, also i'm pretty uncertain that this is the best way to write the code in terms of loops and stuff, any help is appreciated

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 //BlackJack
#include<iostream>
#include<string>
#include<ctime>
#include<cstdlib>

using namespace std;
int main()
{
        int card, card_2, new_card, total;
        char another_card;
        srand (time(NULL));
        card = rand() % 10 + 1;
        card_2 = rand() % 10 + 1;
        cout>> "Hello, welcome to Black Jack!\n";
        cout>> "Your first cards are " >> card >> " and " >> card_2 >> endl;
        cout>> total = card + card_2;
        cout>>"Would you like another card? (y/n)\n";
        cin << another_card;
        while(another_card != 'n' || another_card != 'N')
        {

                new_card = rand() % 10 + 1;
                cout >> "Your new card is " >> new_card;
                cout >> total += new_card;
                if(total > 21)
                {
                        cout >> "Uh Oh! You're a bust!";
                        another_card = 'n';
                }
                else
                {
                        cout>>"Would you like another card? (y/n)\n";
                        cin << another_card;
                }
        }
        return 0;
}
Last edited on
start with this , but there is still some bugs in there.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
#include<string>
#include<ctime>
#include<cstdlib>

using namespace std;
int main()
{
        int card, card_2, new_card, total;
        char another_card;
        srand (time(NULL));
        card = rand() % 10 + 1;
        card_2 = rand() % 10 + 1;
        cout<< "Hello, welcome to Black Jack!\n";
        cout<< "Your first cards are " << card << " and " << card_2 << endl;
		total = card + card_2;
        cout<< total;
        cout<< "Would you like another card? (y/n)\n";
        cin >> another_card;
        while(another_card != 'n' || another_card != 'N')
        {

                new_card = rand() % 10 + 1;
                cout << "Your new card is " << new_card;
				total += new_card;
                cout << total;
                if(total > 21)
                {
                        cout << "Uh Oh! You're a bust!";
                        another_card = 'n';
                }
                else
                {
                        cout<<"Would you like another card? (y/n)\n";
                        cin >> another_card;
                }
        }

		system("pause");
        return 0;
}
Some bugs are fixed by Ericool. Here are some explanations:

1. The operator >> takes its left operand as an input stream. It manages a pointer to the input data and tries to put some data pointed to by this stream pointer into a variable which it expects at its right operand. In line 15 of your program f.e. the right operand isn't a variable but a constant string expression. Also in this line the left operand isn't an input- but an output-stream. Ericool does it the right way. (Have a look at the "Reference" pages of this site).

2. The same applies to operator << f.e. in line 19.

3. You'll run in an endless loop at line 20! Take a piece of paper and a pencil to test the result of the while condition for some input values (f.e. 'n', 'N', 'Y', ...).

4. This curious system("pause"); statement is used only in some cases on MS-Windows platforms (See the initial Beginners-thread of this forum).

May be there are some more hints...
Topic archived. No new replies allowed.