Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-17-2004, 01:52 PM   #1 (permalink)
cracksevi
Registered User
 
Join Date: Oct 2004
Posts: 14
cracksevi is on a distinguished road
Send a message via ICQ to cracksevi
another simple question

im still a newbie tryin to learn the basics...
now...
the prob is double & static_cast functions :

int main()
{
int total, // sum of grades
gradeCounter, // number of grades entered
grade; // one grade
double average; // number with decimal point for average

// initialization phase
total = 0;
gradeCounter = 0;

// processing phase
cout << "Enter grade, -1 to end: ";
cin >> grade;

while ( grade != -1 ) {
total = total + grade;
gradeCounter = gradeCounter + 1;
cout << "Enter grade, -1 to end: ";
cin >> grade;
}

// termination phase
if ( gradeCounter != 0 ) {
average = static_cast< double >( total ) / gradeCounter;
cout << "Class average is " << setprecision( 2 )
<< setiosflags( ios::fixed | ios::showpoint )
<< average << endl;




why -> total <- is in parantheses @ average = static_cast< double >( total ) / gradeCounter;

and is there any connection between double and static_cast??
cuz when i tried a proggie with double...
for example the prog was 11/2
and it showed the result as 5 or 6 (i dont remember well)
and when i tried with static_cast as extra , it showed result as 5.5
an explanation can be so nice friends....



and sorry for asking dumbish questions...
cracksevi is offline   Reply With Quote
Old 10-17-2004, 02:21 PM   #2 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
what's going on here is the order of type promotions is getting shifted around.

Code:
/* #1 */
average =  total / gradeCounter;
/* #2 */
average = static_cast< double >( total ) / gradeCounter;
in line #1, what you are doing is taking one int (total), and dividing it by another int (gradeCounter). that result is then cast to a double and stored into average. so, in integer math, 11 / 2 = 5. this is then cast to a double, and average is 5.0.

in line #2, the first thing that happens is that "total" is promoted to a double. now, you can't divide a double by an int, so "gradeCounter" is promoted to a double too. 11.0 / 2.0 = 5.5. the result is already double, so no promotion is necessary.

now, perhaps someone better at c++ than i can explain to us the value of static_cast over a plain cast.
joe_bruin is offline   Reply With Quote
Old 10-17-2004, 02:54 PM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
"Plain Cast"
I assume you main "tradittional cast" like (doube)anInt/anInt;

Quote:
The C++ Language, section 6.2.7., B. Stroustrup
This Cstyle cast is far more dangerous than the named conversion
operators because the notation is harder to spot in a large program and the kind of conversion intended by the programmer is not explicit.
__________________
Valmont is offline   Reply With Quote
Old 10-17-2004, 06:11 PM   #4 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
thanks.
joe_bruin is offline   Reply With Quote
Old 10-18-2004, 12:53 AM   #5 (permalink)
cracksevi
Registered User
 
Join Date: Oct 2004
Posts: 14
cracksevi is on a distinguished road
Send a message via ICQ to cracksevi
aha now it begins to make some sense...
thanx for the info bros!
cheers !
cracksevi is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
c simple question if13121 Standard C, C++ 10 11-01-2004 06:29 AM
c simple question problem with switch case if13121 Standard C, C++ 1 10-24-2004 09:43 PM
simple c question if13121 Standard C, C++ 3 10-18-2004 10:20 PM
Really simple question... Ilya020 HTML, XML, Javascript, AJAX 12 10-29-2003 02:10 PM
Just a simple Question Zenogear11 Standard C, C++ 12 04-30-2003 01:33 PM


All times are GMT -8. The time now is 10:07 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting