This forum is in archive mode. You will not be able to post new content.

Author Topic: [c++] output statement numbers  (Read 1923 times)

0 Members and 1 Guest are viewing this topic.

Offline rasenove

  • Baron
  • ****
  • Posts: 950
  • Cookies: 53
  • ಠ_ಠ
    • View Profile
[c++] output statement numbers
« on: 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?

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.
« Last Edit: April 23, 2013, 04:49:49 PM by rasenove »
My secrets have secrets...

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: [c++] output statement numbers
« Reply #1 on: 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:)
~Factionwars

Offline rasenove

  • Baron
  • ****
  • Posts: 950
  • Cookies: 53
  • ಠ_ಠ
    • View Profile
Re: [c++] output statement numbers
« Reply #2 on: April 24, 2013, 09:52:02 AM »
Look at the code hot shot.

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

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: [c++] output statement numbers
« Reply #3 on: April 24, 2013, 10:11:34 AM »
Code: (c++) [Select]
//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;
}
~Factionwars

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: [c++] output statement numbers
« Reply #4 on: April 24, 2013, 04:57:27 PM »
Look at the code hot shot.

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

Code: (C++) [Select]
//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).
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.

Offline rasenove

  • Baron
  • ****
  • Posts: 950
  • Cookies: 53
  • ಠ_ಠ
    • View Profile
Re: [c++] output statement numbers
« Reply #5 on: 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.
« Last Edit: April 24, 2013, 05:53:34 PM by rasenove »
My secrets have secrets...

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: [c++] output statement numbers
« Reply #6 on: 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.
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: [c++] output statement numbers
« Reply #7 on: 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
~Factionwars

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: [c++] output statement numbers
« Reply #8 on: 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.
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.