Stuck a bit on input

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Stuck a bit on input

 
0
  #1
Mar 29th, 2006
The goal of my program is to take in 2 inputs - one for the base, and one for the number (which should be of that base type). It will then do a
calculation and print out the number in base 10 form. For instance,

Input Output
========== ======
10 1234 1234
8 77 63 (the value of 77 in base 8, octal)
2 1111 15 (the value of 1111 in base 2, binary)

So far, I've been able to do everything except successfully take a numerical input in - I can't think of a way to take individual numbers from a character array and store it into an integer array so that my equations will work properly =\

  1. /*
  2. 1. Read in 2 inputs: base and number
  3. 2. Convert the number to the specified base
  4. 3. Print the results
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9.  
  10. #define SIZE 100
  11.  
  12. void to_bin(int *, int, int);
  13. void to_oct(int *, int, int);
  14. void to_dec(int *, int);
  15. int pow(int, int);
  16.  
  17. int main(void)
  18. {
  19. int base, i = 0;
  20. int num[SIZE] = {0};
  21. char input[SIZE], *ptr;
  22.  
  23. puts("Enter a base number (2, 8, 10):");
  24. scanf("%d", &base);
  25. puts("Enter a number:");
  26. scanf("%s", &input);
  27.  
  28. ptr = input;
  29. while (*ptr != '\0')
  30. {
  31. num[i] = *ptr;
  32. ptr++;
  33. i++;
  34. }
  35.  
  36. if (base == 2)
  37. to_bin(num, i, base);
  38. if (base == 8)
  39. to_oct(num, i, base);
  40. if (base == 10)
  41. to_dec(num, i);
  42.  
  43. return 0;
  44. }
  45.  
  46. void to_bin(int *num, int i, int base)
  47. {
  48. int sum = 0, n = 0;
  49.  
  50. for (;i >= 0; i--)
  51. {
  52. if (num[i] != 0)
  53. sum += pow(n, base);
  54. n++;
  55. }
  56. printf("%d\n", sum);
  57. }
  58.  
  59. void to_oct(int *num, int i, int base)
  60. {
  61. int sum = 0, n = 0;
  62.  
  63. for (;i >= 0; i--)
  64. {
  65. if (num[i] != 0)
  66. sum += (num[i] * pow(n, base));
  67. n++;
  68. }
  69. printf("%d\n", sum);
  70. }
  71.  
  72. void to_dec(int *num, int i)
  73. {
  74. int cnt;
  75. for (cnt = 0; cnt <= i; cnt++)
  76. printf("%d", num[cnt]);
  77. puts("");
  78. }
  79.  
  80. int pow(int n, int base)
  81. {
  82. int cnt, tot = 1;
  83.  
  84. for (cnt = 0; cnt < n; cnt++)
  85. tot *= base;
  86.  
  87. return tot;
  88. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Stuck a bit on input

 
0
  #2
Mar 30th, 2006
hmm, I don't quite get what you mean?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,753
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Stuck a bit on input

 
0
  #3
Mar 30th, 2006
>>I can't think of a way to take individual numbers from a character array and store it into an integer array

In general this often works for characters that are digits:
int integerValue = array[i] - '0';

where '0' is the char representing zero and integerValue will be the integer value of array[i]. This takes advantage of the integer values of characters as assigned by the character set being used and that digits should be sequential in character set with values ascending by one in the range of zero to nine.

Another useful way to do this is with a switch statement to convert symbols to integers. This can be a rather straightforward way to convert characters representing values greater than 9 in bases greater than 10.
  1. int r;
  2. switch(array[i])
  3. {
  4. case 'A':
  5. r = 10;
  6. break;
  7. case 'B':
  8. r = 11;
  9. break;
  10. etc
I'm sure there are other ways as well.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: Stuck a bit on input

 
0
  #4
Mar 30th, 2006
For instance, if I take an input of numbers and store them into a character array, how would I take each individual number and store it into an integer array?

Like if I entered "1111" as my input it would go into the char array as {'1', '1', '1', '1', '\0'}. Now, I want to take each one of those numbers and put it into an integer array so i get {1, 1, 1, 1}. That way I can change a binary input into base 10 form (in this case 1111 -> 15).

I figured out a weird way of doing it =\

Storing each char into temp[0], placing a '\0' in temp[1] so it would be read as a string and then converting it with atoi().
  1. int main(void)
  2. {
  3. int base, i = 0, j = 0;
  4. int num[SIZE];
  5. char input[SIZE], temp[SIZE] = {""}, *ptr;
  6.  
  7.  
  8. puts("Enter a base number (2, 8, 10):");
  9. scanf("%d", &base);
  10. puts("Enter a number:");
  11. scanf("%s", &input);
  12.  
  13. ptr = input;
  14. while (*ptr != '\0')
  15. {
  16. temp[j] = *ptr;
  17. temp[j + 1] = '\0';
  18. num[i] = atoi(temp);
  19.  
  20. ptr++;
  21. i++;
  22. j = 0;
  23. }
  24.  
  25. i -= 1;
  26.  
  27. if (base == 2)
  28. to_bin(num, i, base);
  29. if (base == 8)
  30. to_oct(num, i, base);
  31. if (base == 10)
  32. to_dec(num, i);
  33.  
  34. return 0;
  35. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,753
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Stuck a bit on input

 
0
  #5
Mar 30th, 2006
  1. int i = 0;
  2. while (input[i] != '\0')
  3. {
  4. num[i] = input[i] - '0';
  5. ++i;
  6.  
  7. or
  8.  
  9. while(input[i] != '\0')
  10. {
  11. switch(input[i])
  12. {
  13. case 'A':
  14. num[i] = 10;

Changing each char of the input into a string and using atoi() to convert it is another way, if the char you are trying to change is a digit, but it won't work if you are trying to change hexadecimal into decimal.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: Stuck a bit on input

 
0
  #6
Mar 30th, 2006
If I had to convert hexadecimal i would've had even more headaches :cry:

Thanks for the replies though.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC