I'm really trouble with fgets i think

This is a discussion on I'm really trouble with fgets i think within the C Programming forums, part of the General Programming Boards category; Hi, I wrote some piece of code that should have been printf the parts of input.Let me give an example ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    46

    I'm really trouble with fgets i think

    Hi, I wrote some piece of code that should have been printf the parts of input.Let me give an example since my english not enough to explain it . If user give an input like A 7638 2303. since its starts with 'A' code should give me 7638 and 2303 one by one.I tried to it and wrote a code like that but output is nothing.It does not give error or warning but also it does not give any output just a blank.Where am i wrong please help me : (

    Code:
    #include<stdio.h>
    int main(){
    char input1[129],input2[129];
    char text[260];
    int i,j;
    fgets(text,260,stdin);
               if(text[0]=='A'){
                         for(i=2; ;i++){
                                if(text[i]==' '){
                                       j=i;
                                       printf("%d \n",i);
                                       break;    }
    
    
                                 else{
                                       input1[i]=text[i];}
                                       }
    
    
    
    
                          while(text[j]!='\0'){
                                  input2[j]=text[j];
                                  j+=1;}
                                          }        
    
    
                                              
    printf("%s %s \n",input1,input2);
    return 0;
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your spacing is very bad. Fix it. An indentation (preferably spaces) of 4 is enough.

    Other than that, your code doesn't make sense to me.

    How about:
    Code:
    char letter;
    int n1 = 0, n2 = 0;
    scanf(" %c", &letter); // initial space in format to skip leading spaces
    if (letter == 'A') {
        scanf("%d %d", &n1, &n2);
    }else{
        scanf("%d", &n1);  // assuming you read only one number if letter is not A
    }
    printf("%d %d\n", n1, n2);
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    You copy text[i] to input1[i]. But remember, you started i at 2, not 0, so 7638 is put into input1[2] through input1[5]. You do something similar in copying text into input2. Also you need to null terminate input1 and input2 when you're done copying. There are far better solutions than this, like sscanf or strtok + strcpy. Any reason you aren't using those

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets trouble
    By sababa.sababa in forum C Programming
    Replies: 2
    Last Post: 11-13-2009, 11:57 AM
  2. trouble with fgets() !
    By davewang in forum C Programming
    Replies: 13
    Last Post: 11-25-2008, 03:13 PM
  3. fgets help?
    By twitch in forum C Programming
    Replies: 2
    Last Post: 06-10-2007, 06:49 AM
  4. fgets ?
    By marrk in forum C Programming
    Replies: 2
    Last Post: 10-07-2006, 07:24 PM
  5. fgets
    By Axel in forum C Programming
    Replies: 16
    Last Post: 09-15-2006, 12:09 PM

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