## 120 - Stacks of Flapjacks

**Moderator:** Board moderators

- Krzysztof Duleba
- Guru
**Posts:**584**Joined:**Thu Jun 19, 2003 3:48 am**Location:**Sanok, Poland-
**Contact:**

i get compile error on OJ and on my computer the program compiles fine and runs too but it just keeps on repeating the output...so teh output just keeps on getting printed infinitely and never stops.

here is my code:

[cpp]

#include <iostream>

using namespace std;

short di[31];

int flip(short n, short s)

{

n=s+1-n;

short i,j;

for (i=n,j=1; i>j;j++,i--)

{

swap(di[j], di

*);*

}

return 0;

}

int main()

{

short a=0, c=0, j, count=0;

short i, max=0,n;

short cdi[31];

bool sorted;

while (cin)

{

while (cin.peek()!='\n')

{

a++;

cin>>di[a];

cdi[a]=di[a];

}

for (i=1;i<=a;i++)

{

cout<<di

}

return 0;

}

int main()

{

short a=0, c=0, j, count=0;

short i, max=0,n;

short cdi[31];

bool sorted;

while (cin)

{

while (cin.peek()!='\n')

{

a++;

cin>>di[a];

cdi[a]=di[a];

}

for (i=1;i<=a;i++)

{

cout<<di

*;*

if (i!=a) cout<<" ";

}

cout<<endl;

for (i=1;i<=a;i++)

for (j=i+1;j<=a;j++)

if (cdiif (i!=a) cout<<" ";

}

cout<<endl;

for (i=1;i<=a;i++)

for (j=i+1;j<=a;j++)

if (cdi

*>cdi[j]) swap(cdi**,cdi[j]);*

c=0;

for (i=1;i<=a;i++)

{

if (dic=0;

for (i=1;i<=a;i++)

{

if (di

*!=cdi**){ sorted=false; break;} else sorted = true;*

}

short fb=0,ft;

while (!sorted)

{

ft=cdi[a-fb];

fb++;

for (i=1;i<=a;i++)

{

if (di}

short fb=0,ft;

while (!sorted)

{

ft=cdi[a-fb];

fb++;

for (i=1;i<=a;i++)

{

if (di

*==ft) n=a+1-i;*

}

if (n!=a) flip(n, a);

flip(fb,a);

for (i=1;i<=a;i++)

{

if (di}

if (n!=a) flip(n, a);

flip(fb,a);

for (i=1;i<=a;i++)

{

if (di

*!=cdi**){ sorted=false; break;} else sorted = true;*

}

if (n!=a) cout<<n<<" ";

cout<<fb<<" ";

}

if (sorted) cout<<"0"<<endl;

}

return 0;

}

[/cpp]

thanx a lot. any help would be greatly appreciated.}

if (n!=a) cout<<n<<" ";

cout<<fb<<" ";

}

if (sorted) cout<<"0"<<endl;

}

return 0;

}

[/cpp]

thanx a lot. any help would be greatly appreciated.

[cpp]

while (cin){

while (cin.peek()!='\n'){ // this does not remove '\n' from input

a++;

cin>>di[a];

cdi[a]=di[a];

}

.....................

..... output .....

}

[/cpp]

No wonder your code keeps on repeating the output...

The way I read input in this problem:

[cpp]

char buffer[300];

while(gets(buffer)){

solution/output

}

[/cpp]

[cpp]

while (cin.peek()!='\n')

{

a++;

cin>>di[a];

cdi[a]=di[a];

}

cin.getline(temp, 1, '\n');

[/cpp]

i hoped to take off the '\n' by reading it in. however, when i run this...the first output works fine then from the second time on it just keeps on outputting some weird numbers. and when i try to debug using F10 (msvc++) it just stops at teh while (cin.peek()!='\n') line. plz help. thanx a lot.

Note (n - 1);istream& getline (char* s, streamsize n );

istream& getline (char* s, streamsize n, char delim );

Get a line from stream.

Extracts characters from the stream and stores them into successive locations in the array pointed by s.

Characters are extracted until either (n - 1) characters have been extracted, the delimiter (parameter delim or '\n' if not specified) is found, or if the end of file or any error occurs in the input sequence.

If the delimiter is found it is extracted but not not stored. Use get if you don't want this character to be extracted.

An ending null character is automatically appended after the data stored in s.

I do not recommend you to use this kind of input, try to use sstream:

[cpp]#include <iostream>

#include <sstream>

char buffer[500];

istringstream instream;

.....................................

while(gets(buffer)){

instream.clear();

instream.str(buffer);

for(a = 0; instream >> di[a]; a++);

... solution/output ...

}[/cpp]

I hope this will help...

#include <iostream>

#include <sstream>

using namespace std;

short di[31];

int flip(short n, short s)

{

n=s+1-n;

short i,j;

for (i=n,j=1; i>j;j++,i--)

{

swap(di[j], di

*);*

}

return 0;

}

int main()

{

short a=0, c=0, j, count=0;

char temp[500];

istringstream instream;

short i, max=0,n;

short cdi[31];

bool sorted;

while(gets(temp))

{

instream.clear();

instream.str(temp);

for(a = 1; instream >> di[a]; a++){

cdi[a]=di[a]; }

a--;

for (i=1;i<=a;i++)

{

cout<<di

}

return 0;

}

int main()

{

short a=0, c=0, j, count=0;

char temp[500];

istringstream instream;

short i, max=0,n;

short cdi[31];

bool sorted;

while(gets(temp))

{

instream.clear();

instream.str(temp);

for(a = 1; instream >> di[a]; a++){

cdi[a]=di[a]; }

a--;

for (i=1;i<=a;i++)

{

cout<<di

*;*

if (i!=a) cout<<" ";

}

cout<<endl;

for (i=1;i<=a;i++)

for (j=i+1;j<=a;j++)

if (cdiif (i!=a) cout<<" ";

}

cout<<endl;

for (i=1;i<=a;i++)

for (j=i+1;j<=a;j++)

if (cdi

*>cdi[j]) swap(cdi**,cdi[j]);*

c=0;

for (i=1;i<=a;i++)

{

if (dic=0;

for (i=1;i<=a;i++)

{

if (di

*!=cdi**){ sorted=false; break;} else sorted = true;*

}

short fb=0,ft;

while (!sorted)

{

ft=cdi[a-fb];

fb++;

for (i=1;i<=a;i++)

{

if (di}

short fb=0,ft;

while (!sorted)

{

ft=cdi[a-fb];

fb++;

for (i=1;i<=a;i++)

{

if (di

*==ft) n=a+1-i;*

}

if (n!=a) flip(n, a);

flip(fb,a);

for (i=1;i<=a;i++)

{

if (di}

if (n!=a) flip(n, a);

flip(fb,a);

for (i=1;i<=a;i++)

{

if (di

*!=cdi**){ sorted=false; break;} else sorted = true;*

}

if (n!=a) cout<<n<<" ";

cout<<fb<<" ";

}

if (sorted) cout<<"0"<<endl;

}

return 0;

}

[/cpp]

one thing though...i use the submitting program on this site to submit. i don't know if emailing will make a difference. thanx a lot.}

if (n!=a) cout<<n<<" ";

cout<<fb<<" ";

}

if (sorted) cout<<"0"<<endl;

}

return 0;

}

[/cpp]

one thing though...i use the submitting program on this site to submit. i don't know if emailing will make a difference. thanx a lot.

gets needs <stdio.h>02235073_24.c: In function `int flip (short int, short int)':

02235073_24.c:14: `swap' undeclared (first use this function)

02235073_24.c:14: (Each undeclared identifier is reported only once for

each function it appears in.)

02235073_24.c: In function `int main ()':

02235073_24.c:27: `gets' undeclared (first use this function)

swap needs <algorithm>

After these corrections you get "Invalid memory reference", which (most likely) mean your code is trying to work with elements out of you arrays limits (negative or greater than 30 indices).

I hope you'll find your error.