| | |
Stuck a bit on input
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2006
Posts: 131
Reputation:
Solved Threads: 0
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 =\
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 =\
C Syntax (Toggle Plain Text)
/* 1. Read in 2 inputs: base and number 2. Convert the number to the specified base 3. Print the results */ #include <stdio.h> #include <stdlib.h> #define SIZE 100 void to_bin(int *, int, int); void to_oct(int *, int, int); void to_dec(int *, int); int pow(int, int); int main(void) { int base, i = 0; int num[SIZE] = {0}; char input[SIZE], *ptr; puts("Enter a base number (2, 8, 10):"); scanf("%d", &base); puts("Enter a number:"); scanf("%s", &input); ptr = input; while (*ptr != '\0') { num[i] = *ptr; ptr++; i++; } if (base == 2) to_bin(num, i, base); if (base == 8) to_oct(num, i, base); if (base == 10) to_dec(num, i); return 0; } void to_bin(int *num, int i, int base) { int sum = 0, n = 0; for (;i >= 0; i--) { if (num[i] != 0) sum += pow(n, base); n++; } printf("%d\n", sum); } void to_oct(int *num, int i, int base) { int sum = 0, n = 0; for (;i >= 0; i--) { if (num[i] != 0) sum += (num[i] * pow(n, base)); n++; } printf("%d\n", sum); } void to_dec(int *num, int i) { int cnt; for (cnt = 0; cnt <= i; cnt++) printf("%d", num[cnt]); puts(""); } int pow(int n, int base) { int cnt, tot = 1; for (cnt = 0; cnt < n; cnt++) tot *= base; return tot; }
•
•
Join Date: Jul 2005
Posts: 1,753
Reputation:
Solved Threads: 283
>>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.
I'm sure there are other ways as well.
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.
C Syntax (Toggle Plain Text)
int r; switch(array[i]) { case 'A': r = 10; break; case 'B': r = 11; break; etc
•
•
Join Date: Mar 2006
Posts: 131
Reputation:
Solved Threads: 0
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().
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().
C Syntax (Toggle Plain Text)
int main(void) { int base, i = 0, j = 0; int num[SIZE]; char input[SIZE], temp[SIZE] = {""}, *ptr; puts("Enter a base number (2, 8, 10):"); scanf("%d", &base); puts("Enter a number:"); scanf("%s", &input); ptr = input; while (*ptr != '\0') { temp[j] = *ptr; temp[j + 1] = '\0'; num[i] = atoi(temp); ptr++; i++; j = 0; } i -= 1; if (base == 2) to_bin(num, i, base); if (base == 8) to_oct(num, i, base); if (base == 10) to_dec(num, i); return 0; }
•
•
Join Date: Jul 2005
Posts: 1,753
Reputation:
Solved Threads: 283
C Syntax (Toggle Plain Text)
int i = 0; while (input[i] != '\0') { num[i] = input[i] - '0'; ++i; or while(input[i] != '\0') { switch(input[i]) { case 'A': 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.
![]() |
Similar Threads
- My Screen Stuck At 4 Bit And 640 X 480 Pixels!! Help Please (Windows NT / 2000 / XP)
- Keyboard input or "stuck in a loop" (C++)
Other Threads in the C Forum
- Previous Thread: partition algorithm question
- Next Thread: I developd a COM to custom IE printing
| Thread Tools | Search this Thread |
Tag cloud for C
#include ansi array arrays asterisks binarysearch calculate centimeter changingto char command convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic fflush file fork forloop framework functions getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware histogram homework inches include incrementoperators input iso kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard multi mysql number opendocumentformat opensource owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv research reversing scanf scripting segmentationfault sequential shape socket socketprograming spoonfeeding standard string strings structures student systemcall testing threads turboc unix user variable voidmain() wab windowsapi






