How to end a for loop with entry of particular value??

I have a for loop in which values are entered for two different arrays, using two cout statements.

I need a way to end the loop and continue with the rest of the function after the value "-1" is entered into the first array. -1 is a signal that all data has been read.

I tried to use if and then break, but that didn't work. What other ways are there to end the loop? thanks so much for any help!

I am very new to programming, as may be obvious.

Here is part of what I have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  const int size = 100;
	int id[size], I;
	double balance[size];

	for (I=0; I<size; I++)
	{
			cout << "Enter the customer's ID number: ";
			cin >> id[I];
			
			while (id[I]<1000||id[I]>9999)
			{	cout << "You have entered an invalid ID.  Please  reenter: ";
				cin >> id[I];
			}

			cout << "Enter the customer's balance: ";
				cin >> balance [I];

			while (balance[I]>10000)
			{	cout << "You have entered an incorrect balance.  Please reenter: ";
				cin >> balance[I];
			}
	}


I would like to insert some sort of statement immediately after the first cout statement that would end the loop when id[I] == -1. I don't want it to go on to the while statements or the next cout one.
Last edited on
Post what you have already written so that we can see where you are going wrong.

Alan
you could either use if/else:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	for (I=0; I<size; I++)
	{
			cout << "Enter the customer's ID number: ";
			cin >> id[I];
			if (cin != -1) {
			    while (id[I]<1000||id[I]>9999)
			    {	cout << "You have entered an invalid ID.  Please  reenter: ";
			    	cin >> id[I];
			    }

			    cout << "Enter the customer's balance: ";
			    	cin >> balance [I];

			    while (balance[I]>10000)
			    {	cout << "You have entered an incorrect balance.  Please reenter: ";
			    	cin >> balance[I];
			    }
                        }
	}


or as you said use break:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	for (I=0; I<size; I++)
	{
			cout << "Enter the customer's ID number: ";
			cin >> id[I];
			if (cin == -1) break;

			while (id[I]<1000||id[I]>9999)
			{	cout << "You have entered an invalid ID.  Please  reenter: ";
				cin >> id[I];
			}

			cout << "Enter the customer's balance: ";
				cin >> balance [I];

			while (balance[I]>10000)
			{	cout << "You have entered an incorrect balance.  Please reenter: ";
				cin >> balance[I];
			}
	}


note that the break method immediately stopps the loop while the upper example just proceeds with the next item. This is identically to use continue;


hope this helps


ironic

Last edited on
ironic, in your first part of the program,

you forgot to write "else "

it should be like this:

1
2
3
4
5
6
7
8
if (cin != -1) 
{
    ...
}
else
{
    break;
}

Last edited on
thanks so much for your help, ironic and chenwei86a. I got it to work!
I would avoid special entries like -1.
Also, (cin != -1) makes no sense...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

// Handy little function to convert string to number
template <typename T>
bool string_to_number( const string& s, T& result )
  {
  // Try to transform that line into the result
  stringstream ss( s );
  ss >> result;

  // Indicate if it succeeded or failed
  return bool( ss );
  }

int main()
  {
  const int max_num_items = 100;

  int    id     [ max_num_items ];
  double balance[ max_num_items ];
  int    num_items;
  string s;

  cout << "Please enter the following requested information.\n"
          "Press ENTER without typing anything else to finish.\n\n";

  for (num_items = 0; num_items < max_num_items; num_items++)
    {
    cout << "Enter the customer's ID number: ";
    while (true)
      {
      // Users expect to have to press ENTER after each input, so read the whole line
      getline( ins, s );
      if (s.empty()) break;
      if (string_to_number( s, id[num_items] ) && (id[num_items] >= 1000) && (id[num_items] <= 9999)) break;
      cout << "You have entered an invalid ID.  Please try again: ";
      }
    if (s.empty()) break;

    cout << "Enter the customer's balance: ";
    while (true)
      {
      getline( ins, s );
      if (s.empty()) break;
      if (string_to_number( s, balance[num_items] ) && (balance[num_items] <= 9999)) break;
      cout << "You have entered an incorrect balance.  Please try again: ";
      }
    if (s.empty()) break;
    }
  cout << "Thank you.\n"
          "You have entered " << num_items << " items.\n\n"

          "Now to do some amazing math with them.\n\n";

  ...

  return 0;
  }


Hope this makes sense.
Topic archived. No new replies allowed.