how to transform this loop..

This is a discussion on how to transform this loop.. within the C Programming forums, part of the General Programming Boards category; i cant use loops in the program and i need to enter the string in a row without telling what ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    how to transform this loop..

    i cant use loops in the program
    and i need to enter the string in a row
    without telling what its length first.
    this loop does exactly that
    but i cant use for/while in the program
    Code:
    printf("enter string\n");
     for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch != EOF; ++i)
        {
            input2[i] = ch;
        }
    
        input2[i] = '\0';
    how to substitute it?

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    use fgets
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    58
    How 'bout something like this?

    Code:
    $ cat prog.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main ()
    {
     char input2[100];
     char *strptr;
    
     printf("enter string\n");
    
     fgets(input2, sizeof(input2), stdin);
     strptr = strchr(input2, '\n');
     *strptr = '\0';
    
     printf("input2 [%s], len[%d]\n", input2, (int) strlen(input2));
    
     exit (0);
    
    }
    
    $ gcc -Wall prog.c
    
    $ a.out
    enter string
    abcdef
    input2 [abcdef], len[6]

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    you should check the return value of fgets and strchr
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    can i do it with gets ,scanf??

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    58
    Quote Originally Posted by vart View Post
    you should check the return value of fgets and strchr
    Yep. Your're right.

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i was told to use gets
    is it possible that "gets" will do the same thing as this loop?

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    perhaps
    Code:
    char str[40];
    scanf("%39c", str);

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    58
    Quote Originally Posted by transgalactic2 View Post
    i was told to use gets
    is it possible that "gets" will do the same thing as this loop?
    Technically, you could, but you run the risk of a buffer overflow. Even the gcc compiler issues a warning against using gets.

    Code:
    $ gcc -Wall x.c
    
    : warning: the `gets' function is dangerous and should not be used.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    but i cant use for/while in the program
    setjmp/longjmp, do-while, goto, recursion?

    But a much better course of action would probably be to thump your teacher with the printouts of mine and Salem's comments as to the usefulness of "learning to tie your shoes with one hand" type exercises. The C language has for, while, do-while to make loops. It is not Lisp or Scheme where (many) problems must be solved by recursion.

    In fact, I do not believe that recursion is a particularly good way to solve problems, with a few exceptions where we know that the number of recursions is relatively small, and the solution is CLEARLY made simpler by recursion. Solving any of my current programming tasks with recursion would probably result in a code-review-comment of "Take that back and rewrite it", since our systems have limited stack-space.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    is there a way using gets/scanf/getch() combination ??

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    is there a way using gets/scanf/getch() combination ??
    That has been described above. getch() is as good/bad as getchar() in the sense that you still need a loop. gets() is VERY BAD ALWAYS (but as I stated elsewhere, if YOU HAVE to do that to solve your task, to not get deducted points "for using functions that you haven't been told about", then as long as you do it for THAT reason, and not some other reason, I'd say it's OK).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by itCbitC View Post
    perhaps
    Code:
    char str[40];
    scanf("%39c", str);
    this command stops when i press enter and puts only the chars that i entered
    or it waits for 40 chars??

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    to read a string upto 39 chars stopping on the first white space use
    %39s format
    to stop on the end-line use
    %39[^\n] format
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #15
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i want to write 10 chars out of those 40
    or 13 out of those 40
    press enter an then
    all those 10 chars will go to the array
    Code:
    scanf("%39[^\n]c", str);
    like this
    ??

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  3. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-19-2005, 11:30 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

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