Thread: Section Finder

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    7

    Section Finder

    I have written a program which acts as a contents page for an on-line version of the programming book. When the user types the section number the start page of that section is displayed.

    I'm not sure if I have used an array correctly to store section numbers.

    It should terminate when user enters zero but it doesn't work.

    Code:
    
    
    #include <stdio.h>
    int main ()
    
    
    {
     char section; 
     int section_number [10]= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
     
     while (section!=0)
     
     {
     for (section=1; section<10; section++)
      {
      printf ("\nPlease enter the section number between 1 and 10: ");
      scanf ("%d", &section);
     
       printf ("\nSection '%d' of C programming book starts at page '%d'\n", section, section_number); 
      }
      
      break;
     }
     
     return 0; 
     }

    Thank you.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I don't think you understand what your loops are doing. Plus, section is an uninitialized variable when you first access it in your while statement.
    Run the code below and and try to understand what it's doing and why. Then rework your code and post it back if you're still having troubles.
    Code:
    #include <stdio.h>
    int main ()
    {
        char section = 1; 
    
        printf("before while\n");
    
        while (section!=0)
        {
            printf("before for\n");
    
            for (section = 1; section < 10; section++)
            {
                printf("in for: section = %d\n", section);
            }
    
            printf("after for\n");
            break;
            printf("after break\n");
        }
    
        printf("after while\n");
    
        return 0; 
    }
    gg

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    7

    section finder

    I'm using dev c++ compiler and when i run it the screen flash it for few seconds so i can't see the result.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Sounds like a FAQ problem to me.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    Thanks a lot. Now I'll try to understand it. I doubt i will....

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Soul.India
    Thanks a lot. Now I'll try to understand it. I doubt i will....
    With that attitude, you're probably right...

    Try this:
    Comment your code with information that explains what you are trying to do. Then we might be able to explain why the statements you are using will or will not accomplish your goal.

    This is actually a suggestion to ALL posters looking for help...
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In need of guidance
    By ajdspud in forum C++ Programming
    Replies: 7
    Last Post: 06-01-2016, 02:23 AM
  2. Maths For Game Programming?
    By kolliash in forum Game Programming
    Replies: 13
    Last Post: 09-18-2008, 11:53 AM
  3. Help with mprotect and writing on code section
    By raghu2383 in forum Linux Programming
    Replies: 3
    Last Post: 06-20-2008, 02:40 AM
  4. What's up with the Tips & Tricks section?
    By bikr692002 in forum C++ Programming
    Replies: 3
    Last Post: 04-05-2006, 08:04 PM
  5. Personal People Finder
    By Coder87C in forum C++ Programming
    Replies: 1
    Last Post: 02-12-2005, 04:26 PM