473,387 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Values

I'm having trouble with expressions giving me the right results.

int healer()
{
printf ("\nOrko the Healer\n");
printf ("Your health: (%d)/(%d)\n\n", chealth, cmaxhealth);
heal = (cmaxhealth - chealth);
heal *= 2;
printf ("%d gold to heal.\n\n", heal);
}

Why does the variable heal always result in 0 while the variables
chealth and cmaxhealth are between 2 and 16? How can I fix this?

Any help would be appreciated... Thanks.

-- Jon
Aug 5 '06 #1
10 1288
Jonathan H. Justvig wrote:
I'm having trouble with expressions giving me the right results.

int healer()
{
printf ("\nOrko the Healer\n");
printf ("Your health: (%d)/(%d)\n\n", chealth, cmaxhealth);
heal = (cmaxhealth - chealth);
heal *= 2;
printf ("%d gold to heal.\n\n", heal);
}

Why does the variable heal always result in 0 while the variables
chealth and cmaxhealth are between 2 and 16? How can I fix this?
Try posting the smallest compilable snippet that exhibits the problem.
You haven't shown the declarations of heal, chealth or cmaxhealth,
let alone the methods you used to put values into them.

At a guess, %d is wrong for the variable heal, but until you post
the missing details, all you'll get are answers from the crystal ball.

--
Peter

Aug 5 '06 #2
Peter Nilsson wrote:
Try posting the smallest compilable snippet that exhibits the problem.
You haven't shown the declarations of heal, chealth or cmaxhealth,
let alone the methods you used to put values into them.

At a guess, %d is wrong for the variable heal, but until you post
the missing details, all you'll get are answers from the crystal ball.
I apologize, my very first post to a newsgroup. Try this...

int heal, chealth, cmaxhealth;
int cstr, cdex, cint, cvit;

-- snip

int class()
{
srand ( time(NULL) );
cvit = rand()%8+1;
cvit += rand()%8+1;
printf ("Your Vitality: %d\n", cvit);
cmaxhealth = cvit / 2;
chealth = cmaxhealth;
printf ("Your Health: (%d/%d)\n\n", chealth, cmaxhealth);

}

-- snip

int healer()
{
printf ("\nOrko the Healer\n");
printf ("Your health: (%d)/(%d)\n\n", chealth, cmaxhealth);
heal = (cmaxhealth - chealth);
heal *= 2;
printf ("%d gold to heal.\n\n", heal);
}

-- snip

I hope this helps, although it may/may not compile. To save disasters,
I don't really want to post the entire source in a newsgroup. Thanks
for the quick response.

-- Jon
Aug 5 '06 #3

Jonathan H. Justvig wrote:
int heal, chealth, cmaxhealth;
int cstr, cdex, cint, cvit;

-- snip

int class()
{
srand ( time(NULL) );
cvit = rand()%8+1;
cvit += rand()%8+1;
printf ("Your Vitality: %d\n", cvit);
cmaxhealth = cvit / 2;
chealth = cmaxhealth;
Initially you set health to max health
printf ("Your Health: (%d/%d)\n\n", chealth, cmaxhealth);

}

-- snip

int healer()
{
printf ("\nOrko the Healer\n");
printf ("Your health: (%d)/(%d)\n\n", chealth, cmaxhealth);
heal = (cmaxhealth - chealth);
heal *= 2;
Here you are comparing health.Did you reduce health somewhere?
Untill that happens, heal will be zero.
printf ("%d gold to heal.\n\n", heal);
}
Aug 5 '06 #4
ma*******@gmail.com wrote:
> chealth = cmaxhealth;
Initially you set health to max health
> heal = (cmaxhealth - chealth);
heal *= 2;
Here you are comparing health.Did you reduce health somewhere?
Untill that happens, heal will be zero.
Actually, no I haven't yet and that made me realize I should decrease
chealth by 1 or more and that solved the problem. Thanks for pointing
that out.

-- Jon
Aug 5 '06 #5
"Jonathan H. Justvig" <cr******@cox.netwrites:
I'm having trouble with expressions giving me the right results.

int healer()
{
printf ("\nOrko the Healer\n");
printf ("Your health: (%d)/(%d)\n\n", chealth, cmaxhealth);
heal = (cmaxhealth - chealth);
heal *= 2;
printf ("%d gold to heal.\n\n", heal);
}

Why does the variable heal always result in 0 while the variables
chealth and cmaxhealth are between 2 and 16? How can I fix this?
By showing us some more code, preferably a complete self-contained
program.

Are chealth, cmaxhealth, and heal all of type int? (You told printf
they are; if you lied, it will get its revenge.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 5 '06 #6
Keith Thompson wrote:
"Jonathan H. Justvig" <cr******@cox.netwrites:
>I'm having trouble with expressions giving me the right results.

int healer()
{
printf ("\nOrko the Healer\n");
printf ("Your health: (%d)/(%d)\n\n", chealth, cmaxhealth);
heal = (cmaxhealth - chealth);
heal *= 2;
printf ("%d gold to heal.\n\n", heal);
}

Why does the variable heal always result in 0 while the variables
chealth and cmaxhealth are between 2 and 16? How can I fix this?

By showing us some more code, preferably a complete self-contained
program.

Are chealth, cmaxhealth, and heal all of type int? (You told printf
they are; if you lied, it will get its revenge.)
No, I didn't lie, they're all integers so I'll be able to sleep safely
tonight. I was wondering though, is there an easy way or a good way to
make a menu inside a menu that require a command prompt and returns to
the previous menu? I'm sort of stuck. Here are some snips...

--snip

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int mhp, mhpmax;
char *monname;
int monhp, monexp, mongold;
int attack;
char cname [15];
char *cclass;
int cstr, cdex, cint, cvit;
int heal, chealth, cmaxhealth;
int experience, gold;
int monattack;
int monatt;
char ans;
int exitgame;

char *menu[] =
{
" ",
" -------------------------------------- ",
" | Crimson's RPG |",
" | ~~~~~~~~~~~~~ |",
" | |",
" | (S)tart New Game |",
" | (L)oad Game |",
" | (Q)uit |",
" | |",
" | Please enter choice below. |",
" | |",
" -------------------------------------- "
" ",
};

startmenu()
{
exitgame = 0;
int line_num;

for (line_num = 0; line_num < 12; line_num++)
{
printf ("%s\n", menu[line_num]);
}

printf ("Command: ");

while ((ans = getchar()) != EOF)

if (ans == 'Q' || ans == 'q')

{
printf ("\nQuiting game...\n\n");
exitgame = 1;
return 0;
}

else if (ans == 'S' || ans == 's')
{
printf ("\nStarting game...\n\n");
printf ("Enter your name: ");
scanf ("%s", cname);
printf ("(F)ighter\n");
printf ("(S)orceress\n");
printf ("(T)hief\n");
printf ("\nEnter your class: ");
class();
return 0;
}
}

char *town[] =
{
" ",
" -------------------------------------- ",
" | Crimson's Town |",
" | ~~~~~~~~~~~~~~ |",
" | |",
" | (F)orest (W)eapon Shop |",
" | (H)ealer (A)rmour Shop |",
" | (Q)uit (Y)our Stats |",
" | |",
" | Please enter choice below. |",
" | |",
" -------------------------------------- ",
" ",
};

int townmenu()
{

int line_num;

for (line_num = 0; line_num < 13; line_num++)
{
printf ("%s\n", town[line_num]);
}

printf ("Command: ");

while ((ans = getchar()) !=EOF) {

if (ans == 'F' || ans == 'f')
{
level1();
return 0;
}
if (ans == 'H' || ans == 'h')
{
healer();
return 0;
}
if (ans == 'Y' || ans == 'y')
{
yourstats();
return 0;
}
else if (ans == 'Q' || ans == 'q')
{
printf ("\nQuiting game...\n\n");
return 0;
}
}

}

-- snip

int main()
{
exitgame = 0;
startmenu();
if (exitgame = 0)
printf ("\nWelcome to Crimson's RPG, %s!\n", cname);
for (;;)
{
if (ans != 'Q' && ans != 'q')
townmenu();
else return 0;
}
printf("Quiting Game...");
return 0;
}
When I press Q from the Town menu I want it to go to the Start Menu but
it just quits the program. Also, I just want a single char input where
a [return] isn't required. Any advice?

-- Jon
Aug 5 '06 #7
On 2006-08-05, Jonathan H. Justvig <cr******@cox.netwrote:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int mhp, mhpmax;
char *monname;
int monhp, monexp, mongold;
int attack;
char cname [15];
char *cclass;
int cstr, cdex, cint, cvit;
int heal, chealth, cmaxhealth;
int experience, gold;
int monattack;
int monatt;
char ans;
int exitgame;
Oh, so many globals! As a general rule, globals cause more problems than
the solve, and IMHO, you should stick with passing arguments. (This is a
pretty widely-shared view in this group.)
char *menu[] =
{
" ",
" -------------------------------------- ",
" | Crimson's RPG |",
" | ~~~~~~~~~~~~~ |",
" | |",
" | (S)tart New Game |",
" | (L)oad Game |",
" | (Q)uit |",
" | |",
" | Please enter choice below. |",
" | |",
" -------------------------------------- "
" ",
};

startmenu()
Relying on implicit int is poor style in C89, and illegal in C99.
{
exitgame = 0;
Don't use tabs on usenet; they're too wide for 80-character displays and
many newsservers strip them out.
int line_num;

for (line_num = 0; line_num < 12; line_num++)
{
printf ("%s\n", menu[line_num]);
How about
puts (menu[line_num]);
}

printf ("Command: ");

while ((ans = getchar()) != EOF)
1) ans needs to be int to catch EOF properl.y
2) I shouldn't need to go to the top of the source to check ans' type.
3) If you put a tolower() around getchar() you don't need to have two
tests for each value. (In fact, you can easily use a switch, which
is the recommended idiom.)
>
if (ans == 'Q' || ans == 'q')

{
printf ("\nQuiting game...\n\n");
exitgame = 1;
return 0;
}

else if (ans == 'S' || ans == 's')
{
printf ("\nStarting game...\n\n");
printf ("Enter your name: ");
scanf ("%s", cname);
printf ("(F)ighter\n");
printf ("(S)orceress\n");
printf ("(T)hief\n");
printf ("\nEnter your class: ");
class();
return 0;
}
}
In general,
puts ("XXX");
is clearer than
printf ("XXX\n");
and possibly more efficient.

Because this post is getting pretty long, I'll let you implement those
suggestions and post back. You might want to condense those menus to a
single line when posting (they're beautiful, I know, but they take up
a lot of space).

--
Andrew Poelstra <http://www.wpsoftware.net/projects>
To reach me by email, use `apoelstra' at the above domain.
"Do BOTH ends of the cable need to be plugged in?" -Anon.
Aug 5 '06 #8

Jonathan H. Justvig wrote:
<snipped much code>
When I press Q from the Town menu I want it to go to the Start Menu but
it just quits the program. Also, I just want a single char input where
a [return] isn't required. Any advice?
<OT>
You'll need to put the terminal in raw mode. Read
up on ncurses. Slang looks useful as well, altough I've
never used it.
</OT>

--
Bill Pursell

Aug 5 '06 #9
Andrew Poelstra wrote:
Oh, so many globals! As a general rule, globals cause more problems than
the solve, and IMHO, you should stick with passing arguments. (This is a
pretty widely-shared view in this group.)
And, I'm sure there will be many more unless I can figure out something
else. I'm fairly new to C and only been programming a couple of weeks
so please bear with me. If you could be so kind, could you give me an
example of a passing argument? Or link me to where I can get a good
idea of what you mean?
Relying on implicit int is poor style in C89, and illegal in C99.
int startmenu() was intended, probably be awhile before I would have
actually noticed it though.
Don't use tabs on usenet; they're too wide for 80-character displays and
many newsservers strip them out.
I'll have to remember to remove them next time.
puts (menu[line_num]);
Worked like a charm.
1) ans needs to be int to catch EOF properl.y
2) I shouldn't need to go to the top of the source to check ans' type.
3) If you put a tolower() around getchar() you don't need to have two
tests for each value. (In fact, you can easily use a switch, which
is the recommended idiom.)
If I used switch, I could avoid all the ' if (ans == ' '... etc)'? How
might I use a switch for a menu?
In general,
puts ("XXX");
Used puts ("XXX"); for most of the text except where I needed to use
variables. Worked okay.
Because this post is getting pretty long, I'll let you implement those
suggestions and post back. You might want to condense those menus to a
single line when posting (they're beautiful, I know, but they take up
a lot of space).
Everything is set. My next objective would be to implent switches
instead of getchar() since getchar() seems kinda generic. Also, where
there is to be a single character command, I'd like to replace scanf as
well. Thanks for all the help thus far.

-- Jon
Aug 5 '06 #10
On 2006-08-05, Jonathan H. Justvig <cr******@cox.netwrote:
Andrew Poelstra wrote:
>Oh, so many globals! As a general rule, globals cause more problems than
the solve, and IMHO, you should stick with passing arguments. (This is a
pretty widely-shared view in this group.)

And, I'm sure there will be many more unless I can figure out something
else. I'm fairly new to C and only been programming a couple of weeks
so please bear with me. If you could be so kind, could you give me an
example of a passing argument? Or link me to where I can get a good
idea of what you mean?
int main (void) /* Recommended style. */
{
int b = 5; /* Not global */

function_that_reads_b (b);
function_that_modifies_b (&b);
return 0;
}

That way it's clear what functions are changing what variables, and what
functions require what variables. It's much easier to read code that way.
>Relying on implicit int is poor style in C89, and illegal in C99.

int startmenu() was intended, probably be awhile before I would have
actually noticed it though.
Hey, that's what comp.lang.c is here for. :-)
>Don't use tabs on usenet; they're too wide for 80-character displays and
many newsservers strip them out.

I'll have to remember to remove them next time.
You'll be reminded every time you don't, so after a few posts it'll be hard
to forget. :-)
>1) ans needs to be int to catch EOF properl.y
2) I shouldn't need to go to the top of the source to check ans' type.
3) If you put a tolower() around getchar() you don't need to have two
tests for each value. (In fact, you can easily use a switch, which
is the recommended idiom.)

If I used switch, I could avoid all the ' if (ans == ' '... etc)'? How
might I use a switch for a menu?
int c = tolower (getchar());
switch (c)
{
case 'f':
/* Fight! */
break;
case 'b':
/* Buy! */
break;
case 'q':
/* Quit! */
break;
case EOF:
/* Oh, no! Problems! */
break;
}
>
Everything is set. My next objective would be to implent switches
instead of getchar() since getchar() seems kinda generic. Also, where
there is to be a single character command, I'd like to replace scanf as
well. Thanks for all the help thus far.
getchar() merely gets the input. You need that. `switch'es replace `if's
to control what you do with said input. See my example above.

--
Andrew Poelstra <http://www.wpsoftware.net/projects>
To reach me by email, use `apoelstra' at the above domain.
"Do BOTH ends of the cable need to be plugged in?" -Anon.
Aug 5 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Steve Hall | last post by:
Folks, My secnario involves two tables - ObservationRegister, and Person. ObservationRegister contains most of the "useful" fields, including the UserID of the person that raised the record, and...
6
by: cipher | last post by:
I have some constant values in my web service that my client application will require. Having to keep server side and client side definitions insync is tedious. I am trying to do something like...
2
by: Hennie | last post by:
I apologise if this is a stupid question, but I would appreciated any help on this subject. I want to create a view (VIEW_1 in example below) where I take numeric values from a field in one...
13
by: Gregor =?UTF-8?B?S292YcSN?= | last post by:
Hi! With VALUES you can do something like: SELECT * FROM (VALUES ('A', 1), ('B', 2), ('C', 2)) AS TEMP(LETTER, NUMBER) which will give you: LETTER NUMBER ------ ------ A 1 B 2...
8
by: gigonomics | last post by:
Hi all, I hope someone can help me out. I need to return the best available seats subject to the constraint that the seats are side by side (or return X consecutive records from a table column...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.