Thread: scanf with stopping condition

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Indonesia
    Posts
    8

    scanf with stopping condition

    I wanted to input some numbers with scanf function,
    i can enter some numbers and if I input -1 to the scanf, the input must end. And the scanf function has limited input, the max that I can input is 40 numbers.
    example if enter 1 2 4 6 5 4 -1
    the scanf function will ended and the result will be appear.
    I wanted to know
    how the scanf function is like that would be best for this problem,
    Code:
    scanf("%d", &n);
    the result if I input those number will be like
    |
    ||
    ||||
    ||||||
    |||||
    ||||


    I am very beginning on c , please anyone can help me??

  2. #2
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    need to make an array
    need to make a for loop, where 40 is the max
    need to make a break out of the for loop if -1 is entered
    need to make another for loop, that stops at the last input from previous loop

    get some code down, so we can help further

  3. #3
    Registered User
    Join Date
    Mar 2013
    Location
    India
    Posts
    2
    Code:
    int n[40]={0},i,j,top;
    for(i=0; i<40 && n[i]!=-1; i++)
      scanf("%d",&n[i]);
    top = i-1;
    for(i=0;i<=top;i++)
    {
       for(j=0;j<n[i];j++)
         printf("|");
       printf("\n");
    }

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    sachin must think we run this forum like google.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While loop not stopping for scanf for some reason. please help!
    By Charlie Lesoine in forum C Programming
    Replies: 14
    Last Post: 11-29-2011, 03:06 PM
  2. stopping in else block
    By linuxman in forum C Programming
    Replies: 3
    Last Post: 03-24-2004, 08:35 PM
  3. Stopping an outside application
    By Korhedron in forum Windows Programming
    Replies: 8
    Last Post: 07-29-2003, 12:51 AM
  4. Stopping pop-ups...
    By dbaryl in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 05-13-2002, 07:24 AM
  5. stopping at eof
    By FastFish in forum C++ Programming
    Replies: 2
    Last Post: 02-14-2002, 12:14 PM

Tags for this Thread