EvilZone

Programming and Scripting => C - C++ => : rasenove April 23, 2013, 04:49:04 PM

: [c++] output statement numbers
: rasenove April 23, 2013, 04:49:04 PM
just had a small question, is it a bad idea to code a program in a like  this?  (http://upload.evilzone.org/download.php?id=7082436&type=rar)

NOT PUTTING THE CODE HERE BECAUSE THE NARROW PAGE MAKES IT LOOK UGLY... :(

i only one used output statement to display all contents instead of using a statement for each line.
Just wanted to know if its good or bad.
: Re: [c++] output statement numbers
: Stackprotector April 24, 2013, 09:36:11 AM
Please post it on pastebin or here. Anyway, i Can already say that if your code is too long for this page it's a bad code style:)
: Re: [c++] output statement numbers
: rasenove April 24, 2013, 09:52:02 AM
Look at the code hot shot.

My code cant be more formated to be "reader friendly".
: Re: [c++] output statement numbers
: Stackprotector April 24, 2013, 10:11:34 AM
: (c++)
//the code is to display data type size

#include <iostream>


int main(void)
{
    std::cout<< "Basic types:\n\n"
           
           
     << "bool        :\t"
     << sizeof(bool       ) << " bit(s)\n"
     
     << "char        :\t"
     << sizeof(char       ) << " bit(s)\n"
     
     << "w_char_t    :\t"
     << sizeof(wchar_t    ) << " bit(s)\n"      // wide char type
     
     << "short       :\t"
      << sizeof(short      ) << " bit(s)\n"
     
     << "int         :\t"
     << sizeof(int        ) << " bit(s)\n"
     
     << "long        :\t"
     << sizeof(long       ) << " bit(s)\n"
     
     << "long long   :\t"
     << sizeof(long long  ) << " bit(s)\n\n\n" //available in c++ 11
     
     << "Float types:\n\n"
     
     
     << "float       :\t"
     << sizeof(float      ) << " bit(s)\n"
     
     << "double      :\t"
     << sizeof(float      ) << " bit(s)\n"
     
     << "long double :\t"
     << sizeof(long double) << " bit(s)\n\n\n"
   
     << "User defined types:\n\n";     //the below comment is a size identifier template
             
             
   //  << " type_name  :\t" << sizeof(type_name)   << "bit(s)\n";
       //dont forget to remove the semicolon
   
    return 0;
}
: Re: [c++] output statement numbers
: bluechill April 24, 2013, 04:57:27 PM
Look at the code hot shot.

My code cant be more formated to be "reader friendly".

: (C++)
//the code is to display data type size

#include <iostream>

using namespace std;

int main(void)
{
    cout << "Basic types:" << endl << endl;
           
    cout << "bool        :\t" << sizeof(bool       ) << " bit(s)" << endl;
    cout << "char        :\t" << sizeof(char       ) << " bit(s)" << endl;
    cout << "w_char_t    :\t" << sizeof(wchar_t    ) << " bit(s)" << endl;      // wide char type
    cout << "short       :\t" << sizeof(short      ) << " bit(s)" << endl;
    cout << "int         :\t" << sizeof(int        ) << " bit(s)" << endl;
    cout << "long        :\t" << sizeof(long       ) << " bit(s)" << endl;
    cout << "long long   :\t" << sizeof(long long  ) << " bit(s)" << endl << endl << endl; //available in c++ 11
     
    cout << "Float types:" << endl << endl;
     
     
    cout << "float       :\t" << sizeof(float      ) << " bit(s)" << endl;
    cout << "double      :\t" << sizeof(float      ) << " bit(s)" << endl;
    cout << "long double :\t" << sizeof(long double) << " bit(s)" << endl << endl << endl;
   
    cout << "User defined types:" << endl << endl;     //the below comment is a size identifier template
             
             
   //  << " type_name  :\t" << sizeof(type_name)   << "bit(s)\n";
       //dont forget to remove the semicolon
   
    return 0;
}

This is how you should have done the code.  Always use multiple cout statements because it's a lot clearer in my mind.  Also, you should pretty much *never* use \n in C++ (unless you specifically want unix newlines and even then it's still better practice to use endl) because it may be a \r\n or a \n for the new line character.  Always use std::endl because that will use the correct version.

Also, Factionwars is right.  If the code is too long to fit in a reasonable amount, you should split it up into functions (assuming that's possible; there are rare exceptions).
: Re: [c++] output statement numbers
: rasenove April 24, 2013, 05:50:01 PM
Thanks for the answer bluechill, but is that the only reason i should use a  cout statement for displaying each content ? To make it look clear?
I used only one cout statement becaus i thought the program would run faster than that becaus it has only one statement to execute.

And my code looks long becaus i aligned all the smiller contents, which is a general rule to make something easy to read/display.
: Re: [c++] output statement numbers
: bluechill April 24, 2013, 05:52:30 PM
Thanks for the answer bluechill, but is that the only reason i should use a  cout statement for displaying each content ? To make it look clear?
I used only one cout statement becaus i thought the program would run faster than that becaus it has only one statement to execute.

And my code long becaus i aligned all the smiller contents, which is a general rule to make something easy to read/display.

So cout is not a function.  What is actually doing the work is the "<<" operator which you already have anyways.  Therefore it's just cleaner and easier to read with the cout.
: Re: [c++] output statement numbers
: Stackprotector April 24, 2013, 05:52:51 PM
Thanks for the answer bluechill, but is that the only reason i should use a  cout statement for displaying each content ? To make it look clear?
I used only one cout statement becaus i thought the program would run faster than that becaus it has only one statement to execute.

And my code long becaus i aligned all the smiller contents, which is a general rule to make something easy to read/display.
You could alsook use a stringstream and cout that once
: Re: [c++] output statement numbers
: bluechill April 24, 2013, 08:46:26 PM
You could alsook use a stringstream and cout that once

If he's going for efficiency that's probably worse than just doing:

string output = "";

output += <blah>

and then cout << output;  That will be less time consuming than outputting individually.  Stringstream probably just overcomplicates the matter but it'd work too.