How would I convert a integer output to a character? [duplicate]
up vote
-3
down vote
favorite
This question already has an answer here:
Output single character in C
4 answers
I'm trying to code a simple fruit slot machine, where it outputs 3 random values and then if there are two or more matching values, then the user "wins".
The problem I have is that when I output the 3 reels, when I set the values of the array to letters, it outputs numbers such as 99, which I presume are ASCII values of the letters.
Below is the code I currently have:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
srand (time(NULL));
int i;
int reels;
char images[4] = {'b', 'o', 'c', 'h'};
for (int reels = 1; reels < 4; reels++)
{
int reelNumber = rand() % 4;
int reelValue = images[reelNumber];
printf("And the winnings for reel %d is: %dn", reels, reelValue);
}
return 0;
}
All help is appreciated,
Thanks.
c
marked as duplicate by mkrieger1, gsamaras
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 at 13:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
-3
down vote
favorite
This question already has an answer here:
Output single character in C
4 answers
I'm trying to code a simple fruit slot machine, where it outputs 3 random values and then if there are two or more matching values, then the user "wins".
The problem I have is that when I output the 3 reels, when I set the values of the array to letters, it outputs numbers such as 99, which I presume are ASCII values of the letters.
Below is the code I currently have:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
srand (time(NULL));
int i;
int reels;
char images[4] = {'b', 'o', 'c', 'h'};
for (int reels = 1; reels < 4; reels++)
{
int reelNumber = rand() % 4;
int reelValue = images[reelNumber];
printf("And the winnings for reel %d is: %dn", reels, reelValue);
}
return 0;
}
All help is appreciated,
Thanks.
c
marked as duplicate by mkrieger1, gsamaras
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 at 13:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Welcome to SO. Please add (edit) missing relevant information: What output do you get and what do you expect (copy&paste of real results)
– Gerhardh
Nov 13 at 12:41
2
Did you check available format specifiers forprintf
? What about%c
?
– Gerhardh
Nov 13 at 12:41
2
MakereelValue
a char and print using%c
.
– Sourav Ghosh
Nov 13 at 12:42
add a comment |
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
This question already has an answer here:
Output single character in C
4 answers
I'm trying to code a simple fruit slot machine, where it outputs 3 random values and then if there are two or more matching values, then the user "wins".
The problem I have is that when I output the 3 reels, when I set the values of the array to letters, it outputs numbers such as 99, which I presume are ASCII values of the letters.
Below is the code I currently have:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
srand (time(NULL));
int i;
int reels;
char images[4] = {'b', 'o', 'c', 'h'};
for (int reels = 1; reels < 4; reels++)
{
int reelNumber = rand() % 4;
int reelValue = images[reelNumber];
printf("And the winnings for reel %d is: %dn", reels, reelValue);
}
return 0;
}
All help is appreciated,
Thanks.
c
This question already has an answer here:
Output single character in C
4 answers
I'm trying to code a simple fruit slot machine, where it outputs 3 random values and then if there are two or more matching values, then the user "wins".
The problem I have is that when I output the 3 reels, when I set the values of the array to letters, it outputs numbers such as 99, which I presume are ASCII values of the letters.
Below is the code I currently have:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
srand (time(NULL));
int i;
int reels;
char images[4] = {'b', 'o', 'c', 'h'};
for (int reels = 1; reels < 4; reels++)
{
int reelNumber = rand() % 4;
int reelValue = images[reelNumber];
printf("And the winnings for reel %d is: %dn", reels, reelValue);
}
return 0;
}
All help is appreciated,
Thanks.
This question already has an answer here:
Output single character in C
4 answers
c
c
asked Nov 13 at 12:39
Jack H
11
11
marked as duplicate by mkrieger1, gsamaras
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 at 13:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by mkrieger1, gsamaras
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 at 13:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Welcome to SO. Please add (edit) missing relevant information: What output do you get and what do you expect (copy&paste of real results)
– Gerhardh
Nov 13 at 12:41
2
Did you check available format specifiers forprintf
? What about%c
?
– Gerhardh
Nov 13 at 12:41
2
MakereelValue
a char and print using%c
.
– Sourav Ghosh
Nov 13 at 12:42
add a comment |
1
Welcome to SO. Please add (edit) missing relevant information: What output do you get and what do you expect (copy&paste of real results)
– Gerhardh
Nov 13 at 12:41
2
Did you check available format specifiers forprintf
? What about%c
?
– Gerhardh
Nov 13 at 12:41
2
MakereelValue
a char and print using%c
.
– Sourav Ghosh
Nov 13 at 12:42
1
1
Welcome to SO. Please add (edit) missing relevant information: What output do you get and what do you expect (copy&paste of real results)
– Gerhardh
Nov 13 at 12:41
Welcome to SO. Please add (edit) missing relevant information: What output do you get and what do you expect (copy&paste of real results)
– Gerhardh
Nov 13 at 12:41
2
2
Did you check available format specifiers for
printf
? What about %c
?– Gerhardh
Nov 13 at 12:41
Did you check available format specifiers for
printf
? What about %c
?– Gerhardh
Nov 13 at 12:41
2
2
Make
reelValue
a char and print using %c
.– Sourav Ghosh
Nov 13 at 12:42
Make
reelValue
a char and print using %c
.– Sourav Ghosh
Nov 13 at 12:42
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
In the printf function your using the %d substitution for reelValue and %d is for decimal types; if you use %c instead the program will print the character instead of the number.
2
The array is accessed starting at 0. What is starting at 1 is the number of the reel which ist not used to access an array. No risk of out of bounds accesses.
– Gerhardh
Nov 13 at 12:52
Ah, yes you're correct the modulus 4 takes care of it.
– EricH
Nov 13 at 13:10
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
In the printf function your using the %d substitution for reelValue and %d is for decimal types; if you use %c instead the program will print the character instead of the number.
2
The array is accessed starting at 0. What is starting at 1 is the number of the reel which ist not used to access an array. No risk of out of bounds accesses.
– Gerhardh
Nov 13 at 12:52
Ah, yes you're correct the modulus 4 takes care of it.
– EricH
Nov 13 at 13:10
add a comment |
up vote
0
down vote
In the printf function your using the %d substitution for reelValue and %d is for decimal types; if you use %c instead the program will print the character instead of the number.
2
The array is accessed starting at 0. What is starting at 1 is the number of the reel which ist not used to access an array. No risk of out of bounds accesses.
– Gerhardh
Nov 13 at 12:52
Ah, yes you're correct the modulus 4 takes care of it.
– EricH
Nov 13 at 13:10
add a comment |
up vote
0
down vote
up vote
0
down vote
In the printf function your using the %d substitution for reelValue and %d is for decimal types; if you use %c instead the program will print the character instead of the number.
In the printf function your using the %d substitution for reelValue and %d is for decimal types; if you use %c instead the program will print the character instead of the number.
edited Nov 13 at 13:10
answered Nov 13 at 12:46
EricH
12
12
2
The array is accessed starting at 0. What is starting at 1 is the number of the reel which ist not used to access an array. No risk of out of bounds accesses.
– Gerhardh
Nov 13 at 12:52
Ah, yes you're correct the modulus 4 takes care of it.
– EricH
Nov 13 at 13:10
add a comment |
2
The array is accessed starting at 0. What is starting at 1 is the number of the reel which ist not used to access an array. No risk of out of bounds accesses.
– Gerhardh
Nov 13 at 12:52
Ah, yes you're correct the modulus 4 takes care of it.
– EricH
Nov 13 at 13:10
2
2
The array is accessed starting at 0. What is starting at 1 is the number of the reel which ist not used to access an array. No risk of out of bounds accesses.
– Gerhardh
Nov 13 at 12:52
The array is accessed starting at 0. What is starting at 1 is the number of the reel which ist not used to access an array. No risk of out of bounds accesses.
– Gerhardh
Nov 13 at 12:52
Ah, yes you're correct the modulus 4 takes care of it.
– EricH
Nov 13 at 13:10
Ah, yes you're correct the modulus 4 takes care of it.
– EricH
Nov 13 at 13:10
add a comment |
1
Welcome to SO. Please add (edit) missing relevant information: What output do you get and what do you expect (copy&paste of real results)
– Gerhardh
Nov 13 at 12:41
2
Did you check available format specifiers for
printf
? What about%c
?– Gerhardh
Nov 13 at 12:41
2
Make
reelValue
a char and print using%c
.– Sourav Ghosh
Nov 13 at 12:42