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?
#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.
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.
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;
}
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;
}
}
}
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;
}
}
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;
}
}
Here's a good description / explanation on how to solve this problem Life universe and everything problem in C++, C, and Java:
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.