Help a rookie

This is a discussion on Help a rookie within the C Programming forums, part of the General Programming Boards category; I'm trying to write a program that will take as an input a string of characters and output their hex ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    17

    Help a rookie

    I'm trying to write a program that will take as an input a string of characters and output their hex representation.

    i decided to start off by writing a program that takes in one input character and outputs its hex representation, and it worked. Code below:

    Code:
    #include <stdio.h>
    
    main()
    {
       int i;
    
        i=getchar();
        printf("its hex character is %3x\n", i);
    }
    then I tried to take it to the next step by having my program understand a string of characters and output its hex characters, here is my attemp:

    Code:
    #include <stdio.h>
    #define MAXLINE 100
    main()
    
    {
       
       int i;
       char line[MAXLINE];	
       line[i] = getchar ();
    
        
        printf("%s, its hex character is %3x\n", i);
    }
    as you can see maybe I'm trying non-sense but I tried to define a char line(maximun number of bits to be 100). then I wanted the input line typed by the keyboard to be placed in the string to be printf as a '%s' string of character is hex format. Any help would be appreciated it, hence this is basically my first program, i am trying to write to learn this language

    elrookie

  2. #2
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142
    There is no any loop.You read always the first element of the word

    #include <stdio.h>
    #define MAXLINE 100
    main()

    {

    int i;
    char line[MAXLINE];
    for(i=0;i<MAXLINE;i++)
    line[i] = getchar ();


    printf("&#37;s, its hex character is %3x\n", i);
    }
    Also you try print a string without palacing at printf and you print i which is a number only.
    CAn you explain what hex is?
    Last edited by ch4; 06-13-2007 at 08:38 AM.

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    If you look at your printf statement:
    Code:
    printf("%s, its hex character is %3x\n", i);
    You are printing a string, then a hex value. But after that you only have 'i', which is an undefined integer.

    This compiles:
    Code:
    #include <stdio.h>
    #define MAXLINE 100
    main()
    
    {
       
       int i;
       char line[MAXLINE];	
       line[0] = getchar ();
    
        
        printf("its hex character is %3x\n", line[0]);
        getchar();
        getchar();
        
    }
    I changed a couple of things here. It will only print the first characters hex value. To print them all in a sequence you will need to loop through the characters.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    17
    Quote Originally Posted by ch4 View Post
    There is no any loop.You read always the first element of the word



    Also you try print a string without palacing at printf and you print i which is a number only.
    CAn you explain what hex is?
    humm ok this is starting to make sense now,

    this is the thought i follow when it comes to 'for' loops:

    for (initialisation; condition; iteration) command;


    so if i understand it right I want to loop through the characters as mike is also saying.

    Code:
    #include <stdio.h>
    #define MAXLINE 100
    main()
    
    {
       
       int i;
       char line[MAXLINE];	
    
       for ( i=1;i<=100;i++)
       line[i] = getchar ();
    
        
        printf("its hex character is &#37;3x\n", line[i]);
        getchar();    
    }
    but now it's not outputing anything, what's wrong with my comand? i think my initialisation; condition; iteration are correct ?

    example
    hence the hex character of J is 4A
    so my input is J and it outpus 4A

    thanks fort the help so far
    Last edited by elrookie; 06-13-2007 at 09:21 AM.

  5. #5
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Now you are looping a hundred times, getting a character each time. This means the user would have to enter 100 characters.

    Instead use a method to get a string such as fgets(). Example:
    Code:
    #include <stdio.h>
    #define MAXLINE 100
    int main()
    {
       int i=0;
       char line[MAXLINE];	
       fgets(line, MAXLINE, stdin);
       printf("The string &#37;s has a hex value of: ", line); 
       while(line[i] != '\0')
       {
           printf("%3x ", line[i]);
           i++;        
       }    
       getchar();    
    }
    [edit] Basically you want to be looping through the characters to convert, not the characters that the user enters. Oh yeah something I overlooked: You should use
    Code:
    int main()
    not
    Code:
    main()
    [[/edit]
    Last edited by mike_g; 06-13-2007 at 09:27 AM.

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    17

    re:

    ohh that program works great,

    ohhh i didn't know about 'fgets'.... can you write this without using fgets?

    how come i want to use int main?

  7. #7
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Quote Originally Posted by elrookie View Post
    ohh that program works great
    Its not perfect, theres an extra 'a' on the end of the string for some reason.

    Quote Originally Posted by elrookie View Post
    ohhh i didn't know about 'fgets'.... can you write this without using fgets?
    you could use gets(), but fgets() validates the input size and since you already had MAXLINE set up you may as well use fgets, as its better.

    Quote Originally Posted by elrookie View Post
    how come i want to use int main?
    Main returns a non zero if there is an error, you should allways use int main().

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Always use fgets() in place of gets().

    Also, if you feel the need to write random code, look up a tutorial or something on the subject that you're writing for.

  9. #9
    Registered User
    Join Date
    Jun 2007
    Posts
    17

    re:

    i like that function fgets() i see it is used a lot to read characters and input, i came across a getline() it was said that it can do the same, i wanted to implement this using the same idea mike and is not quite working, what can change about my code to correctly implement this idea, I tried getline by itself and i can use it to count characters. Can this be done ? I know fgets() works but i need to learn more since i am quite new at this.


    Code:
    #include <stdio.h>
    #define MAXLINE 100
    int main()
    int getline (char s[], int maxline);
    
    { 
       int i=0;
       char line[MAXLINE];	
       getline(line, MAXLINE, stdin);
        
       printf("the string &#37;s has a hex character of: ", line);
       while (line[i] != '\0')
    
        {
    	printf("%3x", line[i]);
    	i++;
        }
    
        getchar();
        
    }
    
    int getline (char s[], int maxline)
    
    { 
    	int c, i;
    	for ( i=0; ( c=getchar() ) != EOF && c!='\n'; ++i) 
    	s[i] = c;
    
    	if ( c =='\n') {
    		s[i] = c;
    		++i;
    	}
    	
    	s[i] = '\0';
    
    	return i;
    }
    c:4 error: declaration for parameter "getline" but no such parameter
    c:9 error: too many arguments to function
    Last edited by elrookie; 06-13-2007 at 11:46 AM.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Don't write these functions if you don't know what you're doing. You have to break down the problem to very small parts.

    Simple version of getline() I wrote just now for you:

    Code:
    char *getline(char *szBuffer, size_t stLen)
    {
    	size_t i;
    	int c;
    	for(i=0;i<stLen;i++)
    	{
    		c = fgetc(stdin);
    		if(c == EOF)
    		{
    			return NULL;
    		}
    		else if(c == '\n')
    		{
    			return szBuffer;
    		}
    		szBuffer[i] = c;
    	}
    	return szBuffer;
    }

  11. #11
    Registered User
    Join Date
    Jun 2007
    Posts
    17

    re:

    thank you, but could you tell me where my fault lies in my code so at least i could understand why it doesn't work

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    First problem is that you're not using maxline at all. You just keep writing, and writing, and writing, and you'll overrun your block of memory if the user keeps typing.

    In terms of calling the function, look at the arguments that getline() accepts. Look at what you're passing it. See a conflict?

  13. #13
    Registered User
    Join Date
    Jun 2007
    Posts
    17

    re:

    you are right looking at the arguments that getline() accepts and looking at what i'm passing confuses the crap out of me, i can't use getline() then.

    Code:
    #include <stdio.h>
    #define MAXLINE 5
    main()
    
    {
       
       int i;
       char line[MAXLINE];	
    
       for ( i=1;i<=5;i++)
       line[i] = getchar ();
    
        
        printf("its hex character is %3x\n", line[i]);
        getchar();    
    }
    here is a version of my program that i was trying to make it work, all i need is to loop through the characters to convert instead of looping the given characters (as Mike suggested right before he introduced fgets()). How can I manipulate a loop in this case to do what i want for the code above? this is killing me now very slowly and painfully

  14. #14
    Registered User
    Join Date
    May 2006
    Posts
    903
    *cough*int main*cough*

  15. #15
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    your printf tries to print element 5 which is out of bounds array
    BTW indexes of the array go from 0 to 4 so even your for is buggy
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rookie in need of some compiler startup help
    By Skizdigidy in forum C++ Programming
    Replies: 6
    Last Post: 07-09-2006, 01:15 PM
  2. Student average program rookie in need of help
    By Mshock in forum C Programming
    Replies: 24
    Last Post: 05-17-2006, 12:51 AM
  3. Rookie pointer problem: 2 dimensions, malloc
    By GatesAntichrist in forum C Programming
    Replies: 11
    Last Post: 12-20-2005, 11:35 AM
  4. my code.. remember i am a rookie dont laugh
    By aubrey in forum C Programming
    Replies: 2
    Last Post: 11-24-2002, 09:08 PM
  5. Rookie needs a lead
    By DirtElk in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 07:27 AM

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