Thread: Help with using Arrays with While loop and sentinel value in C

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    7

    Help with using Arrays with While loop and sentinel value in C

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define SIZE 50
    main()
    {
    	int value = 0, num[SIZE] = { 0 };
    	int i = 0;
    
    
    	printf("Enter up to 50 numbers. (enter -999 to stop)\n");
    	
    	while (num[i] != -999 || value != 50)
    	{
    		scanf_s("%i", &num[i]);
    		value++;
    		printf("Enter a new number.\n");
    	}
    
    
    	for (i = 0; i < SIZE; i++)
    	{
    		printf("%i\n", num[i]);
    	}
    
    
    
    
    	system("pause");
    }
    i am having trouble getting my Array to work with the sentinel values i have placed in the while loop. the user is supposed to be able to input up to 50 numbers or type -999 to end the program and then the program outputs all the numbers entered; neither of the sentinel values i have placed are working though and i'm not sure why i would appreciate any kind of help or nudge in the right direction.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > scanf_s("%i", &num[i]);
    > value++;
    Your index never increments.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sentinel controlled do-while loop
    By theCanuck in forum C++ Programming
    Replies: 10
    Last Post: 03-22-2011, 11:07 PM
  2. Sentinel controlled loop
    By arjay in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 04:30 AM
  3. Sentinel while loop and errors.
    By lollypop in forum C Programming
    Replies: 12
    Last Post: 10-19-2003, 12:40 PM
  4. Sentinel Loop?
    By TerryBogard in forum C Programming
    Replies: 1
    Last Post: 12-17-2002, 01:11 AM
  5. sentinel terminated loop/avg calculation
    By Sway in forum C++ Programming
    Replies: 3
    Last Post: 09-14-2002, 08:04 AM

Tags for this Thread