still need help asap ,program runs twice, dont know how to stop the loop

i think my program is running twice , im not sure why , in my data file i have "2" and my result is "Your total is 4"
why is my program running twice even though i have while(!ins.eof())

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Write a program that scores a blackjack hand
// goal is to get a score as close to 21 
// without going over 21
// going over 21 is called "busted"
// card values 2 through 10
// jack, queen, king  and ace represented by
// t , j , q , k , a

#include<iostream>// allows user input/output
#include<conio.h>
#include<fstream>//data file / result file
#include<iomanip>
#include<cmath> // math function

#define in_file "data.txt"
#define out_file "result.txt"

using namespace std;

char choice;

void blackjackhand();

int main ()

{
	blackjackhand();

}

void blackjackhand()

{
	ifstream ins; //ins as input stream
	ofstream outs; //outs as output stream
	ins.open(in_file); //open files
	outs.open(out_file);

		int value = 0;
		int total = 0;

while (!ins.eof())
{

	ins >> choice;

	switch (choice)
	{
		case '2': 
				value = 2;
				total = total + value;
				break;
		case '3':
				value = 3;
				total = total + value;
				break;
		case '4':
				value = 4;
				total = total + value;
				break;
		case '5':
				value = 5;
				total = total + value;
				break;
		case '6':
				value = 6;
				total = total + value;
				break;
		case '7':
				value = 7;
				total = total + value;
				break;
		case '8':
				value = 8;
				total = total + value;
				break;
		case '9':
				value = 9;
				total = total + value;
				break;
		case 't':
				value = 10;
				total = total + value;
				break;
		case 'j':
				value = 10;
				total = total + value;
				break;
		case 'q':
				value = 10;
				total = total + value;
				break;
		case 'k':
				value = 10;
				total = total + value;
				break;
		case 'a':
				value = 11;
				total = total + value;

					if (total > 21)
					{
						value = 1; 
					}
				break;

	} // end switch statement


   }
		if (total <= 21)
		{
	outs << "Your total is " << total << endl;
		}
	else if (total > 21)
		{
		outs << "Busted" << endl;
		}
}
Last edited on
The problem occurs in case of an eof on line 45: choice is not changed, so switch does the same as the last time.

To avoid this change this:
1
2
3
4
while (!ins.eof())
{

	ins >> choice;
To
1
2
while (ins >> choice)
{
if i change the code to this:

1
2
3
4
5
6
7
8
9
10
11
	int value = 0;
	int total = 0;



while (ins >> choice)
{

	switch (choice)
	{
		case '2': 


with the input data:
2 5
3

my result is:
Your total is 10
Your total is 10

so now it adds the numbers correctly but i need it to read the lines separately so the first line would be 8 and the second line would be 3
so if you want to read an entire line you need to use getline and stringstream:

http://www.cplusplus.com/reference/string/string/getline/?kw=getline
http://www.cplusplus.com/reference/sstream/stringstream/?kw=stringstream

like so:
1
2
3
4
5
6
7
8
 string line;
while (getline(ins, line))
{
  stringstream ss(line);
while (ss >> choice)
{

	switch (choice)

Topic archived. No new replies allowed.