fgets

This is a discussion on fgets within the C Programming forums, part of the General Programming Boards category; in my book that my teacher is using. it wnats me to write a program that converts a binary number ...

  1. #1
    CaN Opner
    Guest

    fgets

    in my book that my teacher is using. it wnats me to write a program that converts a binary number to a decimal number. and with out using string .h or pointers. the only way i know to store a string is fgets. but as you probaly know fgets stores the array as a char and i need the array as a double for a pow function later on. can any one point me in the right direction on how to convert the array to a double array or to store it origanly as a double array ty so much

  2. #2
    Registered User CaN Opener's Avatar
    Join Date
    Jan 2003
    Posts
    4
    yes that is the main goal of my program but i just need to know how to get the array that i have inputed from fgets witch is a char ro a double so i can use the pow from math.h

  3. #3
    Registered User CaN Opener's Avatar
    Join Date
    Jan 2003
    Posts
    4

    fget type convertion??? HElp PLz

    Here is my code as u can see i tried to convert a array (whitch is a char) ro a double if you could tell me a better way to do this or tell me whats wrong with my code i would be ver great full o and btw the code is suposed to convert a bionary number into a decimal


    Code:
    #include "stdafx.h"
    #include "math.h"
    
    
    void get_string( char buffer[], int size ) ;
    void convert (int temp, double array2[]) ;
    void main (void)
    {
    
    	int i = 0;
    	int temp ;
    
    
    	char array[20] ;
    
    	double array2[20];
    	
    
    
    	printf("Please enter in a binary number: ");
    
    get_string (array,20);
    
    for(i=0; array[i]=='\0' ;i++)
    {
    	temp = i ;
    }
    
    printf("%i",temp);
    for(i =0; i>=20; i++)
    {
    array[i] = (char)array2[i] ;
    }
    
    for(i=0; array[i]!= '\0' ; i++ )
    {
    	convert(temp,array2[i]) ;
    }
    
    }
    
    
    
    /*
    Name		:	get_string - Allows the user to input a string.
    Parameters	:	buffer     - The input string.
    */
    void get_string( char buffer[], int size )
    {
     	char character;
     	int j = 0;		
    
     	do									/* Get a character until newline or 		*/
     	{									/* we run out of characters.				*/
    		character = getchar() ;
    		buffer[j] = character ;
    		++j;
    	}
    	while ( character != '\n' && j < size ) ;
    	
    	while ( character != '\n' )			/* Get rid of extra characters.				*/
    		character = getchar() ;
    
    	buffer[j - 1] = '\0' ;				/* Replace newline with the null-byte.		*/
    }
    
    
    
    void convert (int temp, double array2[])
    {
    double temp3 ;
    double temp4 ;
    int i = 20;
    double power =0;
    
    for(i=temp; i>=0 ; i--)
    
    {
    	temp3 =	pow ( array2[i], power) ;
    	temp4 += temp3 ;
    	power++ ;
    	printf("##%lf",temp3);
    }
    
    printf("%lf,##%lf\n##%i", temp4,temp3,array2[0] ) ;
    
    
    
    }

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Without using string.h or pointers (not directly at least).
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
      printf ( "%d\n", (int)strtol ( "101", NULL, 2 ) );
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User CaN Opener's Avatar
    Join Date
    Jan 2003
    Posts
    4
    printf ( "%d\n", (int)strtol ( "101", NULL, 2 ) );
    what dose the strol function do


    and my program has to have user input for the binary number i cant seem to replace "101" with a varable + the number that goes after null how do u determin the size of that

  6. #6
    Registered User CaN Opener's Avatar
    Join Date
    Jan 2003
    Posts
    4
    printf ( "%d\n", (int)strtol ( "101", NULL, 2 ) );
    what dose the strol function do


    and my program has to have user input for the binary number i cant seem to replace "101" with a varable + the number that goes after null how do u determin the size of that ty for any help

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    99

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    7
    In one of your for loops you had array[i] == '\0' or something symilar, this should be array[i] != '\0'. As far as the convertion, just cast it (for (int i = 0; array[i] != '\0'; i++){longarray[i] = (long) array[i];}). But I don't see why you need pow (), just use something like this:

    int i;

    for (i = 0; array[i] != '\0'; i++);

    ptrarray = (char *) calloc (i / 8, 1);

    for (i = 0; array[i] != '\0'; i++);
    {
    for (int j = 0; j < 8; j++)
    *(ptrarray + i) |= (array[i] & (1 << j));
    }

  9. #9
    squishy
    Guest

    Unhappy ???

    i too have to write a program just like Can opener's. this is what i did...but it doesnt work...could someone help me with the calculations? and find out whats wrong?

    Code:
    #include <stdio.h>
    
    void get_string( char buffer[], int size ) ;
    void binary( char number[] ) ;
    
    const int MAX = 9 ;
    
    /*
    Name		:	get_string - Allows the user to input a string.
    Parameters	:	buffer     - The input string.
    */
    void get_string( char buffer[], int size )
    {
     	char character;
     	int j = 0;		
    
     	do									/* Get a character until newline or 		*/
     	{									/* we run out of characters.				*/
    		character = getchar() ;
    		buffer[j] = character ;
    		++j;
    	}
    	while ( character != '\n' && j < size ) ;
    	
    	while ( character != '\n' )			/* Get rid of extra characters.				*/
    		character = getchar() ;
    
    	buffer[j - 1] = '\0' ;				/* Replace newline with the null-byte.		*/
    }
    
    void main ( void )
    {
    	char number[ MAX ] ;
    
    	printf( "Enter a binary number : " ) ;
    		get_string( number, MAX ) ;
    	
    	binary( number ) ;
    
    	printf( "\nDecimal value : %s\n", number ) ;
    }
    
    void binary( char number[] )
    {
    	int base = 2;
    	int result = 0 ;
    	int i ;
    
    	for ( i = 0 ; number[ i ] != '\0'; i++ )
    	{
    		result = result * base + ( number[ i ] - '0' ) ;
    	}
    }
    if i enter - 00010101 - the decimal value should be 21...but it just outputs 00010101...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 06:33 PM
  2. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 09:10 AM
  3. problem with fgets
    By Smoot in forum C Programming
    Replies: 4
    Last Post: 12-07-2003, 03:35 AM
  4. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 01:08 PM
  5. help with fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 09:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21