We have to make a program that calculates the score of a bowling game, here's my code, but it isn't working right here's an input that was given to us along with the scores, my code is at the bottom:
1----2----3----4----5----6----7----8----9----10
9\__-\___X___X__62__7\__8\___X___9-__9\X
10--30---56---74---82--100-120-139-148-168
What do I need to do to fix it or that?
EDIT: No I don't have the 10th frame setup yet, I wanna get these 9 frames working, numbers 0 to 9 work but X \ - don't
1----2----3----4----5----6----7----8----9----10
9\__-\___X___X__62__7\__8\___X___9-__9\X
10--30---56---74---82--100-120-139-148-168
Code:
//Frame.h
#ifndef __FRAME_H_
#define __FRAME_H_
#include <iostream>
using namespace std;
struct Score
{
int amount;
char symbol;
};
Score score[16] = {
{0, '0'},
{1, '1'},
{2, '2'},
{3, '3'},
{4, '4'},
{5, '5'},
{6, '6'},
{7, '7'},
{8, '8'},
{9, '9'},
{10, 'X'},
{10, 'T'},
{0, 'F'},
{0, 'G'},
{0, '-'},
{10, '\\'} };
class Frame
{
private:
int numBowls;
char bowl[3];
int scoreBowl;
public:
Frame(int nBowls) { numBowls = nBowls; scoreBowl = 0; };
void calculateScore(Frame aFrame, Frame bFrame);
void setBowls(char, char, char);
int getScore();
};
void Frame::calculateScore(Frame aFrame, Frame bFrame)
{
Frame tempFrame = Frame(2);
tempFrame.setBowls('0','0', '0');
int x = 0;
int y = 0;
for(x = 0; x <= 15; x++)
{
for(y = 0; y < numBowls; y++)
{
if((bowl[y] == score[x].symbol) && (bowl[y+1] != '\\'))
{
scoreBowl = score[x].amount;
}else if(bowl[y+1] == '\\')
{
scoreBowl = 10;
aFrame.calculateScore(tempFrame, tempFrame);
scoreBowl += aFrame.getScore();
// cout << scoreBowl << "\t\t";
y = numBowls;
x = 15;
}
else if((bowl[y] == score[x].symbol) && (score[x].symbol == 'X') )
{
aFrame.calculateScore(tempFrame, tempFrame);
bFrame.calculateScore(tempFrame, tempFrame);
//cout << bFrame.bowl << endl;
scoreBowl += aFrame.getScore();
scoreBowl += bFrame.getScore();
y = numBowls;
x = 15;
}
else if((bowl[y] == score[x].symbol) && (score[x].symbol == '\\'))
{
aFrame.calculateScore(tempFrame, tempFrame);
scoreBowl += aFrame.getScore();
y = numBowls;
x = 15;
}
}
}
}
void Frame::setBowls(char a, char b, char c)
{
if(numBowls == 2)
{
bowl[0] = a;
bowl[1] = b;
}
if(numBowls == 3)
{
bowl[0] = a;
bowl[1] = b;
bowl[2] = c;
}
}
int Frame::getScore()
{
return scoreBowl;
}
#endif
Code:
//main.cpp
#include <iostream>
#include "frame.h"
using namespace std;
int main (int argc, char * const argv[]) {
char bowling[22];
int score = 0;
cout << "Please enter bowling scores without spaces in the form of 0-9, T, F, X, \, -: ";
cin >> bowling;
Frame frm[9] = Frame(2);
int y = 0;
for(int x = 0; x < 22; x++)
{
if(y < 9){
for(y = 0; y < 10; y++)
{
if(bowling[x] == 'X')
{
frm[y].setBowls(bowling[x], '0', '0');
//cout << bowling[x]<< "\t" << bowling[x+1] << "\t" << y << endl;
x += 1;
}else
{
//cout << bowling[x] << "\t" << bowling[x+1] << "\t" << y << endl;
frm[y].setBowls(bowling[x], bowling[x+1], '0');
x += 1;
}
}
}
}
// frm[0].calculateScore(frm[1], frm[2]);
for(int z = 0; z < 9; z++)
{
if(z <= 6)
{
frm[z].calculateScore(frm[z+1], frm[z+2]);
}else if(z == 7)
{
frm[z].calculateScore(frm[z+1], NULL);
}else
{
frm[z].calculateScore(NULL, NULL);
}
}
// frm[1].calculateScore(frm[2], NULL);
// frm[2].calculateScore(NULL, NULL);
for(int a = 0; a < 9; a++)
{
score += frm[a].getScore();
cout << score << "\t";
}
return 0;
}
What do I need to do to fix it or that?
EDIT: No I don't have the 10th frame setup yet, I wanna get these 9 frames working, numbers 0 to 9 work but X \ - don't