Printing smileys

Hey I'm trying to get it to print numbers from this file and its printing weird smiley faces instead. This is all thats in the file:

9 0
3 5
6 1
3 6
8 1
5 3
2 5
8 0
7 1
8 1 0


I want it to print out "9-0 3-5 6-1" etc... but its printing weird black and white smileys in the place of the numbers. There are probably other errors in the code but this it the problem I'm worried about. Here's the code:

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
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

const int NUMSCORES=21;
int score[NUMSCORES];
ifstream scores;
string scorer(int score[NUMSCORES], int NUMSCORES);

int main()
{
	char filename[20];
	
	cout << "Welcome to Bowling Scorer!" << endl
		<< "Please enter the name of a score file: ";
	cin >> filename;
	scores.open(filename);
	if(scores.fail()){
		perror(filename);
		exit(1);
	}
	cout << scorer(score, NUMSCORES);
	return 0;
}

string scorer(int score[], int NUMSCORES){
	int i;
	string game = "";
	for(i=0;i<NUMSCORES;i++){
		while(scores >> score[i]){
			game+=(2*1);
			game+="-";
			game+=(2*i+1);
			game+="\t";
		}
	}
	return game;
}


Thanks!
while(scores >> score[i])
I've never seen it done like that. Does it really work? EDIT: I couldn't get it to compile. Could be C++11 I suppose.

To answer your question:
When you do something like this: game += (2*1); you are actually just adding a character to the string equivalent to an ascii code.

See:
http://cplusplus.com/doc/ascii/

If you want to output a number and not the ascii equivalent of that number then use a stringstream instead. This will format integers, floats, anything into a string automatically:
1
2
3
4
5
6
7
8
9
10
string scorer(int score[], int NUMSCORES){
    stringstream game;
    for(int i=0;i<NUMSCORES;i++){
        game << score[i*2];
        game << "-";
        game <<	score[i*2+1];
        game << "\t";
    }
    return game.str();
}


Edit: I forgot to mention. Replace #include <string> with #include <stringstream>
Last edited on
You don't want to insert integers to a string (unless it's the integer ascii code of the character you want to stick in the string)

1
2
  std::string foo;
  foo += (8*4); // 32 which is ascii for space character, not "32" 


Look into stringstreams.
Alright thanks, that clears it up.
Topic archived. No new replies allowed.