EvilZone
Programming and Scripting => C - C++ => : 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.
-
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:)
-
Look at the code hot shot.
My code cant be more formated to be "reader friendly".
-
//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;
}
-
Look at the code hot shot.
My code cant be more formated to be "reader friendly".
//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).
-
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.
-
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.
-
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
-
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.