Need a quick bit of advise. Convert Int to Char.

Coldkilla

Diamond Member
Oct 7, 2004
3,944
0
71
I just started a programming 100 course in college and they have given an assignment out that deals with a bunch of "idiot bowlers". In it, I would have to declare a variable, lets call it 'b1' for ball one. If they get a strike, a strike is 10 pins. But the scoreboard shows an X, not a 10. I have to retain this X so once the game is over, the final scoreboard shows all of the values for all of the balls thrown, excluding 10 of course because those are all strikes (there are no spares in this game, and only three frames 1,2,3).

We just learned if and else statements, so I'm assuming the program is centralizing on this concept. Also, we recently learned about prototypes and declarative statements.

The Game:

* There are three Frames. [[I'm only worried about the first two at this point]]

* In each Frame you roll a heavy ball at triangular arrangement of 10 pins in the attempt to knock them over.

* After each frame the 10 pins are reset to their triangular arrangement.

* For the first and second Frame you roll your ball at the pins and note how many you knocked over.
1. If you knocked them all over it is deemed a Strike.
2. If you have not knocked them all over then you roll your ball again, at the pins remaining, and note how many more you knocked over.
1. If you knocked the rest over it is deemed a Spare.

Example

Enter pin total for 1st ball in frame 1 : 2
Enter pin total for 2nd ball in frame 1 : 7
Enter pin total for 1st ball in frame 2 : 10
Enter pin total for 1st ball in frame 3 : 4
Enter pin total for 2nd ball in frame 3 : 6
Enter pin total for 3rd ball in frame 3 : 9

Frame Ball 1 Ball 2
------------------------------
1-------- 2------ 7
2-------- X____ -
3-------- 4------ 6
I keep getting errors when I try to make that 10 into an X, not by saying b1 = X, but I'm trying everything else I can think of and I keep coming up short.


 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,700
4,661
75
What language are you writing in? The solution could be very different in C than in Javascript, for instance.
 

Coldkilla

Diamond Member
Oct 7, 2004
3,944
0
71
I pretty sure its C++.

This is what I am doing, and I know this is wrong because if I have say 7 variables. (3 frames, 2 balls for the first two frames and 3 balls for the last frame). When I finally finish and export all of the values onto a table (like in the example), it wont work.

char strike_ten();

int main ()
{
int b1;
char strike;

//FRAME1
cout << "Enter pin total for 1st ball in frame 1 : ";
cin >> b1;
{
if (b1 == 10)
{
strike = strike_ten();
cout << strike;
}

//Ball 1 "strike" should be "b1" but "b1" is an int, not a char..
//Table:
cout << endl << endl;
cout << "Frame\t Ball 1\t Ball 2\t Ball3 \t" << endl;
cout << "------------------------------------" << endl;
cout << "1\t" << strike << endl;
cout << "2\t" << endl;
cout << "3\t" << endl;
cout << endl << endl;
}
return 0;
}

char strike_ten()
{
char strike1;
cout << "x"; // temporary (just to show me it is there).
return strike1;
}

I don't know how I would call b1 (as an "x") later when I would introduce it into the table. I haven't learned loops yet, so would I have to have seperate functions for each variable? (IE: b1, b2, b3, b4, b5, b6, b7?)
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
The rules for each frame are the same right? So it makes sense to me to do something like

main ()
{

PlayOneFrame( 1 ) ;
PlayOneFrame( 2 ) ;
PlayOneFrame( 3 ) ;

}

... but that would require passing in &b1, &b2 by reference or returning a string for the results, which you might not have learned yet.

 

Kirby

Lifer
Apr 10, 2006
12,028
2
0
Just make the input char, since 0-9, '/', and 'X' are valid characters.
 

iCyborg

Golden Member
Aug 8, 2008
1,355
63
91
Originally posted by: Coldkilla
I pretty sure its C++.

I don't know how I would call b1 (as an "x") later when I would introduce it into the table. I haven't learned loops yet, so would I have to have seperate functions for each variable? (IE: b1, b2, b3, b4, b5, b6, b7?)

That program isn't syntactically correct (3 open braces, only 2 closed?).

I'd have 7 variables too, though I would name them something like b11, b12, b21 etc. i.e. first digit is the frame, the second is the roll.
From your example, I think you have to read strikes as 10, and then for each ball just test if it's 10, print X, otherwise print the value, plus some formatting to make it pretty. I don't think you need to store 'X' into any variables.

Also, if the last 5 lines of your code is supposed to be strike_ten() function, it's printing "x" (this part is fine but you don't need a function for this), and returning a noninitialized local variable which makes no sense, nor do I see it's being used.
 

Coldkilla

Diamond Member
Oct 7, 2004
3,944
0
71
I accidentally posted an older version of my program. (Updated it above). The variable "strike" does not display in the table as "x" for some reason and I don't know why.

The rules for each frame are different, but I feel that if I understand the rules for the frames are the same with the exception of the last one - but I feel that I can easily understand it given that I understand these two.

-You can have integers be chars? I thought that wasn't possible? Or just chars to integers is impossible? Just 0-9 is possible? What about when it hits 10?
-Yea I really have no idea what DaveSimmons is talking about, sorry.

I forgot to mention he gave us some rules we have to follow:
You must write a separate function that given a ball's value as its only input parameter, inserts exactly one of the following (via the rules indicated) onto cout:

1. 'X' : if the ball's value is 10 (a strike).
2. The ball's value : if it is valid and not a strike (i.e. 0 - 9 inclusive).
3. '-' : if the ball's value is invalid or was not rolled (e.g. ball 2, if ball 1 was a strike)
 

Net

Golden Member
Aug 30, 2003
1,592
3
81
i'm guess that since its a 100 level class he doesn't ask you to check to do error checking.

assuming he doesn't want you to check if its a valid number, i.e. 0 < b1 < 10

then here is some 100 level style code in the c language

program output:

-bash-3.2$ ./aa

Enter pin total for 1st ball in frame 1: 2

Enter pin total for 2nd ball in frame 1:10

Enter pin total for 1st ball in frame 2: 2

Enter pin total for 2nd ball in frame 2:9

Enter pin total for 1st ball in frame 3: 8

Enter pin total for 2nd ball in frame 3:10

Frame Ball 1 Ball 2

1: 2 X
2: 2 9
3: 8 X

Edit: Wow, I would have thought the attached code button would make it looks nice, its all on one line lol.

I posted it here: http://rafb.net/p/GePBe971.html

and below incase it's erased by the time your read.

#include <stdio.h>

int main(){

int b1, b12, b2, b22, b3, b32;
char c1, c12, c2, c22, c3, c32;

printf ("\nEnter pin total for 1st ball in frame 1: ");
scanf ("%d", &b1);
if (b1 < 10){
printf ("\nEnter pin total for 2nd ball in frame 1:");
scanf ("%d", &b12);
if (b12 == 10)
c12 = 'X';
else
c12 = (char)b12 + '0';

}

printf ("\nEnter pin total for 1st ball in frame 2: ");
scanf ("%d", &b2);
if (b2 < 10){
printf ("\nEnter pin total for 2nd ball in frame 2:");
scanf ("%d", &b22);
if (b22 == 10)
c22 = 'X';
else
c22 = (char)b22 + '0';

}

printf ("\nEnter pin total for 1st ball in frame 3: ");
scanf ("%d", &b3);
if (b3 < 10){
printf ("\nEnter pin total for 2nd ball in frame 3:");
scanf ("%d", &b32);
if (b32 == 10)
c32 = 'X';
else
c32 = (char)b32 + '0';
}

if (b1 == 10)
c1 = 'X';
else
c1 = (char)b1 + '0';
if (b2 == 10)
c2 = 'X';
else
c2 = (char)b2 + '0';
if (b3 == 10)
c3 = 'X';
else
c3 = (char)b3 + '0';

printf ("\nFrame\t Ball 1\t Ball 2\n");
printf ("\n1:\t %c\t %c", c1, c12);
printf ("\n2:\t %c\t %c", c2, c22);
printf ("\n3:\t %c\t %c\n", c3, c32);
return 0;
}