CoolerScouter
CoolerScouter

Reputation: 31

Get char at a defined location in a string in C

I'm looking to read in a number from the keyboard and then I have to manipulate each digit individually (it's an Octal to Decimal converter). Is there something similar to the charAt() method from Java that can be used to work with s particular digit?

I currently have the below code (incomplete) but when compiling, it returns "error: subscripted value is neither array nor pointer"

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

int main()
{
    printf("Please enter an octal number ending with #");
    char nextNum = getchar();
    char number;
    int counterUp = 0; //Records how many digits are entered
    int counterDown = 1; //Records progress during conversion
    int decimalNumber = 0;

    while(nextNum != '#') //reads in the whole number, putting the characters together to form one Octal number.
    {
        number = (number + nextNum);
        counterUp++;
        nextNum = getchar();
    }


    //Begin converson from Octal to Decimal

    while(counterUp >= 0)
    {
        int added = (number[counterUp] * (pow(8, counterDown)));
        decimalNumber = (decimalNumber + added);
        counterDown++;
    }
}

I'm not looking to be told how to go from octal to decimal, just how to work with one digit at a time.

Upvotes: 0

Views: 163

Answers (4)

David Grayson
David Grayson

Reputation: 87386

I think you're used to Java way where you can write something like:

String number = "";
number += "3";
number += "4";

Strings in C do not work like that. This code doesn't do what you think it does:

char number = 0;   // 'number' is just a one-byte number
number += '3';     // number now equals 51 (ASCII 3)
number += '4';     // number now equals 103 (meaningless)

Maybe something like this will work for you:

char number[20];
int i = 0;
number[i++] = '3';
number[i++] = '4';

Or, you could simply use scanf to read a number in from the keyboard.

I recommend that you find a good book about C and read about strings first, then scanf second.

Upvotes: 1

John3136
John3136

Reputation: 29266

You need to define number as an array of chars.

e.g.

char number[16];

Then change your reading loop to append to the array.

while(nextNum != '#') 
{
     number[counterUp] = nextNum;
     counterUp++;
     nextNum = getchar();
}

Upvotes: 0

Crashworks
Crashworks

Reputation: 41374

I think you need to step back and look at your algorithm more closely.

What does char number store? What do you expect this loop to do:

while(nextNum != '#') //reads in the whole number, putting the characters together to form one Octal number.
{
    number = (number + nextNum);
    counterUp++;
    nextNum = getchar();
}

In particular, what does number = (number + nextNum); mean?

Upvotes: 0

Richard J. Ross III
Richard J. Ross III

Reputation: 55533

Use fgets() instead of a single char:

char number[25]; // max of 25 characters in string

fgets(number, 24, stdin); // read a line from 'stdin', with a max of 24 characters
number[24] = '\0'; // append the NUL character, so that we don't run into problems if we decide to print the string

Now you can subscript number at will, e.g. number[10] = 'A'.

Upvotes: 1

Related Questions