1

I have modified the code a bit and have what is below. The number that is output is more realistic now but it still isn't correct (for example, inputting 54# results in an output of 36 when it should put out 44)

#include <stdio.h>
#include <math.h>

main()
{
    printf("Please enter an octal number ending with # \n");
    int nextNum = getchar();
    int number[100];
    int numberOfSlots = 0; //Records how many digits are entered
    int power = 0; //Sets power of 8 during conversion
    int decimalNumber = 0;
    int i=0;

    while(nextNum != '#') //reads in the whole number, putting the characters together to form one Octal number.
        {
             if(nextNum >='0' && nextNum <='9')
                nextNum = (nextNum - '0');
             else{printf("Oops! That's not a valid number!");}

             number['i'] = (nextNum);
             //numberOfSlots++;
             i++;
             nextNum = getchar();
printf("%d\n", number['i']);

    }


    //Begin converson from Octal to Decimal

    for(i; i > 0; i--)
    {
        decimalNumber = decimalNumber + (number['i'] * pow(8,power));
        power++;
    }

    printf("%d", decimalNumber);

}
CoolerScouter
  • 31
  • 2
  • 6

6 Answers6

2

Since this is homework, I'll give a hint rather than an answer:

The function getchar() returns a character rather than a number. A character has a value based on the character encoding of the system (see ASCII).

Look at the value that's actually assigned to number and convert from what you are getting to what the numeric value actually should be.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
1

Eric has already pinpointed the reason. Here's what you need to do

int nextNum= getchar();
if ( nextNum >='0' && nextNum <='9' )
    nextNum = nextNum -'0';

number[numberOfSlots] = nextNum;

And take off the initial getchar() at the time of declaration and make it generic inside the loop

Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125
0

in printf("%d", number[numberOfSlots]); you are accessing an index that has not been initialized (the counter numberOfSlots has been incremented beforehand.)

Either move the incrementation after the printf or access the element via numberOfSlots-1 in the printf

The above will also be a problem in the conversion loop as the last increment on numberOfSlots points past the last valid element in number.

Also, as Eric J. said, getchar() returns an integer code of the character, not the number the character represents (usually it is the ASCII value, but it is platform dependent).

Attila
  • 28,265
  • 3
  • 46
  • 55
0
decimalNumber = (decimalNumber + (number[numberOfSlots] * (pow(8, power))));

You are begging for trouble by using the pow() function. For large integers the floating point values will no longer hold integer values and you'll have round off errors. You should never mix in the use of floating point math in these kinds of situations. You should use an integer variable instead.

fdk1342
  • 3,274
  • 1
  • 16
  • 17
0
#include <stdio.h>

main(){
    printf("Please enter an octal number ending with # \n");
    int number[21];
    int numberOfSlots = 0;
    int decimalNumber = 0;
    int nextNum, i;

    for(nextNum=getchar();nextNum != '#'; nextNum=getchar()){
        number[numberOfSlots++] = nextNum - '0';
    }

    for(i=0; i< numberOfSlots ; ++i){
        decimalNumber = decimalNumber * 8 + number[i];
    }

    printf("%d", decimalNumber);

}
/*
17777777777#
2147483647
*/
#if 0
#include <stdio.h>

int main(){
    int n, ch;
    printf("Please enter an octal number ending with # \n");

    for(n=0 ; '#' != (ch = getchar()); ){
        n = n * 8 + ch - '0';
    }

    printf("%d", n);
    return 0;
}
#endif
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
0

fixed

#include <stdio.h>
#include <math.h>

main()
{
    printf("Please enter an octal number ending with # \n");
    int nextNum = getchar();
    int number[100];
    int numberOfSlots = 0; //Records how many digits are entered
    int power = 0; //Sets power of 8 during conversion
    int decimalNumber = 0;
    int i=0;

    while(nextNum != '#') //reads in the whole number, putting the characters together to form one Octal number.
        {
             if(nextNum >='0' && nextNum <='9'){
                nextNum = (nextNum - '0');
                number[numberOfSlots] = nextNum;
                numberOfSlots++;

             } else{
                printf("Oops! That's not a valid number!");
             }

             nextNum = getchar();

    }


    //Begin converson from Octal to Decimal

    for(i=numberOfSlots-1; i >= 0; i--)
    {
        decimalNumber = decimalNumber + (number[i] * pow(8,power));
        power++;
    }

    printf("%d", decimalNumber);

}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70