multiple inputs of multiple types

This is a discussion on multiple inputs of multiple types within the C Programming forums, part of the General Programming Boards category; Im having trouble dealing with a program that takes multiple inputs (which can be a character, decimal, octal, hex) in ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    5

    multiple inputs of multiple types

    Im having trouble dealing with a program that takes multiple inputs (which can be a character, decimal, octal, hex) in any order. finding out if the input is a character or a digit.

    When using argv it stores it as a string in a char array. I need to convert my input to a long or char depending on what it is. But I don't know the order, so i don't know what to expect. I trired using isalpha() but get Segmentation fault when executing.

    If I knew what it was I could use strol(). Any ideas on how to deal with this??? Thank you.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Post the code you have along with any warnings and errors the compiler gives.

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    5
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    void convert();
    
    int main(int argc, char* argv[]){
            convert(argc,argv);
            return 0;
    }
    
    void convert(int size, char* element[]){
    
    	unsigned i;
    	for(i=1;i<size;i++){
    		if(isalpha(element[i])){
    			char buffer;
       			strcpy(buffer,element[i]);
    			printf("%c  %d  %x\n", buffer,buffer,buffer);
    		}
    		else{
    			long buffer;
    			if(isxdigit(element[i])){
    				buffer = strtol(element[i],NULL,16);
    				printf("%c  %d  %x\n", buffer,buffer,buffer);
    			}
    			else{
    				strtol(element[i],NULL,10);
    				printf("%c  %d  %x\n", buffer,buffer,buffer);
    			}
    		}
    	}
    }
    during compile:

    convert.c: In function `convert':
    convert.c:18: warning: passing arg 1 of `strcpy' makes pointer from integer without a cast


    Thanks.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    You forgot to include string.h
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You're having a hard time working with an array of pointers to char and you have signed mismatches. Then of course there is not including string.h. This compiles, but it won't work properly:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    void convert();
    
    int main(int argc, char* argv[]){
      convert(argc,argv);
      return 0;
    }
    
    void convert(int size, char* element[]){
    
      int i;
      for(i=1;i<size;i++){
        if(isalpha(element[i][0])){
          char buffer[BUFSIZ];
          strcpy(buffer,element[i]);
          printf("%c  %d  %x\n", buffer,buffer,buffer);
        }
        else{
          long buffer;
          if(isxdigit(element[i][0])){
            buffer = strtol(element[i],NULL,16);
            printf("%c  %d  %x\n", buffer,buffer,buffer);
          }
          else{
            buffer = strtol(element[i],NULL,10);
            printf("%c  %d  %x\n", buffer,buffer,buffer);
          }
        }
      }
    }
    My best code is written with the delete key.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    First glance things:
    You are allocating only one char for buffer but are trying to use it as a string. That is the reason for
    convert.c:18: warning: passing arg 1 of `strcpy' makes pointer from integer without a cast
    error
    Last edited by Thantos; 01-19-2004 at 02:52 PM.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >1) Unless you are using a C99 compiler you can't declare variables after statements in C.
    Variables can be declared only at the beginning of a block in C89, the OP's code is legal as this constraint is not violated anywhere in the posted code.
    My best code is written with the delete key.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    When testing I figured that out and removed it from my post. Thanks for the reminder though Prelude.

  9. #9
    Registered User
    Join Date
    Jan 2004
    Posts
    5
    Thanks for the help, I think im on the right track now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generalising methods to handle multiple weapon types
    By Swarvy in forum Game Programming
    Replies: 2
    Last Post: 05-22-2009, 03:52 AM
  2. checking inputs meets multiple conditions
    By 60beetle60 in forum C Programming
    Replies: 5
    Last Post: 04-19-2008, 09:25 AM
  3. Reading multiple data types to file
    By Wicket in forum C++ Programming
    Replies: 2
    Last Post: 04-05-2008, 11:14 AM
  4. multiple types in one decleration
    By hebele in forum C++ Programming
    Replies: 1
    Last Post: 10-17-2004, 08:23 AM
  5. Getting multiple inputs using cin
    By edk in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2001, 03:34 PM

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