programming style

Hi all
I don't know if this is the best place for this quastion but here it goes.
I wrote this "fake" program:
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
#include<stdio.h>
int main(){
        int i, a, total=0;
        for(i=0;i<4;i++){
                printf("a = ");
                scanf("%d",&a);
                switch(a){
                        case 1: a+=1;
                                break;
                        case 2: a*=2;
                                break;
                        default:break;
                }
                total+=a;
        }
        if(total<10){
                printf("low\n");
                printf("bust do better\n");
        }
        else    if(total<50)
                        printf("medium\n");
                else{   printf("Hello");
                        printf(" ");
                        printf("world!");
                        printf("\n");
                }
        printf("total = %d\n",total);
        return 0;
}

The question is what do you recommend me in this matter?
I would like hear opinions from experienced C/C++ programmers.
How ould you write the same code?
Thanks

Are you trying to learn C or C++? If C++, you should get away from printf and scanf and use cout and cin instead.

Also, a minor thing, but I would decrease your tab size. Most people tend to use a tab size of somewhere between 2 and 4 spaces. An 8 space tab can make code unreadable after just a few levels of indentation.
That is straight-up C, which is fine.
It looks fine. I just have two comments.

Line 5. I know you know that this means 'give me a number', but for programs you intend to distribute you should be as explicit as possible about your prompts. For example, "Please enter an integer number: ".

Lines 22..25. You should generally try to use I/O functions as rarely as possible. C++ syntax actually tends to encourage people to do this:
1
2
3
4
5
/* C */                // C++
printf( "Hello"  );    cout << "Hello"
printf( " "      );         << " "
printf( "world!" );         << "world!"
printf( "\n"     );         << endl;

Instead, write as much as you can at once:
1
2
3
4
/* C */
printf( "Hello world!\n" );
// C++
cout << "Hello world!\n";


Otherwise, looks good!
There's always one, and I guess it might as well be me.
I definitely DON'T like this particular style of bracing:

openingbrace{

} closingbrace

I prefer

{ openingbrace

}closingbrace

but each to his own
Thanks
I is not important if using C or C++ for my question point of view. Just interested in the way the code flows.
I agree with not using the 8tabs. I used anjuta and it comes with this value by default. It realy makes code dificult to read.
About the coment on thlines 22..25 i just used 4 printf functions to have a code block. So where would you put the {} for instance?
1
2
3
4
5
6
7
8
9
10
11
        if(total<10){
                printf("low\n");
                printf("bust do better\n");
        }
        else    if(total<50)
                        printf("medium\n");
                else{   printf("Hello");
                        printf(" ");
                        printf("world!");
                        printf("\n");
                }

or
1
2
3
4
5
6
7
8
9
10
11
12
13
14
        if(total<10)
        {
                printf("low\n");
                printf("bust do better\n");
        }
        else    if(total<50)
                        printf("medium\n");
                else
                {
                        printf("Hello");
                        printf(" ");
                        printf("world!");
                        printf("\n");
                }

or
1
2
3
4
5
6
7
8
9
10
11
12
13
14
        if(total<10)
                {
                printf("low\n");
                printf("bust do better\n");
                }
        else    if(total<50)
                        printf("medium\n");
                else
                       {
                        printf("Hello");
                        printf(" ");
                        printf("world!");
                        printf("\n");
                       }

or ...
Just trying to figure out the best way :)

Last edited on
I'd use
1
2
3
4
5
6
7
8
9
10
11
if (total<10){
	printf("low\n");
	printf("bust do better\n");
}else if (total<50)
	printf("medium\n");
else{
	printf("Hello");
	printf(" ");
	printf("world!");
	printf("\n");
}

Also, case labels should have their own lines and so should else keywords and if statements.

guestgulkan: I don't see what you mean.
Last edited on
guestgulkan is saying he does not like the
1
2
3
/*...*/ {
    //...
}

brace style and instead prefers
1
2
3
4
/*...*/
{
    //...
}
The else on line 22 is indented as if it belongs logically to the if on line 20, but it's more common to indent it as if it belongs to the if on line 16. I.e. remove one tab from all lines 21 through 26
Last edited on
I will take a look in The GNU coding standards.
http://www.gnu.org/prep/standards/
Did anybody here already read it?
Found some good tips on chapter 5.
Acording to Richard Stallman none of our styles is the most apropriate :)
I think i will give it a trie.
Last edited on
He must know, right? After all, he did write the GNU Hu- Oh, wait.
Yeah, I don't think I'll take style advice from activists.
Yeah, RS knows what he is doing, but that doesn't stop him from opening his mouth too often. :-)

As far as "standards" go, the only standards you will ever encounter are those given you by your boss or agreed upon by your work group. In your own stuff, develop your own style. Play with it. Change it. Etc. Just try to be
1) readable
2) consistent

and you will do fine. Anyone whose nose isn't too full of snot will have little trouble reading consistently-organized code.

My $0.02
My $0.02

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* File Comment Should Go Here Etc
 * This file contains:
 * Author: etc
 */

#include<stdio.h>

int main() {
  
  int a;
  int total = 0;
  
  for (int i = 0; i < 4; ++i) {
    cout << "a = ";
    cin >> a;
    
    switch (a) {
      case 1:
        a += 1;
        break;
      case 2:
        a *= 2;
        break;
      default:
        break;
    }
    total += a;
  }
  
  if (total < 10) 
    cout << "low\nbust do better" << endl;
  else if (total < 50)
    cout << "medium" << endl;
  else
    cout << "Hello, World!" << endl;
  
  // OR
  if (total < 10) {
    cout << "low\nbust do better" << endl;
  } else if (total < 50) {
    cout << "medium" << endl;
  } else {
    cout << "Hello, World!" << endl;
  }
  // Don't Mix Braces and No-Braces in the same If-Else Statements
  
  cout << "total = " << total << endl;
  
  return 0;
}
default: break;
Doesn't make sense to me. Just leave that away.
I also think it it is more readable if you always use braces even if you have just a single line of code. This way it will be much easier for you to change you code a month or two months afterwards.
Topic archived. No new replies allowed.