EvilZone
Programming and Scripting => C - C++ => : parad0x March 11, 2013, 10:44:44 AM
-
This simple base convertor converts between base 2 and and base 16(including them).
Sorry, I didn't wrote comments. ;)
#include <stdio.h>
int convertedNumber[64];
long int numberToConvert;
int base;
int digit = 0;
void getNumberandBase ();
void convertNumber ();
void showConvertedNumber ();
int main() {
getNumberandBase();
convertNumber();
showConvertedNumber();
return 0;
}
void getNumberandBase () {
printf("Enter a number to be converted: ");
scanf("%li", &numberToConvert);
printf("Enter the base: ");
scanf("%i", &base);
if(base < 2 || base > 16){
printf("You entered a wrong base.\n The base should be between 2 and 16.\n");
base = 10;
}
}
void convertNumber (){
do {
convertedNumber[digit] = numberToConvert % base;
++digit;
numberToConvert/= base;
}while(numberToConvert != 0);
}
void showConvertedNumber() {
const char baseDigits[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
int nextDigit;
printf("Converted number = ");
for(--digit; digit >= 0; --digit) {
nextDigit = convertedNumber[digit];
printf("%c", baseDigits[nextDigit]);
}
printf("\n");
}
-
Just a quick note:
When you're declaring func. prototypes, you're also supposed to declare the parameters and their types.
Since your functions don't take any, this would be
- void getNumberandBase (void);
-
-
- void convertNumber (void);
-
-
- void showConvertedNumber (void);
This doesn't change the program or anything. But it's a convention and helps the compiler to point out if there are inconsistancies b/w your func prototype and function calls, i.e. if the order/type/number of parameters.don't match.
-
Just a quick note:
When you're declaring func. prototypes, you're also supposed to declare the parameters and their types.
Since your functions don't take any, this would be
- void getNumberandBase (void);
- void convertNumber (void);
- void showConvertedNumber (void);
This doesn't change the program or anything. But it's a convention and helps the compiler to point out if there are inconsistancies b/w your func prototype and function calls, i.e. if the order/type/number of parameters.don't match.
Thanks, I'll take care of that.
-
You should avoid using global variables in your programs, instead declare it in a function and pass it by reference into other functions.
The reason for this is once you start making larger programs, any function anywhere in the problem can change it, so if you or a partner decide to change the value of a global variable somewhere in the program and it creates a bug, you would have to sift though 1000s of line of code to find it.
Global variables should only be declared as a constants to avoid this.
-
Just a quick note:
When you're declaring func. prototypes, you're also supposed to declare the parameters and their types.
Since your functions don't take any, this would be
- void getNumberandBase (void);
-
-
- void convertNumber (void);
-
-
- void showConvertedNumber (void);
This doesn't change the program or anything. But it's a convention and helps the compiler to point out if there are inconsistancies b/w your func prototype and function calls, i.e. if the order/type/number of parameters.don't match.
Fairly well said. I think it should be added that, as indicated, the type is important though not the variable name. This means that you can declare a prototype as such:
int func(int, char, char*, char*, userType*);
You should avoid using global variables in your programs, instead declare it in a function and pass it by reference into other functions.
The reason for this is once you start making larger programs, any function anywhere in the problem can change it, so if you or a partner decide to change the value of a global variable somewhere in the program and it creates a bug, you would have to sift though 1000s of line of code to find it.
Global variables should only be declared as a constants to avoid this.
Absolutely agreed. Avoid using global variables unless absolutely necessary. This is a lesson best learned now rather than later. Even at work, in a professional environment with learned individuals, I have to suffer through code that was not written with this in mind and thus I am constantly finding small bugs where a global value has been changed without concern for what else needs to use that variable. This is made far worse by multithreaded code. Adding mutexes to protect the use of global variables is not a solution and introduces further problems, particularly in performance. Since I am paid salary, it is a serious waste of my time to run through the 2+ GBs of source that we maintain at work to resolve issues with global variables that never should have appeared in the code to begin with.
-
-- Placeholder --
What?
-
Hes going modify that post and write something when he gets time. Mean while, that post will hold the place for him to write.