### Thread: Storing Multiple Values and Outputting

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
3
Rep Power
0

#### Storing Multiple Values and Outputting

Hi Guys

I'm a youth worker attempting to work with some young people on writing a c programme.... and getting lost, but it is fun!

We have written a programme that asks for how many players and then depending on the number asks for players names.

We then roll a dice and what we want to do is say Player 1 your go but for Player 1 substitute their name.

Then on the next dice roll, say the second players name along with your roll.

Here is our code as it is at the moment.

Code:
```//
//  main.c
//  Not Yours
//
//  Created by Richard Brown on 12/10/2012.
//

#include <stdio.h>
#include <string.h> /* memset */
#include <unistd.h> /* close */

int main(void)
{
int dice;
char c;
int numPlayers;
char playerName[10][100];
int loopcount;
int rolls;

numPlayers = 0;
loopcount = 0;
rolls = 0;
dice=5;

srand(time(NULL));

printf("\n\nWelcome to this initial game by Fishfingers Incorporated!\n\nEnter the number of players: \n");
scanf("%d", &numPlayers);

while (loopcount < numPlayers) {
scanf("%s", playerName);
printf("Hello %s\n", playerName);

loopcount++;
}

printf("\n\nYou can press Q to quit at anytime.\n\nIn order to roll again, press the big button called return or enter: \n\n");

while (rolls < numPlayers*13+1) {
int i = 0;

printf("\n",playerName[10][100]);

while (i < dice) {
printf("%d,", 1 + rand() % 6);
i++;

}

rolls++;

// Convert to character value
scanf("%c", &c);
if (c == 'Q' || c == 'q')
break;
}
}```
Thanks for any help.

Rich
2. fishfinger, take your copyright to the bottom of the sea with you.

gpl
3. We work best when presented with an actual problem. Since many of us also work for a living, we only have small bits of time to spend here and so an actual problem allows us to make better use of that time.

One suggestion:
Code:
```        // Convert to character value
scanf("%c", &c);
if (c == 'Q' || c == 'q')
break;```
The way that scanf is written, all characters will be read in, including whitespace (ie, "non-data" characters such as spaces, tabs, newlines, control characters, etc) such as the newline that was entered when you pressed the Enter key to input that character. As a result, you will go through that loop an extra time for each legitimate roll, resulting in about twice as many rolls as you entered.

Instead of "%c", use " %c". That leading space tells scanf that you expect whitespace to precede the meaningful character that you want to read into c and so it will skip any and all leading whitespace, or none if there is none. This practice will also eliminate a common problem posted here of scanf calls "being ignored".

Also, if your development environment includes a debugger (eg, Visual Studio, Visual C++ 2010 Express Edition), start learning how to use it. It will prove very useful when your program doesn't seem to be working right because with it you can execute your program step by step and see what values the variables hold, etc.

And thank you for using code tags on your very first post here. A rarity that is much appreciated.
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
3
Rep Power
0
Originally Posted by b49P23TIvg
fishfinger, take your copyright to the bottom of the sea with you.
Sorry didn't mean to offend. The copyright was meant to be a joke. We use xcode and it added all of that automatically. Sorry we just changed it for a joke. I'll remove it straight away.

Rich
5. However, taking progress made on one forum and passing it off as your starting point on another forum is bad voodoo.

It looks like you're trying to get others to do your work a few lines at a time, then find another forum to add another few lines.
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
3
Rep Power
0
Originally Posted by salem
However, taking progress and passing it off as your starting point on another forum is bad voodoo.

It looks like you're trying to get others to do your work a few lines at a time, then find another forum to add another few lines.
Hi Salem

That is not what was intended. I thought you might have finished for the day and was asking the question here.

I will delete this thread and return from whence I came.

I am sorry to have offended folks.

Rich