Home > OS >  Reading input in loop until sentinel value is reached in C
Reading input in loop until sentinel value is reached in C

Time:04-26

I'm relatively new to C and I'm trying to write some code that asks a user to enter numbers and use -999 as a sentinel value to end the input loop. The numbers they enter must populate an array to be used in a binary tree later. The issue I'm having is the loop doesn't terminate if -999 is entered, and I also don't know how to read those values into the array without specifying how large the array is. The program must dynamically size the array based on how many inputs were given. I have attached my code. Any help is greatly appreciated!

#include <iostream>
#include <string.h>
//#include "binarySearchTree.h"
//#include "binaryTree.h"

using namespace std;
int i;
int n;

struct Node {
    int data;
    struct Node* left;
    struct Node* right;
}

Node(int val)
{
    data = val;
    left = NULL;
    right = NULL;
};

int main() {

    //struct Node* root = new Node(1);

    cout << "Enter values up to -999: \n";
    int numbers[] = {};

    for(int i =0; i!=999; i  ){
        cin >> numbers[i];
    }

    cout << "\nYour numbers are: ";

    for(int j=0; j<=sizeof(numbers) ; j  ){
        cout << numbers[j] << " ";
        }
    return 0;
    }

CodePudding user response:

Here is a program that does what you want.

#include <iostream>
#include <vector>

// never, ever use using namespace std;

int main()
{
   std::vector<int> input; // this is the way to spell "an array that can grow as needed"
                           // int numbers[] = {}; is not one.

   int number;
   while (std::cin >> number // check whether the input is successful, i.e. a number 
                             // is entered. entering a non-number or an
                             // end-of-file indicator is a failure
          && number != -999) // and whether that number is not -999
   {
      input.push_back(number); // add it to the end of our growing-as-needed array
   }

   for (auto i : input) // that's how you enumerate entries in the array
   {
      std::cout << i << " ";
   }
   std::cout << "\n";   // print a newline when you are done

}

Live demo

CodePudding user response:

Beside all other problems, the main problem is here:

for(int i =0; i!=999; i  ){
    cin >> numbers[i];
}

You are reading from the standard input into the i-th element of the array numbers. You are comparing i with 999, which basically makes no sense. Why comparing i? And why comparing it with 999, instead of -999?

Let's try to fix it. Start with an empty infinite loop:

while (true) {
    // Read
    // Check
    // Use
}

Read an integer:

while (true) {
    // Read
    int val;
    cin >> val;
    // Check
    // Use
}

Now let's check if we managed to read something and if not let's exit from the loop.

while (true) {
    // Read
    int val;
    cin >> val;
    // Check
    if (cin.fail()) {
        break;
    }
    // Use
}

We need to exit from the loop also if we read a -999:

while (true) {
    // Read
    int val;
    cin >> val;
    // Check
    if (cin.fail()) {
        break;
    }
    if (val == -999) {
        break;
    }
    // Use
}

Now you want to put it in the i-th position of numbers, so:

int i = 0;
while (true) {
    // Read
    int val;
    cin >> val;
    // Check
    if (cin.fail()) {
        break;
    }
    if (val == -999) {
        break;
    }
    // Use
    numbers[i] = val;
      i;
}

Ok, now we have a working loop (hopefully). What other problems you have in your code?

  1. int numbers[] = {};
  2. j<=sizeof(numbers)

You cannot define arrays without a compile time size in C . Use std::vector<>. Then, the sizeof operator doesn't do what you think it does. Save it for (much?) later. Use std::vector::size(). But for starters, you can assume that 1000 numbers will be enough for everyone (Bill Gates docet), and keep the count in variable i:

#include <iostream>
using namespace std;

int main() 
{
    cout << "Enter values. Use -999 to stop entering values.\n";
    int numbers[1000]; // We accept 1000 numbers at most            
  • Related