Scrabble Bag of tiles not working
Dec 13, 2015 at 7:45pm UTC
So i am making a scrabble game and one of the first things i need to do is to create an array of tiles with a class, but the code i have when run will only output the word "test1" and then it will leave a blinking cursser there, not ending the program.
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
#ifndef Tile_H
#define Tile_H
#include <iostream>
#include <fstream>
#include<sstream>
#include<string>
using namespace std;
class tile
{
private :
int myval;//value of tile
char mylet;//letter of tile
public :
tile();
tile(int , char );
void fill(int , char );
char Getlet();
int Getval();
};
tile::tile() {
myval = 0;
mylet = '.' ;
}
tile::tile(int newval, char newchar) {
myval = newval;
mylet = newchar;
}
void tile::fill(int tileval, char tilelet) {
myval = tileval;
mylet = tilelet;
}
char tile::Getlet() {
return (mylet);
}
int tile::Getval() {
return (myval);
}
#endif
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
#ifndef BagOfTiles_H
#define BagOfTiles_H
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include "Tile.h"
using namespace std;
class BagOfTiles {
private :
tile arrTiles[100];
public :
BagOfTiles();
BagOfTiles(string);
string tostring();
};
BagOfTiles::BagOfTiles() {
for (int i = 0; i <= 100; i++) {
arrTiles[i].fill(0, '.' );
}
}
BagOfTiles::BagOfTiles(string Fname) {
char let;
int val,max,oldmax;
string tempval,tempmax;
ifstream infile;
infile.open(Fname, ios::in);
oldmax = 0;
if (!infile) {
cout << "Error opening file.\n" ;
}
else {
infile.unsetf(ios::skipws);
while (!infile.eof()) {
infile >> let;
infile >> tempval;
infile >> tempmax;
ostringstream buffer;
buffer << tempval;
istringstream convert1(buffer.str());
convert1 >> val;
buffer << tempmax;
istringstream convert2(buffer.str());
convert2 >> max;
for (int i = oldmax; i <= max; i++) {
arrTiles[i].fill(val,let);
}
oldmax = max+1;
}
infile.close();
}
}
string BagOfTiles::tostring() {
std::ostringstream buffer;
for (int i = 0; i <= 100; i++) {
buffer<< "yee" <<arrTiles[i].Getlet() << arrTiles[i].Getval();
}
return buffer.str();
}
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Scrabble.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include "Tile.h"
#include "BagOfTiles.h"
using namespace std;
int main()
{
cout << "test1" << endl;
BagOfTiles bag("scrabblevalues.txt" );
cout << bag.tostring()<<"test2" << endl;
return 0;
}
UPDATE: I have found that is it somewere in the fill code that it stops the program
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
void BagOfTiles::fill(string Fname) {
char let;
int val,max,oldmax;
string tempval,tempmax;
ifstream infile;
infile.open(Fname, ios::in);
oldmax = 0;
if (!infile) {
cout << "Error opening file.\n" ;
}
else {
infile.unsetf(ios::skipws);
while (!infile.eof()) {
infile >> let;
infile >> tempval;
infile >> tempmax;
ostringstream buffer;
buffer << tempval;
istringstream convert1(buffer.str());
convert1 >> val;
buffer << tempmax;
istringstream convert2(buffer.str());
convert2 >> max;
for (int i = oldmax; i <= max; i++) {
arrTiles[i].fill(val,let);
}
oldmax = max+1;
}
infile.close();
}
}
Last edited on Dec 13, 2015 at 8:44pm UTC
Dec 13, 2015 at 8:08pm UTC
> for (int i = 0; i <= 100; i++)
out of bounds.
Topic archived. No new replies allowed.