C newbie help on arrays

This is a discussion on C newbie help on arrays within the C Programming forums, part of the General Programming Boards category; I am trying to take a user input, the split it into seperate varibale so that I can then reorder ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    16

    C newbie help on arrays

    I am trying to take a user input, the split it into seperate varibale so that I can then reorder so

    string input = 123456789
    output = 654789123

    I know this is basic but really struggling with.... I have posted where I am upt o so far.... please go easy I am 3 hours into this.....

    thanks




    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int main()
    {
    /* set variables / array */
    char string[13];
    char first;
    char second;
    char third;

    printf( "Please enter string with no colons: " );

    /* notice stdin being passed in */
    fgets ( string, 13, stdin );
    printf( string );

    first = string[0];
    second = string[1];
    third = string[2];

    printf( first );
    printf( second );
    printf( third );

    getchar();
    return(0);

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    1. Use [code] for your code.
    2. It is better write main(void) instead main().
    3. The syntax of your printf function is wrong (printf(string). You can use puts(string) or printf("%s", string); Also take a look in the last 3 printf of your code.
    4. You must close you code with "}".
    5. When using printf if you want to print a character use %c instead %s which is for printing strings.


    I hope i helped you...
    Last edited by brack; 09-10-2010 at 06:55 AM.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    16
    Use [code] for your code. - will do

    It is better write main(void) instead main(). - noted

    The syntax of your printf function is wrong (printf(string). You can use puts(string) or printf("%s", string); Also take a look in the last 3 printf of your code. - the printf (sstring) works fine, but have changed to you recommendation, still cannot get the 3 at the bottom to work?

    You must close you code with "}". - forgot to copy that bit...

    When using printf if you want to print a character use %c instead %s which is for printing strings. - dont understand this...

    In MS-DOS this is so much more straight forward....

    Can you spoonfeed a little more.....

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Letmein.

    You've made several changes to the code, can you repost what you have currently, and use the code tags - (that # icon) in the editing window.

    That'll make it easier to see what's up.

    You don't need to "split it into separate variables", because each element of the array can be accessed by using the array[index number].

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Quote Originally Posted by letmein View Post
    Use [code] for your code. - will do

    It is better write main(void) instead main(). - noted

    The syntax of your printf function is wrong (printf(string). You can use puts(string) or printf("%s", string); Also take a look in the last 3 printf of your code. - the printf (sstring) works fine, but have changed to you recommendation, still cannot get the 3 at the bottom to work?

    You must close you code with "}". - forgot to copy that bit...

    When using printf if you want to print a character use %c instead %s which is for printing strings. - dont understand this...

    In MS-DOS this is so much more straight forward....

    Can you spoonfeed a little more.....
    lets see the 3 last printf...

    when you want to print a string, you print it like this:
    Code:
    // buf = "My mama is cooking";
    printf("%s", buf);
    // %s shows us that buf is a string, and %c prints characters, just to make sure you know it
    Right? So,
    if you want to printf a character what would you change in the above code? It' s easy...

    Also note that in your code, first, second, third is characters...

  6. #6
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Code:
    printf("Hello, World!\n");    // Use to print string literals.
    printf("%s\n", string);        // Use to print string variables.
    printf("%c\n", ch);              // Use to print single characters.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    16
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
         /* set variables / array */
        char MAC[13];
        char first;
        char second;
        char third;
    
         printf( "Please enter MAC with no colons: " );
    
    
        /* notice stdin being passed in */
        fgets ( MAC, 13, stdin );
        printf("%s",MAC);
        puts(MAC);
    
    /* first = MAC[0];
    second = MAC[1];
    third = MAC[2];
    
    puts( first );
    puts( second );
    puts( third );*/
    
    
    
        getchar();
        return(0);
    The above compiles and runs.

    I have tried printf("%s",MAC[1]); to print to screen the 2 character of the array it compiles but then get run error?

    In my minfd i want to printf("%s",MAC[7]) and printf("%s",MAC[6]) etc so that to screen is showns character7character6 etc

    The MAC input can contain letters or numbers, this why I have stayed with char instead of int.... is this right?

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    16
    Got it working, thanks for all the help, I have no doubt I'll have a few more questions coming up....

    Code:
      printf("%c", MAC[9]); printf("%c", MAC[8]);

  9. #9
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Quote Originally Posted by letmein View Post
    [CODE]#include <stdio.h>


    The MAC input can contain letters or numbers, this why I have stayed with char instead of int.... is this right?
    actually it contains only characters this is what it sees. But yes it can print numbers as 1, 2, 3... or bill, nick, a,...

    TIP: when you have a char number to convert it to int use atoi();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie question about 'sizeof' and arrays
    By Sharke in forum C Programming
    Replies: 27
    Last Post: 12-09-2009, 06:30 AM
  2. newbie problems with pointers arrays and functions.
    By MegaManZZ in forum C++ Programming
    Replies: 6
    Last Post: 01-08-2008, 05:33 PM
  3. newbie question?? vectos versus arrays
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 07-30-2002, 06:29 PM
  4. pointers, arrays, and functions oh my (newbie Q)
    By eazhar in forum C++ Programming
    Replies: 6
    Last Post: 07-21-2002, 07:26 PM
  5. Newbie Help (Arrays, Structures, Functions,Pointers)
    By tegwin in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 06:29 PM

Tags for this Thread


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