#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");
}
elseif(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:
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?
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
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.
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.
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.