home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 2704
Discussions: 188,384, Posts: 2,243,502, Members: 232,614
Old February 13th, 2003, 12:44 AM   Digg it!   #1 (permalink)
Banned
 
xenon200's Avatar
 
Join Date: Dec 2002
Location: Canada
Posts: 271
Program How-To

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

xenon200 is offline   Reply With Quote
Old February 13th, 2003, 01:07 AM     #2 (permalink)
Ultimate Member
 
PyroSama's Avatar
 
Join Date: Nov 2002
Location: Boise, Idaho
Posts: 2,782
Send a message via ICQ to PyroSama Send a message via MSN to PyroSama
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

PyroSama is offline   Reply With Quote
Old February 13th, 2003, 01:47 AM     #3 (permalink)
Banned
 
xenon200's Avatar
 
Join Date: Dec 2002
Location: Canada
Posts: 271
Cool, thanks!

xenon200 is offline   Reply With Quote
Old 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)
vass0922 is offline   Reply With Quote
Old February 13th, 2003, 02:10 AM     #5 (permalink)
Ultimate Member
 
PyroSama's Avatar
 
Join Date: Nov 2002
Location: Boise, Idaho
Posts: 2,782
Send a message via ICQ to PyroSama Send a message via MSN to PyroSama
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
PyroSama is offline   Reply With Quote
Old February 13th, 2003, 07:55 AM     #6 (permalink)
Ultimate Member
 
strangerstill's Avatar
 
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);
strangerstill is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Most Active Discussions
Is It Just Me? (2896)
CPU wont boot (5)
3-days in and no threads about Gaza (160)
The United States Debt (20)
I think I just killed my computer w.. (24)
Upgrading RAM (5)
hp compaq nc6000 problems (138)
Folderchat Weekday thread (441)
Antec 300 bulk purchase? (11)
Recent Discussions
CPU wont boot (5)
Creative T-3000 Subwoofer (3)
ACPI controller halt on boot (2)
Building a gaming computer advi.. (3)
Worth the upgrade?? (15)
Folderchat Weekday thread (441)
ADVICE (0)
How to increase my ram? (5)
Help with an Ati Radeon HD 4850.. (27)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 10:11 PM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28