-3

When using getchar() in C, you get a ascii value for the character entered. If you needed to loop through values entered using getchar() and convert the number entered ('.' included) to floating point how would convert the value and also read in multiple lines based on the how many values the user wants to enter? So far, I managed to get the user input check for how many values they want to convert to floating point by using a while loop with a counter. My issue is using the inner loop to convert the value from getchar() one at a time to float where the number ends up being formatted to floating point. Any ideas?

char input;
float value;
int count = 0;
int i = 0;
scanf("%i", count)
while(i < count)
{
    input = getchar();
    while(input != '\n')
    {
        input = input - '0';
        printf("%d",value);
    }
    i++;
}

I'm stuck either getting infinite loops or 0.0 values after read in. Any ideas?

MD_90
  • 5
  • 1
  • 6
  • 1
    If you use scanf (incorrectly, but that's fixable) for the integer at the beginning, why not for the floating point numbers too? – Carl Norum Sep 05 '16 at 02:50
  • wanting to learn getchar() more so I want to avoid using scanf. Only use it to read in the counter for the overall loop – MD_90 Sep 05 '16 at 02:53
  • @MD_90: if you want to skip scanf(), then just use fgets(). But honestly, better let the library do the parsing, than to let your own code do it (probably) wrong. – siride Sep 05 '16 at 02:55
  • the goal is to avoid scanf which is a requirement for this project but im having issues with converting individual characters from getchar to their real values instead of ascii and being able to format the output for each number using printf – MD_90 Sep 05 '16 at 02:57
  • suppose to avoid most c functions accept getchar and printf and no arrays – MD_90 Sep 05 '16 at 03:05
  • Work it out on paper first. If I gave you the number "10.2" what basic maths would you use to convert each digit into its corresponding numeric value. Anyway, your attempt is obviously wrong even without knowing the algorithm since you don't even use the `input` value that was read. Probably you meant to type `input` for some of the places where you currently have `value`. – kaylum Sep 05 '16 at 03:25
  • if im right you would use a loop and read each char value with getchar() then convert to float until you reach end of line then print and move on to the next entry. Problem is my code isnt working that way. It seems im checking for the '\n' wrong – MD_90 Sep 05 '16 at 03:48
  • How do you expect `input` to ever equal `\n` in the inner loop? all you ever do is subtract `'0'` from it, so it just keeps getting smaller and smaller... – John3136 Sep 05 '16 at 03:59
  • "then convert to float". Not quite. You need to convert each digit to a float and add it to the previous result. Also, now you have fixed the previous typo and gone too far - now you are not storing any result in `value`. Please pay attention to the details - these are basic mistakes that you really should be able to pick up yourself rather than relying on others to point it out to you. – kaylum Sep 05 '16 at 04:02
  • so it would be while ( input != '\n') { float convert = value - '0'; printf(convert); } ? – MD_90 Sep 05 '16 at 04:31
  • No. Don't just code without thinking. What is stored in `value`? Nothing. So how could that possibly be correct. – kaylum Sep 05 '16 at 04:47
  • I suggest you work through the book The C Programming Language from K&R. There are quite some exercises that will teach you these and similar techniques. – Ely Sep 05 '16 at 04:53
  • normally I've been okay with programming just been a bit since ive taken a course in programming. So, I wrote out the idea and I came to the conclusion that logically you need to loop first with the number of iterations for the count of lines the user wants for numbers to be converted. Next you would read in the char value from getchar() and convert it to the decimal it should be since 3 in ascii isnt same as 3 in decimal. you would then loop til end of line and after converting with a float variable print out the digit in the output right? sadly, its not working that way – MD_90 Sep 05 '16 at 05:08
  • I feel as though im missing a step – MD_90 Sep 05 '16 at 05:08

1 Answers1

0

As per mentioned the actual problem in the comments, I have edited the code and written it according to the things you described.

#include<stdio.h>

void main()
{
    int count = 0;
    int i = 0;
    int count_digits=0;
    char output[50];        //For storing the input
    scanf("%d",&count);
    while(i < count)
    {
        printf("\n\nInput:%d       ",i+1);     //For printing the input number.
        scanf("%s",&output);            //taking input as a String

        int char_no=0;                  //character no pointer
        count_digits=0;                 //for counting number of characters (in our case : Digits) before decimal points which is then to be printed after decimal points

        while(output[char_no]!='\0' && output[char_no]!='.')  //printing all digits before decimal point or print whole number if it doesn't contain decimal point
        {
            printf("%c",output[char_no]);     //printing character(Digit)
            char_no++;                  //incrementing character number
            count_digits++;             //Counting total number of digits before decimal point
        }



        if(output[char_no]!='\0')       //If it is not the end of character array means number contains decimal point
        {
            printf("%c",output[char_no]);   //printing decimal point as a character
            char_no++;                  //Incrementing character pointer
        }
        else
            printf(".");               //If number doesn't contain decimal point it will print it.

        while(count_digits>0 && output[char_no]!='\0')  //loop will run till no of digits after decimal point gets printed or reaching at the end of input
        {
            printf("%c",output[char_no]);   //printing the character as it is
            char_no++;              //incrementing character number
            count_digits--;       //decrementing total no of digits to be printed after decimal point as one character gets printed.
        }

        while(count_digits>0)    //if input ends but still the no of digits after decimal points are less than the no. of digits before decimal points, It will print zeros to make no.of digits after and before decimal points equal
        {
            printf("0");        //printing 0
            count_digits--;     //decrementing total no of digits to be printed after decimal point as one character gets printed.
        }
        i++;
    }
}

Sample Output for the above is: Output Hope this will solve your problem.

Sanket Makani
  • 2,491
  • 2
  • 15
  • 23
  • I thought getchar() returns an int? plus what if they enter a decimal number for input? – MD_90 Sep 05 '16 at 05:44
  • If you want to get directly ascii value by `getchar()` , You can directly assign it like `int ascii_char = getchar();` which gives ascii value of the character or `char c = getchar(); printf("%d",c);` both will give same output. @MD_90 – Sanket Makani Sep 05 '16 at 05:49
  • say user enters 5 for how many lines of numbers they want to convert to floating point. First number is 10.2. I need to read in the one from getchar() then convert it to 1 in float format then getchar gets 0 do same then the decimal comes in then the 2 all following the same procedure. The rest of 4 lines of numbers follow the same idea. What am i doing wrong? These numbers have to be formatted with 2 points after the decimal – MD_90 Sep 05 '16 at 06:04
  • What do you want in output for the input `10.2` ? Do you want output `10.20`? – Sanket Makani Sep 05 '16 at 06:09
  • 10.20 theres 2 before the decimal and 2 after – MD_90 Sep 05 '16 at 06:12
  • So you want same no. of digits before decimal points , after the decimal points? Suppose for input `100.3` , Output should be `100.300`? – Sanket Makani Sep 05 '16 at 06:14
  • yeah thats the idea. I feel like im getting closer but im not sure how to loop the 5 lines and loop the digit and convert them to that format – MD_90 Sep 05 '16 at 06:18
  • im not sure how to proceed – MD_90 Sep 05 '16 at 06:54
  • What is the maximum input for your program? – Sanket Makani Sep 05 '16 at 07:00
  • Looks very good Sadly only getchar is allowed and no arrays only loop structures like for while and do. Scanf is okay for entering the counter for how many sequences of values the user wants to use but only getchar is allowed after that point. I know you can use printf for formatting like say .2f after decimal and 2 before. In my inner while loop is it wrong to loop while not equal to a new line or should I use a different check? If else and switch are allowed – MD_90 Sep 05 '16 at 15:45