Thread: how to cast a char to an int

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    25

    how to cast a char to an int

    Hi, I actually have two questions about this code. One is in the subject line. I want to basically read in a string and make sure it is an integer which works fine. But then I want to cast it to int so I can actually use it. I know that what I have only returns the pointer but I don't know how to get the actual value. I have tried many things and always get "segmentation fault". My other question is about fgets. The second loop has an "fgets" but it skips it the first time for some reason, which is why I have that flag=0 so it goes through the loop again and then it works fine. I've tried using fflush(stdin) in several places but always the same thing.

    Code:
    #include <stdio.h>
    #include "math2.h"
    
    int main(void)
    {
    	int i,x,flag,z;
    	char *y, c[BUFSIZ];
    	y=c;
    
    	do {
    		printf("Please enter a 0 or 1: ");
    		scanf("%d",&x);
    
    	} while (x!=0&&x!=1);
    
    	do {
    		printf("\nPlease enter a number to evaluate: ");
    		fflush(stdin);
    		fgets(y, BUFSIZ, stdin);
    
    
    		flag=0;
    
    		for(i=0;i<strlen(y)-1;i++) {
    			if (*(y+i)<'0' || *(y+i)>'9') {
    				flag=0;
    				break;
    			} else {
    				flag=1;
    			}
    		}
    
    	} while (flag==0);
    
    	if (x==0) {
    		printf("\nThe factorial of %s is: %d\n",y,fact((int)*y));
    	} else {
    		printf("\nThe fibonaccia sequence is %d\n",fibonacci((int)*y));
    	}
    
    	exit(0);
    }
    Thanks for any help you can provide.

  2. #2
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    fflush is for output streams not input streams.

    You have c[BUFSIZE], why not use that?

    Code:
    fgets(c, BUFSIZE, stdin);
    
    y = malloc(strlen(c) + 1);
    
    j=0;
    
    for(i=0; c[i] != '\0'; i++)
    {
        if(isdigit(c[i]))
            y[j++] = c[i];
    }
    
    y[j] = '\0';
    
    anum = atoi(y);
    
    /* do something with anum */
    
    free(y);
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  2. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM