Reputation: 11441
I have the following code from a book about Algorithms by Robert Sedwick.
How do i break a for loop in the following statement if (!(cin >> a[N])) break;
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
int compare(const void *i, const void *j)
{ return strcmp(*(char **)i, *(char **)j); }
int main()
{ const int Nmax = 1000;
const int Mmax = 10000;
char* a[Nmax]; int N;
char buf[Mmax]; int M = 0;
for (N = 0; N < Nmax; N++)
{
a[N] = &buf[M];
if (!(cin >> a[N])) break;
M += strlen(a[N])+1;
}
// std::cout << "Number of strings entered are " << N << std::endl;
qsort(a, N, sizeof(char*), compare);
for (int i = 0; i < N; i++)
cout << a[i] << endl;
}
Upvotes: 0
Views: 721
Reputation: 92371
If you type the input at the console, you have to enter the end-of-file code.
For Linux this would be Ctrl+D
, and for Windows Ctrl+Z
.
Upvotes: 8