07 Oct, 2011, arholly wrote in the 1st comment:
Votes: 0
Hello:
I've got a money problem and I'm not clear enough as to why it is doing what it is doing. Here is the what happens:
Quote
<unhurt> news create
Syntax: newspaper create <cost_in_cents> <name>

<unhurt> newspaper create 200 The Sentinel
Paper 'The Sentinel' created.

<unhurt>

<unhurt> news list
[ 0] The Sentinel $2.198 [Off Stands]

And here is the code for creating newspapers:
int newspaper_new(CHAR_DATA *ch, char *argument)
{
NEWSPAPER *news = new_newspaper();
char arg[MAX_INPUT_LENGTH];
char buf[MAX_INPUT_LENGTH];

if(argument[0] == '\0')
{
send_to_char("Syntax: newspaper create <cost_in_cents> <name>", ch);
return FALSE;
}

argument = one_argument(argument, arg);
if(!is_number(arg))
{
send_to_char("Syntax: newspaper create <cost_in_cents> <name>", ch);
return FALSE;
}

news->cost = atoi(arg);
news->name = str_dup(argument);
news->next = paper_list;
paper_list = news;
sprintf(buf, "Paper '%s' created.\n\r", news->name);
send_to_char(buf, ch);
return TRUE;
}

Any thoughts as to why it would be doing that?
07 Oct, 2011, arholly wrote in the 2nd comment:
Votes: 0
Sorry, had to add all the context in the quote.
07 Oct, 2011, arholly wrote in the 3rd comment:
Votes: 0
More information. In the file it get's saved too, it displays correctly:
Quote
Name The Sentinel~
States 0 200
Articles -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

The 200 is how many pennies it costs, so I'm confused as to why it is saying it is $2.19 and then it also lists that way. Why would it do that?

Thanks,
Arholly
07 Oct, 2011, Vatiken wrote in the 4th comment:
Votes: 0
atoi() could be the issue as it converts a string to an integer, and you appear to have your money as a float. atof() may solve the problem.

Nevermind…

Could still be a int to float conversion problem, as its apparently maintaing the value properly.
Are you using "float_variable = (float) int_variable"???

Might be beneficial to post your news_list() function.
07 Oct, 2011, Sharmair wrote in the 5th comment:
Votes: 0
Maybe if works like a lot of MUDs do with shops, where the cost in
the shop is the cost of the object modified by things like level and
cha of the buyer.
I would look at the code where it lists that 2.198 cost and see where
it is getting the data it is printing. It might also be possible that the
code that converts the 200 into dollars might be wrong too, but that is
where I would start to look.
07 Oct, 2011, arholly wrote in the 6th comment:
Votes: 0
Thanks Vatiken. Here is the news_list code.
int newspaper_list(CHAR_DATA *ch, char *argument)
{
NEWSPAPER *news;
int on_stands = -1;
char buf[MAX_STRING_LENGTH];
int count = 0;

if(!str_cmp(argument, "out now") || !str_cmp(argument, "on stands"))
on_stands = 1;
else if(!str_cmp(argument, "off stands")) on_stands = 0;

for(news = paper_list; news; news = news->next)
{
if(on_stands == -1 || news->on_stands == on_stands)
{
sprintf(buf, "[%4d] %-15s $%d.%.2d [%s]\n\r",
count, news->name, news->cost/100,
(news->cost - news->cost/100),
news->on_stands?"On Stands":"Off Stands");
send_to_char(buf, ch);
}
count++;
}

return TRUE;
}
07 Oct, 2011, arholly wrote in the 7th comment:
Votes: 0
Sharmair said:
Maybe if works like a lot of MUDs do with shops, where the cost in
the shop is the cost of the object modified by things like level and
cha of the buyer.
I would look at the code where it lists that 2.198 cost and see where
it is getting the data it is printing. It might also be possible that the
code that converts the 200 into dollars might be wrong too, but that is
where I would start to look.

Interesting thought. The code looks right (though kludgy), but being new and getting this from someone else means I'm having to learn on the fly. Thanks for the thought though. I mean, it does look like it's converting it right, doesn't it?
07 Oct, 2011, Sharmair wrote in the 8th comment:
Votes: 0
And it looks like your conversion is incorrect, that second part (the
(news->cost - news->cost/100), should be more like:
news->cost%100,
07 Oct, 2011, arholly wrote in the 9th comment:
Votes: 0
Sharmair said:
And it looks like your conversion is incorrect, that second part (the
(news->cost - news->cost/100), should be more like:
news->cost%100,

Which (help the guy out) does what? I want to understand what is going on so I can fix it in the likely happening that this crops up in other places.
07 Oct, 2011, arholly wrote in the 10th comment:
Votes: 0
OK, so what you suggested Sharmair worked for the newspaper list command, but doesn't fix the problem in newspaper_new function. I'm not sure what it does, so if you could explain, I'd appreciate it.
07 Oct, 2011, Runter wrote in the 11th comment:
Votes: 0
arholly said:
OK, so what you suggested Sharmair worked for the newspaper list command, but doesn't fix the problem in newspaper_new function. I'm not sure what it does, so if you could explain, I'd appreciate it.


The modulo operator evaluates to the remaining value after dividing by the value. So 125 % 100 evaluates to 25. It's akin to division. Where 125 / 100 evaluates to 1.
07 Oct, 2011, Runter wrote in the 12th comment:
Votes: 0
http://codepad.org/xnouiE3b

Incidentally you could have also written it like this for formatting the dollar value.
07 Oct, 2011, arholly wrote in the 13th comment:
Votes: 0
OK, that makes sense. Thanks for the explanation Runter. That does help.
07 Oct, 2011, Sharmair wrote in the 14th comment:
Votes: 0
The way you had it, to print the part after the . you were taking
the 200 and subtracting 2. As a side note here, you could also
change what you have to instead of subtracting 2, subtract 2*100:
(news->cost - news->cost/100*100)

The way math works in C is that if both arguments are ints (to a
/), it does an int divide and chops off any decimal part (198/100
is 1 with int math). The % operator is remainder, and will do an
int divide and return the remainder. So in your case, 200/100 will
be 2, the correct whole part, and 200%100 will be 0, the correct
decimal part. Using the method I just posted in THIS post, you
take the 200, divide by 100 (using int math), getting 2, then mult
that by 100 getting 200, then subtracting that from your base 200
getting 0, either way will work, but you will see the remainder used
in alot of code used in MUDs.
07 Oct, 2011, arholly wrote in the 15th comment:
Votes: 0
Thanks. You guys have really cleared it up for me. This will be a big help down the road because I suspect there are more areas where it is coded like that.
0.0/15