Still incorrect data type on line 14.
And the amount of memory you are allocation on for your arrays doesn't match up with your data types. Yikes!
29 Replies - 2206 Views - Last Post: 10 November 2012 - 06:36 PM
#17
Re: [C] Allocating memory for Dice Roller
Posted 08 November 2012 - 06:11 PM
Oh what sweet relief. I think I have been at this for possibly two months now. I'll send this code to my friend and see what he says.
There is a chance that he'll say "Great, but you didn't do what I asked" >.>
Keep you posted.
There is a chance that he'll say "Great, but you didn't do what I asked" >.>
Keep you posted.
#18
Re: [C] Allocating memory for Dice Roller
Posted 08 November 2012 - 06:17 PM
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include <string.h>
int main()
{
//initilization of Dice amount
char dice[64]; // this almost is the only type that makes much sense
unsigned diceSides, diceAmount; // why unsigned? int
signed modifier = 0; /// why signed? int
int diceTotal = 0; // yay
char *diceResultList; // why char *
char *diceList; // why char *
srand ( time (NULL) );
//Ask user for number of sides and dice and how many dice to roll
printf("Enter dice amount and sides (example, 1d20 or 1d20 +5): ");
gets(dice);
sscanf(dice, "
%u %i", &diceAmount, &diceSides, &modifier);
//Allocation of memory
diceResultList = malloc(sizeof(double)*(diceAmount)); // huh? sizeof(double), you don't use any doubles!?!
diceList = malloc(sizeof(double)*(diceAmount));
//Initilization of dicePrintList to diceList to allow printing of results
char *dicePrintList = diceList; // ok...
for(int i = 0; i < diceAmount; i++)
{
int r = rand() % diceSides + 1;
diceResultList[i] = r; // NO, you have made this an array of char. say what you acutally mean!
//By adding each instance of "r" to diceTotal, we can get a total of all numbers rolled
diceTotal += r;
}
// what does dicePrintList += sprintf even mean?
for(int j = 0; j < diceAmount; j++)
{
if(j + 1 < diceAmount)
dicePrintList += sprintf(dicePrintList, "%d, ", diceResultList[j]);
else
dicePrintList += sprintf(dicePrintList, "%d", diceResultList[j]);
}
printf("%s\n", diceList); // when did you load anything into diceList
diceTotal += modifier;
printf("\nTotal is: %d \n", diceTotal);
getchar();
// returning an int would be nice
}
A number have people have commented here. You've pretty much ignored them all. You are simply throwing code at the wall and hoping it will stick. This approach will not make a working program magically happen.
#19
Re: [C] Allocating memory for Dice Roller
Posted 08 November 2012 - 06:30 PM
Excuse me for such rudeness baavgai. As I have said in the past, I am basically working with another guy in all this who tells me what he believes is best.
You also have to understand that at this point, throwing grenades at the wall this something works was the best I could do til I could figure out and understand what was wrong.
So some of the code is labeled incorrectly due to such process.
Really, I'm under the stress of doing something for a person acting as teacher and I don't want to be doing things I don't understand.
I apologize for how I have acted in doing this.
EDIT: Also the reason I can assume for the characters is to allow for a String to be printed or returned and thus allowing for this to be put into a line on Skype
You also have to understand that at this point, throwing grenades at the wall this something works was the best I could do til I could figure out and understand what was wrong.
So some of the code is labeled incorrectly due to such process.
Really, I'm under the stress of doing something for a person acting as teacher and I don't want to be doing things I don't understand.
I apologize for how I have acted in doing this.
EDIT: Also the reason I can assume for the characters is to allow for a String to be printed or returned and thus allowing for this to be put into a line on Skype
This post has been edited by Akasen: 08 November 2012 - 06:32 PM
#20
Re: [C] Allocating memory for Dice Roller
Posted 08 November 2012 - 06:48 PM
Quote
I am basically working with another guy in all this who tells me what he believes is best.
Quote
doing something for a person acting as teacher
If you're working along with this person in the hopes of learning, ditch him now. Your code and approach is garbage, and if he can't recognize and correct you, ignore him.
Maybe your intentions are honest, but your approach can't work here. It's programming by guessing, which can't fly.
if your objective is to learn, start from scratch, and avoid such "teachers".
#21
Re: [C] Allocating memory for Dice Roller
Posted 08 November 2012 - 07:07 PM
I know he is not fully an idiot. He is not scamming me either. But such a simple issue, and he looked over it? I doubt he has my source.
You know, msybe I should start this from scratch and work on the new one thrpugh this forum. I'll continue working with him on the current project but when a snag is reached again, I am not pursuing an answer.
Look at the source, even I know it's a mess. I can't really defend it
I thsnk you all for your help thus far and apologize for my rudeness
You know, msybe I should start this from scratch and work on the new one thrpugh this forum. I'll continue working with him on the current project but when a snag is reached again, I am not pursuing an answer.
Look at the source, even I know it's a mess. I can't really defend it
I thsnk you all for your help thus far and apologize for my rudeness
#22
Re: [C] Allocating memory for Dice Roller
Posted 08 November 2012 - 07:51 PM
Quote
But such a simple issue
Your fundamentally not approaching programming correctly. That's why a simple program is so difficult. You have 56 lines of code, but even the first few lines of code have severe problems. A proper mentor should have stopped you and helped you properly.
You want to build your code in small, reliable chunks. For example, you start with a program that does nothing:
int main(void) {
return 0;
}
Then focus on asking the user for information:
#include <stdio.h>
#define MAXLINEINPUT 80
int main(void) {
char userInput[80];
puts("Enter dice amount and sides (example, 1d20 or 1d20 +5): ");
if(fgets(userInput, MAXLINEINPUT, stdin) == NULL) {
// What do I do now?
}
return 0;
}
Then focus on parsing the user input to break apart the dice notation.
And maybe you'll end up refactoring along the way:
int fetchDiceNotationInput(int *numDice, int *numDiceSides, int *modifier); int parseDiceNotation(int *numDice, int *numDiceSides, int *modifier, char *str);
and so on.
It takes experience to approach tasks with this discipline. If your mentor isn't helping you with this, and instead is forcing you to follow his pace, he's not much of a mentor.
This post has been edited by Oler1s: 08 November 2012 - 07:51 PM
#23
Re: [C] Allocating memory for Dice Roller
Posted 08 November 2012 - 08:03 PM
So what do I do then? I've been at trying to learn for less than a year now. It's really starting to seem hopeless now.
I can't seem to rely on anything, not even myself to learn this.
I can't seem to rely on anything, not even myself to learn this.
#24
Re: [C] Allocating memory for Dice Roller
Posted 08 November 2012 - 08:11 PM
Like I said, ditch the person who is supposedly teaching you.
You need to find an entry point that is appealing. Trying out different programming languages might help. I don't consider C a good starting language.
Maybe you do like C, and it's just the learning resource. What book are you using to learn?
Maybe you just need to do something that's not directly programming at first. Learning a bit of HTML and CSS to do web design, and then learning Javascript for programming might be an entry point.
Or if you like games, using something like Unity to begin learning.
Once you find something you can enthusiastically spend time exploring on your own, you can then pursue learning at your own pace, however fast or slow it needs to be.
You need to find an entry point that is appealing. Trying out different programming languages might help. I don't consider C a good starting language.
Maybe you do like C, and it's just the learning resource. What book are you using to learn?
Maybe you just need to do something that's not directly programming at first. Learning a bit of HTML and CSS to do web design, and then learning Javascript for programming might be an entry point.
Or if you like games, using something like Unity to begin learning.
Once you find something you can enthusiastically spend time exploring on your own, you can then pursue learning at your own pace, however fast or slow it needs to be.
#25
Re: [C] Allocating memory for Dice Roller
Posted 08 November 2012 - 08:20 PM
I currently have the book "C By Discovery" by Foster & Foster. I believe I have somewhere "The C Programming" by Kernhigan and Ritchie.
I always wanted to get into programming for games. Never really knew how to though so I tried to start off as basic as I thought I could.
I always wanted to get into programming for games. Never really knew how to though so I tried to start off as basic as I thought I could.
#26
Re: [C] Allocating memory for Dice Roller
Posted 08 November 2012 - 09:06 PM
#27
Re: [C] Allocating memory for Dice Roller
Posted 09 November 2012 - 01:44 AM
I hace read that article before. i don't think I ever got anythibg out of it though.
I'm starting to slowly lose hope now. I don't even know where to really start anymore. I could try doing HTML, CSS, and Javascript, but would it really help me?
I'm starting to slowly lose hope now. I don't even know where to really start anymore. I could try doing HTML, CSS, and Javascript, but would it really help me?
#28
Re: [C] Allocating memory for Dice Roller
Posted 09 November 2012 - 03:54 AM
Stop. Right now. Take a breath. Do not loose hope. You CAN program. It takes time and effort and a high tolerance for frustration. Most people get blown away by the last one.
The computer is what you trust. Trust in the idea that there is no magic and everything happens for a logical reason. As Oler1s said, you must work in small, logical chunks. If, at any time, you can't completely explain the logic of a chunk, stop and figure it out. If it seems too much, break it into smaller chunks.
Why trust the computer? Because it will tell you, in no uncertain terms, if you got it right or not. This is the frustration. It can look right to you, but the computer is stupid literal; it does EXACTLY what you told it to do. You might think your intent is obvious, but programs don't run on what you think, only what you instruct.
I commented the crap out of you original program. I may have been blunt, but I wasn't trying to be cruel. There are questions in my comments. Every one of those questions you, the programmer, should be able to answer. You should be able to explain and defend any code you write. Because YOU wrote it, for a reason. Even if your reasoning was based on invalid assumptions and misunderstanding, you should still be able to tell me why. Then, you need to figure out why the computer didn't agree with your reasoning.
Don't give up. If you know you don't know, you're ready to learn.
The computer is what you trust. Trust in the idea that there is no magic and everything happens for a logical reason. As Oler1s said, you must work in small, logical chunks. If, at any time, you can't completely explain the logic of a chunk, stop and figure it out. If it seems too much, break it into smaller chunks.
Why trust the computer? Because it will tell you, in no uncertain terms, if you got it right or not. This is the frustration. It can look right to you, but the computer is stupid literal; it does EXACTLY what you told it to do. You might think your intent is obvious, but programs don't run on what you think, only what you instruct.
I commented the crap out of you original program. I may have been blunt, but I wasn't trying to be cruel. There are questions in my comments. Every one of those questions you, the programmer, should be able to answer. You should be able to explain and defend any code you write. Because YOU wrote it, for a reason. Even if your reasoning was based on invalid assumptions and misunderstanding, you should still be able to tell me why. Then, you need to figure out why the computer didn't agree with your reasoning.
Don't give up. If you know you don't know, you're ready to learn.
#29
Re: [C] Allocating memory for Dice Roller
Posted 09 November 2012 - 06:20 PM
Akasen, on 09 November 2012 - 03:42 AM, said:
... I would like to know Why the array is showing negative numbers or incorrect numbers.
The array has elements of the char type, which can store values of -127 to 128. 1D1000 will return values larger than that and will give you your strange results when entered into the array.
#30
Re: [C] Allocating memory for Dice Roller
Posted 10 November 2012 - 06:36 PM
Okay I'm back and calmed down. But I think I may put this whole dice roller project on hiatus for now and work on my own things for now. Maybe program Hangman, that would be good fun 
I've learned from past mistakes before, I'll do it again. Plus I got better things to worry about like a college C project.
In the end, I guess I should be grateful for coming across this website. Without people like you, I think I would have given up on my journey to become a programmer.
Also, diceList seemed to serve no purpose. Removed it altogether and replaced the print statement for it with "diceResultList" and it works quite well. I can even tell it to roll 1000d10 for example.
Anyways, that will be all for this. I think that wraps it up for this thread. I think locking it would be a fine way to end things.
Also thanks #define. That really helps to make sense of why it was happening.
I've learned from past mistakes before, I'll do it again. Plus I got better things to worry about like a college C project.
In the end, I guess I should be grateful for coming across this website. Without people like you, I think I would have given up on my journey to become a programmer.
Also, diceList seemed to serve no purpose. Removed it altogether and replaced the print statement for it with "diceResultList" and it works quite well. I can even tell it to roll 1000d10 for example.
Anyways, that will be all for this. I think that wraps it up for this thread. I think locking it would be a fine way to end things.
Also thanks #define. That really helps to make sense of why it was happening.

New Topic/Question
Reply



MultiQuote





|