Results 1 to 8 of 8

Thread: How's it looking?

  1. #1

    How's it looking?

    On the program I've being working on for around a month now(I'm lazy...), I've finally got it towards final coding stages. Could someone please post any bugs and stuff they see in the raw code?


    Code:
    #include <iostream.h>
    #include "Defs.h"
    
    using namespace std;
    
    long lnTrillion = "1000000000000";
    
    int main();
     {
         cout << "Welcome to the Burn-A-Natar 3000! Type in anything to" << endl; <<
         "continue, or \"exit\" to quit: " << endl; <<;
         
         cin >> INTRO_ANSWER;
         
         if (INTRO_ANSWER == INTRO_COMPARE)
          {
              
              do 
                 ( int z;
                  z = lnTrillFunc ( .0000000000000000002 , .0000000000000000002 );
                  cout << "We're at: " << z << " right now."; )
                  
              while ( z != lnTrillion );
          }
          
          else
          
              {
                  cout << "Goodbye!";
                  
                      return(0);
              }
          }
              
    
        
         double dblTrillFunc ( double dblDeciOne , double dblDeciTwo );
       
       {
           long lnTrillResult;
           
           /* double dblDeciOne(.000000000000000000002);
           double dblDeciTwo(.000000000000000000002); */
           
           lnTrillResult = ( dblDeciOne * dblDeciTwo );
           
           return(lnTrillResult);
       }
    Defs.h:

    Code:
    #define INTRO_ANSWER
    #define INTRO_COMPARE "exit"
    Tell me if you think I\'m spamming or doing something stupid, please.

  2. #2
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,255

    Re: How's it looking?

    Originally posted here by Kamikaze Badger
    On the program I've being working on for around a month now(I'm lazy...), I've finally got it towards final coding stages. Could someone please post any bugs and stuff they see in the raw code?
    For starters, in the do loop you have z = lnTrillFunc, but the function is defined as dblTrillFunc. There also doesn't seem to be a good reason to use Defs.h.
    Chris Shepherd
    The Nelson-Shepherd cutoff: The point at which you realise someone is an idiot while trying to help them.
    \"Well as far as the spelling, I speak fluently both your native languages. Do you even can try spell mine ?\" -- Failed Insult
    Is your whole family retarded, or did they just catch it from you?

  3. #3
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi

    I am sorry, but is it just me, or do others also have the impression that this piece
    of code won't even compile? And if it does, with what kind of compiler?
    First, if you use the std, include <iostream> (without the h). Second,
    you cannot assign a char[1?] to a long (lnTrillion). Third, use int main(){
    instead of int main(); {. Remove the ; after the first endl; Do not stop
    the cout with a "<<", you cannot cin on a preprocessor variable, ...

    And so on. Please, work on it until it at least compiles. Or don't you have available
    a compiler? If not, I recommend gcc[1] for linux or dev-cpp[2] for windows.

    Cheers

    [1] http://gcc.gnu.org/install/binaries.html
    [2] http://www.bloodshed.net/devcpp.html
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  4. #4
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,255
    Originally posted here by sec_ware
    I am sorry, but is it just me, or do others also have the impression that this piece
    of code won't even compile?
    Not on my box anyway. 'tis very broken, much of it syntactically, but some of it logically as well.
    Code:
    weird.cpp:8: invalid conversion from `const char*' to `long int'
    weird.cpp:11: parse error before `{' token
    weird.cpp:14: syntax error before `&gt;&gt;' token
    weird.cpp:20: ISO C++ forbids declaration of `z' with no type
    weird.cpp:20: `lnTrillFunc' was not declared in this scope
    weird.cpp:21: syntax error before `&lt;&lt;' token
    weird.cpp:30: parse error before `{' token
    weird.cpp:34: ISO C++ forbids declaration of `lnTrillResult' with no type
    weird.cpp:34: `dblDeciOne' was not declared in this scope
    weird.cpp:34: `dblDeciTwo' was not declared in this scope
    weird.cpp:35: parse error before `return'
    IOW, broken as hell.

    First, if you use the std, include &lt;iostream&gt; (without the h).
    To elaborate, the .h includes for the standard headers is deprecated. GCC 3.2.2 gives you a warning if you add the .h:
    This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the &lt;X&gt; header for the &lt;X.h&gt; header for C++ includes, or &lt;sstream&gt; instead of the deprecated header &lt;strstream.h&gt;. To disable this warning use -Wno-deprecated.
    Chris Shepherd
    The Nelson-Shepherd cutoff: The point at which you realise someone is an idiot while trying to help them.
    \"Well as far as the spelling, I speak fluently both your native languages. Do you even can try spell mine ?\" -- Failed Insult
    Is your whole family retarded, or did they just catch it from you?

  5. #5
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi

    Since I am in a good mood, eating excellent tagliatelli with gorgonzola sauce,
    and this will be my junior member post, I corrected the errors and modified the
    source code a little bit (because I really could not get a meaning out of it).
    Here it goes:

    Code:
    #include &lt;iostream&gt;
    #include &lt;string&gt;
    using namespace std;
    
    #include &lt;math.h&gt;
    #include "Defs.h"
    
    
    double dblTrillFunc ( double dblDeciOne , double dblDeciTwo );
    double lnTrillion = 1.0e+12;
    
     
    
    int main()
     {
    	double z;
    	long counter;
    
         cout &lt;&lt; "Welcome to the Burn-A-Natar 3000! Type in anything to" &lt;&lt; endl &lt;&lt;   "continue, or \"exit\" to quit: " &lt;&lt; endl ;
         
         cin &gt;&gt; INTRO_ANSWER;
         
    
         if (strcmp(INTRO_ANSWER,INTRO_COMPARE))
          {
    		 counter=0;
             z= 1.0000000000000;
    		 do {
                  
                  z = dblTrillFunc ( 1.002 , z );
                  cout &lt;&lt; "Step: " &lt;&lt; counter &lt;&lt; ". We're at: " &lt;&lt; z &lt;&lt; " right now." &lt;&lt; endl;
                  
    			  counter++;
    
    		 } while ( fabs(z - lnTrillion)/lnTrillion&lt;1.0 );
    		 cout &lt;&lt; "Hey, using interests of interests (and no fees), we reached 1 Trillion $ in " &lt;&lt; counter/365 &lt;&lt; " years!" &lt;&lt; endl;
        	 cout &lt;&lt; "And this, starting with 1$ and an daily conformal interest of 0.002." &lt;&lt; endl;
          }
          
          else
          
              {
                  cout &lt;&lt; "Goodbye!";
                  
                      return(0);
              }
    
    	    return(0);
          }
              
    
        
         double dblTrillFunc ( double dblDeciOne , double dblDeciTwo )
       
       {
           double lnTrillResult;
           
           /* double dblDeciOne(.000000000000000000002);
           double dblDeciTwo(.000000000000000000002); */
           
           lnTrillResult = ( dblDeciOne * dblDeciTwo );
           
           return(lnTrillResult);
       }
    and the Defs.h

    Code:
    //#define INTRO_ANSWER
    char INTRO_ANSWER[1024]; // yeah I know, I am sorry ... :D
    #define INTRO_COMPARE "exit"


    Cheers


    P.s. If you actually want something like arbitrary precision while doing
    calculations, check out MAPM[1].


    [1] http://www.tc.umn.edu/~ringx004/mapm-main.html
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  6. #6
    double dblTrillFunc ( double dblDeciOne , double dblDeciTwo );

    Just a quick look over, on that line, you don't want a semi-colon at the end of the line. When you are actually defining your Functions, you don't put a semi-colon after your declaration.

    You only use a semi-colon, when you are stating it as a prototype, and when you are actually making a call to it.

  7. #7
    Ok, did some work on it with the help of you guys and guys from other forums, and here's the updated(and heavily fixed) code:


    Code:
    #include &lt;iostream&gt;
    #include &lt;string&gt;
    
    using namespace std;
    
    
    
    double dblTrillFunc ( double dblDeciOne , double dblDeciTwo )
       
       {
           //long lnTrillResult;
           
           /* double dblDeciOne(.000000000000000000002);
           double dblDeciTwo(.000000000000000000002); */
           
           double lnTrillResult = ( dblDeciOne * dblDeciTwo );
           
           return(lnTrillResult);
       }
    
    
    string intro_answer;
    
    string intro_compare = "exit";
    
    long lnBillion = 1000000000;
    
    double z;
    
    int main()
     {
         cout &lt;&lt; "Welcome to the Burn-A-Natar 3000! Type in anything to" &lt;&lt; endl &lt;&lt; \
    "continue, or \"exit\" to quit: " &lt;&lt; endl;
         
         cin &gt;&gt; intro_answer;
         
         if (intro_answer != intro_compare)
          {   
              do 
                 {
                  z = z + dblTrillFunc ( .0000000000000000002 , .0000000000000000002 );
                  cout &lt;&lt; "We're at: " &lt;&lt; z &lt;&lt; " right now.\n";
                 }    
                  
              while ( z &lt; lnBillion );
          }
          
          else
          
              {
                  cout &lt;&lt; "Bye";
                  
                      return(0);
              }
          }


    That compiles, but with the result of z, it also outputs 4e-blahblah. Anyone know what the reason is?




    And I wrote all this in Dev-C++. As you can see, my skills are advancing as I read more of the C++ tutorial at cplusplus.com.
    Tell me if you think I\'m spamming or doing something stupid, please.

  8. #8
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi

    Two comments on the code
    1. always initialise variables to be on the "safe side": double z=0.0;
    2. always return a value, if your function has a non-void return type.
    Code:
    	  return(0);
    } // int main(){
    Concerning your question: "outputs 4e-38":

    A number like 1000 can be written as 10^3 (ten to the power of 3),
    another notation is 1e+3 (three 3 zeroes after the 1).
    A number like 0.001=1e-3 (if you shift the dot to the right 3 times, you get "1.0").
    "cout" by default uses the "1e-3"-notation.

    In your case: .000000000000000000002 = 2e-19.
    And the following calculation:
    .000000000000000000002*.000000000000000000002 =
    2e-19 * 2e-19 = 4e-38.


    Cheers
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •