Thread: help with an inifite loop

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    8

    help with an inifite loop

    Program goes into an infinite loop, no idea why.
    Right after I enter the first value for what role, it crashes.

    Well I'm guessing it's a loop, but ultimately I have no idea.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int role, type, permit;
        int stickerStudent = 100;
        int stickerStaff = 200;
        int stickerFaculty = 300;
        int hangtagStudent = 150;
        int hangtagStaff = 300;
        int hangtagFaculty = 450;
        
        printf("What role are you? (1=student, 2=staff, 3=faculty)\n");
        scanf("%d\n", role);
        
        if(role == 1)
        {
                printf("What type of tag do you want? (1=sticker, 2= hang tag)\n");
                scanf("%d\n", type);
                if(type == 1)
                {
                        permit = stickerStudent;
                }
                if(type == 2)
                {
                        permit = hangtagStudent;
                }
        }
        
        if(role == 2)
        {
                printf("What type of tag do you want? (1=sticker, 2= hang tag)\n");
                scanf("%d\n", type);
                if(type == 1)
                {
                        permit = stickerStaff;
                }
                if(type == 2)
                {
                        permit = hangtagStaff;
                }
        }
        
        if(role == 3)
        {
                printf("What type of tag do you want? (1=sticker, 2= hang tag)\n");
                scanf("%d\n", type);
                if(type == 1)
                {
                        permit = stickerFaculty;
                }
                if(type == 2)
                {
                        permit = hangtagFaculty;
                }
        }
                
        printf("You need to pay $%d for your parking permit.\n", permit);
        
        system("pause");
        return 0;
    }
    EDIT: I'm an idiot and forgot the & in the scanf.
    Last edited by cheami; 09-23-2009 at 02:40 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Add the ampersand to all your scanf() calls: &

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Your scanf() usage is incorrect. scanf() takes pointers, not values. In other words, it should be:
    Code:
    scanf("%d\n", &type);
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    8
    Thanks for response, figured it out right when I posted it :/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poll event loop
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 04:28 AM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 09:46 PM
  3. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  4. 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
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM