Sentinel control is not working

This is a discussion on Sentinel control is not working within the C Programming forums, part of the General Programming Boards category; Essentially my program became an infinite loop and I don't know how to fix this. I want this program to ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    3

    Sentinel control is not working

    Essentially my program became an infinite loop and I don't know how to fix this. I want this program to repeatedly ask the user to enter a wattage until the user inputs -1.


    Code:
                                                                                   #include <stdio.h>
    int main()
    {
    
    int wattage, brightness, n;
    
    printf("Enter the wattage (-1 to quit):\n");
    scanf("%d", &wattage);
    
    while(n!=-1)
    {
    switch(wattage/10)
    {
    
    case 2:  printf("Brightness = 125\n"); break;
    
    case 3:  printf("Brightness = 215\n"); break;
    
    case 4:  printf("Brightness = 500\n"); break;
    
    case 5:  printf("Brightness = 880\n"); break;
    
    case 6:  printf("Brightness = 1000\n"); break;
    
    case 7:  printf("Brightness = 1675\n"); break;
    
    default: printf("\nAn invalid wattage was entered!");
    
    
    n++;
    }
    printf("Enter the wattage (-1 to quit):\n");
    scanf("%d", &wattage);
    
    }
    return 0;
    }


    Thank you, in advance

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > while(n!=-1)
    You're inputting wattage, but testing n ?

    Maybe you should test wattage != -1 instead.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 10-04-2012, 12:36 AM
  2. Help with sentinel control using EOF . . .
    By [Student] in forum C++ Programming
    Replies: 5
    Last Post: 08-19-2011, 12:22 PM
  3. Working with "Sentinel-Exit (-1)".. :{
    By MAV_DevWantB in forum C Programming
    Replies: 5
    Last Post: 09-10-2009, 02:01 PM
  4. Help with sentinel value
    By saahmed in forum C++ Programming
    Replies: 8
    Last Post: 02-15-2006, 05:22 PM
  5. sentinel
    By oscuro in forum C++ Programming
    Replies: 1
    Last Post: 03-12-2002, 04:48 PM

Tags for this Thread


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