GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Feb-2007, 13:37
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

Odd behavior when removing #include statement from source file


As a modification of my last program, to print squares of a series of integers, I moved the functions to calculate the widths of the two columns to a separate source file. That source file exists strictly to define the calculations;

CPP / C++ / C Code:
#include "FindColWidths.h"
#include <cmath>
//#include <iostream>
using std::fabs;
using std::pow;

int findRootWidth(double i, double width) {
    if (fabs(i) < pow(10, width)) {
        return int(width) + 2;
    }
    else {
        return findRootWidth(i, width + 1);
    }
}

int findSqrWidth(double i, double width){
    if (pow(i, 2) < pow(10, width)) {
        return int(width) + 1;
    }
    else {
        return findSqrWidth(i, width + 1);
    }
}

Header:

CPP / C++ / C Code:
#ifndef GUARD_FindColWidths_h
#define GUARD_FindColWidths_h

int findRootWidth(double, double);

int findSqrWidth(double, double);

#endif
Main:
CPP / C++ / C Code:
#include <iostream>
#include <cmath>
#include <iomanip>
#include "FindColWidths.h"

using std::endl;
using std::cout;
using std::pow;
using std::fabs;
using std::setw;
using std::setprecision;
using std::streamsize;

int main () {
    double start = -10;
    double end = 1001;
    double absoluteLargest = fabs(start) < fabs(end) ? end : start;
    cout << "Absolute Largest: " << absoluteLargest << endl;
    int rootWidth = findRootWidth(absoluteLargest, 1);
    cout << "RootWidth: " << rootWidth << endl;
    int sqrWidth = findSqrWidth(absoluteLargest, 1);
    streamsize prec = cout.precision();
    cout << setprecision(sqrWidth);
    for (start; start < end; start++) {
        cout << setw(rootWidth) << start 
             << setw(sqrWidth) << pow(start, 2)
             << endl;
    }
    cout.precision(prec);
    return 0;
}

I found that the left column wasn't being formatted correctly,

Quote:
Absolute Largest: 1001
RootWidth: 3
-10 100

so inserted some cout statements in the relevant function, and discovered quite accidentally that adding

CPP / C++ / C Code:
#include <iostream>

to the FindColumnWidths.cpp source file caused the formatting problem to disappear.

Quote:
Absolute Largest: 1001
RootWidth: 6
-10 100

Why is this? That source file is used only to calculate the widths of the columns and return their int values. All output messages and stream manipulation are done from the main function. The source file doesn't need #include <iostream> to function; so why would adding it there cause the calculations to change?

TIA as usual
  #2  
Old 08-Feb-2007, 14:28
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,200
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Odd behavior when removing #include statement from source file


Quote:
Originally Posted by earachefl
The source file doesn't need #include <iostream> to function; so why would adding it there cause the calculations to change?

Assuming that putting in the header is the only thing you changed: It wouldn't.

I respectfully suggest that you go back with your finished program and put the #include <iostream> back in and take it out again.

If it gives you different results (and #including or not #including the header is the only thing you change), then.

1. Tell us what compiler you are using.

2. Tell us how you are compiling the files. (Make file? Batch file/script? Project in some Integrated Development Environment? What?)

Regards,

Dave
  #3  
Old 08-Feb-2007, 14:34
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

Re: Odd behavior when removing #include statement from source file


Quote:
Originally Posted by davekw7x
Assuming that putting in the header is the only thing you changed: It wouldn't.

I respectfully suggest that you go back with your finished program and put the #include <iostream> back in and take it out again.
I've done that prior to posting.
Quote:
If it gives you different results (and #including or not #including the header is the only thing you change), then.

1. Tell us what compiler you are using.

2. Tell us how you are compiling the files. (Make file? Batch file/script? Project in some Integrated Development Environment? What?)

I'm using XCode 2 on a Mac.

I guess I'll try it in Dev-C++ on my POS PC and see what happens there.
  #4  
Old 08-Feb-2007, 16:12
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

Re: Odd behavior when removing #include statement from source file


Well, it doesn't seem to exhibit that behavior in Dev-C++. I'm mystified - guess I have to find a specific XCode forum to get answers to the original question.
  #5  
Old 09-Feb-2007, 11:34
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

Re: Odd behavior when removing #include statement from source file


Here's a clue, which doesn't tell me anything, but maybe it'll speak to you guys..

I set up the debugger to watch the variables being passed into function findRootWidth. When the line

CPP / C++ / C Code:
#include <iostream> 

is enabled in the source file, the variable is passed as I expect. When that line is disabled, the variable always shows up as 2.233392082528404e-308, no matter what the argument is.

This is in XCode 2.4.1, GCC 4.0.1, Apple build 5367.

Any thoughts?
  #6  
Old 09-Feb-2007, 12:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,200
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Odd behavior when removing #include statement from source file


Quote:
Originally Posted by earachefl
Any thoughts?


Compile with #include <iostream>. Run from a command line (not from inside XCode IDE).

Compile with //#include <iostream>. Run from a command line.

Regards,

Dave
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not 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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 08:10
help with classes bucho MS Visual C++ / MFC Forum 3 20-Oct-2004 07:16
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28
[Tutorial] Standard I/O aaroncohn C Programming Language 20 27-Feb-2004 22:07

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:48.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.