February 13th, 2003, 12:44 AM
|
#1 (permalink)
| | Banned
Join Date: Dec 2002 Location: Canada
Posts: 271
|
Hey,
I'm writing a simple C++ program, and here's what I've gotten so far... I've taken out some of my previous code, just so I can start from scratch with input I receive here. I have this code for a program;
#include <stdio.h>
main()
{
int atbat, hits, walks;
float average;
printf("Enter the the number of times at bat ");
printf("and hit enter:");
scanf("%d", &atbat);
printf("Enter the number of hits, and");
printf("hit enter:");
scanf("%d", &hits);
printf("Enter the number of walks, and");
printf("hit enter:");
scanf("%d", &walks);
average = hits / (atbat - walks);
printf("The batting average for this player is %f.\n\n", average);
}
I have two problems with this;
1) I would like to add another variable called "name", so that the person can also enter the player's name at the very beginning, so that I can have it print it out at the end, like, printf("The batting average for %q is %d.\n", name, average);
However... I tried to do that the only way I know how (I'm NEW at this...) by adding "char name"... But it didn't work out.
2) How do I make it so that the output for "average" is a decimal-based number? Right now I'm just getting 0.000000 for whatever numerical combination I enter.
Thanks...
x2 |
| |
February 13th, 2003, 01:07 AM
|
#2 (permalink)
| | Ultimate Member
Join Date: Nov 2002 Location: Boise, Idaho
Posts: 2,782
|
I have some old sorce code I wrote for a teacher that calculated semester grade average that worked fine. I will post it and you can use it to probably fix the second problem.
PyroSama
__________________
JOIN [FaD] | TEAM NUMBER 2037 | http://www.techimo.com/forum/t121132.html
|
| |
February 13th, 2003, 01:47 AM
|
#3 (permalink)
| | Banned
Join Date: Dec 2002 Location: Canada
Posts: 271
| |
| |
February 13th, 2003, 02:00 AM
|
#4 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 21,019
|
1. Thats C not C++ 
2. 'char' only holds one character, not a name.. so you'll have to create an array to hold all of the characters.. there's an easy way to do it (scanf? .. can't remember, been too long  )
3. If I remember right there is a Format function that will give you the desired output.. OR if you declare average as int the decimal points will be truncated (do believe they are truncated and NOT rounded!, there is a difference) |
| |
February 13th, 2003, 02:10 AM
|
#5 (permalink)
| | Ultimate Member
Join Date: Nov 2002 Location: Boise, Idaho
Posts: 2,782
|
My sorce code is actualy in c so if I can find it it should work fine. But I havent had any luck so far.
PryoSama |
| |
February 13th, 2003, 07:55 AM
|
#6 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 1,542
|
1) Strings are sequences of characters, so use char[] as the type: Code: char name[256];
scanf("%255c", name) You don't need to reference the name variable as it has type char[] which is the same as char * i.e. pointer to char. You need to pass the maximum string length to scanf to prevent a buffer overflow (255 characters + the terminating NUL = 256).
2) Ah, fun... see, when doing arithmetic on integer variables, C uses integer arithmetic to make things faster, integer division discards the remainder. To force floating point arithmetic to occur, you need to cast one of the sides of the division to a floating point type (of the required precision): Code: average = hits / (float) (atbat - walks); |
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |