How you write code

closed account (EwCjE3v7)
Hello there I was wondering how you guys write your code, use the following template(you may edit), and this is my format

#include <istream>
using std::cout using std::endl; using std::cin;

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
   int x = 0; // test comment
   int y[2] = {1, 2};
   
   cin >> x;
   
   for (int z = 2; z != 10; ++z) {
      cout << z << " times" << endl;
   }
}
   


How would you write this code?
closed account (3hM2Nwbp)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
static_assert(__clang__ && __cplusplus >= 199711L, "Your compiler is not supported.");

#include <iostream>
/// Triple slash to include in auto-generated documentation
int main()
{
   // test comment (two slashes to exclude from auto-generated documentation)
   int x = 0;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
   auto y = {1, 2};
#pragma clang diagnostic pop
   std::cin >> x;
   for (int z = 2; z != 10; ++z)
   {
      std::cout << z << " times\n";
   }
   std::cout << std::endl;
}


* Reason being that I'm trying to force as many people as possible to switch to the obviously superior cross-platform compiler. :)
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <vector>

int main()
{
	int x = 0; //test comment
	std::vector<int> y = {1, 2};
   
	std::cin >> x;
   
	for (int z = 2; z < 10; ++z)
	{
		std::cout << z << std::endl;
	}
}
Last edited on
My C++ is rusty. I honestly been spending too much time doing UnrealScript. I hope in UE4 the C++ code support is not going to be too Unreal Engine specific or far off from actual C++ code.

So I'd probably write that code like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main() {

    //test comment
    int x = 0;
    int y[2] = { 1, 2 };

    std::cin >> x;

    for(int z = 2; z < 10; z++) {
        std::cout << z << std::endl;
    }

}


And if it was longer then I'd probably add in "using namespace std;" at the top.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

namespace{

   /* Type conversion function.  I use this so that I can basically treat all data
       input and output as characters.*/
    template<class type1, class type2>
    inline type2 conv(const type1& t1)
    {
         std::stringstream ss;
         type2 t2;
         ss<< t1;
         ss>> t2;
         return t2;
    }
}

int main()
{
    using namespace std; //namespace for only this function.
    int an_int(0);
    int arr[2] = {1, 2};
    
    {
         string temps("");
         cin.rdbuf()->in_avail(); /*remove anything in the buffer.  We want to make sure that the user 
                                enters somthing*/

         cin>> temps;
         an_int = conv<string, int>(temps);
    }
    for(short x = 2; x < 11; x++) cout<< x<< " "<< ((x != 1) ? "times" : "time")<< endl;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main(){
   std::cin.get();

   const int n = 10;
   for(int K=2; K<n; ++K)
      std::cout << K << " times\n";

   return 0;
}
Last edited on
It really depends on what I'm doing and whether I'm being lazy or not. I use std::, but if I'm being lazy (testing something) I just do using namespace std;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <array>

int main()
{
    int x( 0 ); // test comment
    std::array<int, 2> arr = { 1, 2 };

    std::cin >> x;
   
    for( std::size_t i = 2; i < 10; ++i ) {
        std::cout << i << " times\n";
    }
}
Last edited on
closed account (EwCjE3v7)
Thanks guys, I just wanted some tips to make my code clear(er)
And yea I love clang but can`t get it working with Code::Blocks :(
Topic archived. No new replies allowed.