21 / 103
Oct 2010

[bbone=CPP,9]#include
using namespace std;

int main()
{
int x;
while(scanf("%d", &x) && x != 42)
printf("%d\n", x);
return 0;
}

[/bbone]

20 days later

Ok i've manged to find a solution but when i submit it i got wrong answer even though it's doing what it's supposed to do

#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
	int x;
	do 
	{
		scanf ("%d", &x);
		printf (" %d \n", x);
	}
	while (x != 42);
return 0;
}

so what i'm doing wrong?

do-while is an exit control loop. it will first print the value and after that checks whether the entered value is terminating one i.e 42 or not. check you code for test cases.

I'm a bit puzzled. Because I fail to see where is the mistake in my code. Could anyone give me a hint?

#include <iostream>
using namespace std;
int main(void) {
 int a=0;
  while (a != 42){
  cin >> a;
  cout << a << endl;
  }
return 0;   
}
28 days later

I'm still not sure what the questions actually asking. What's the number of inputs that our program has to take? Is it just five indicated by the example?

The number of inputs is undefined. Read until match.

3 months later

#include <iostream>
using namespace std;
int main()
{
int size = 0;
int n[size];
do
	{
	cin>>n[size];
	size++;
	}while(n[size-1] != 42);
cin>>n[size];
for(int i = 0; i < (size-1); i++)
	{
		cout<<n[i]<<endl;
	}
return 0;
}

Hello everyone,
When i compile it it outputs the correct answer, however judge points me out to a runtime error (SIGSEGV).
What is wrong with the code then?
Thanks in advance, and glad to join this community. smile

When you declared n size was equal to zero, so n has zero elements. c and c++ don't check array bounds, they only throw errors when the program tries to access illegal memory (memory outside the defined space for the program).

For this problem the number of inputs are undefined. There could be ten, there could be a million. The amount doesn't matter.

Input a number. If it's 42 stop. Otherwise, output the number. Repeat.

4 months later

I'm sorry this is actually my first program in C++. I've done this program in pascal before, and i just 'translated' in C++. The sample input and output works, but time limit exceeds. Am i doing something wrong? It works fine on Ideone.

#include <iostream>
using namespace std;
int main ()
{
  int a, b;
  bool c;
  c=false;
  while (true)
  {
        cin >> b;
        if (b == 42)
           c = true;
        else if (c == false)
            cout << b << "\n";      
  }
  return 0;
}

while(true) will never terminate. This code will not have run correctly on ideone.

1 month later

I write this code and got pass in test.
The common mistake every one do in the beginnign is they actually put some helping text on cout to tell others what to do which is not needed.

#include <iostream>
using namespace std;
int main(){
    int x=0;
    cin >> x;
      while(x != 42){
              if (x <=99){
                 cout << x <<endl;
                 cin >> x;
                 }else {
                       cin >> x;
                 }
      }
}
21 days later

my code is given below...my problem is just about the termination of input when i enter blank or simply press the return key...i appreciate any help..thanks alot!!

include

include

include

using namespace std;

main()
{
int input;
vectorv;
while(scanf ( "%d", &input ) == 1 )
{
//cin>>input;
if(!input)
break;
else
v.push_back(input);

}
vector<int>::iterator i;
for(i=v.begin();i<v.end();i++)
{
    if(*i!=42)
    cout<<*i<<endl;
    else break;
}

}

13 days later
1 month later

i'm having the same "wrong answer". I don't know what's wrong! i get the right output! D:

#include <iostream>
using namespace std;
int main() {
int n, n2, n3, n4, n5;
cin>>n;
cin>>n2;
cin>>n3;
cin>>n4;
	switch(n4==42)
	{
		break;
		case 1:
			cin>>n5;
			cout<<n<<endl;
			cout<<n2<<endl;
			cout<<n3<<endl;
		break;
}
}

The "sample input" is not the only test case that your code will be tested on.

Some other test cases:

42

1
2
3
4
4
5
5
6
7
8
9
42
12
12
13
9 months later
24 days later

include

include

void main()
{ int n;
while(1)
{ cin>>n;
if(n==42)
break;
cout<<"\n"< }
getch();
}

this is the code which i wrote but it shows a compilation error.can someone please tell me what is wrong with this code?

include

int main()
{ int n;
while(1)
{ cin>>n;
if(n==42)
break;
cout<<"\n"<}
return;
}

this is the edited code but this is also not working.it shows a compilation error.Could someone please give a sample code in c++ which will be accepted by spoj because when i compile it in my compiler then it shows no error.