12 / 103
Nov 2008
3 months later

[edit] Previous post deleted.

Having re-read the question, I have a different take on what it means.

It appears that what is being asked is, from the input of: 1, 2, 88, 42, 99 - output must be each digit up until when 42 is typed, then no output from anything entered.

I now have a program that does that:

#include <iostream>
int main()
{
	using std::cin;
	using std::cout;
	using std::endl;
    int inputDigit;
for (int i = 0; i <= 10; i++)
{
	cin >> inputDigit;
	if (inputDigit != 42)
	{
		cout << endl << inputDigit;
	}
	else
	{
		return 0;
	}
}
return 0;
}

It's messy, but it works. However, on submission I got "wrong answer". Blah!

-Oscarian

[edit] edited to save multiple posts.

Correct, you should stop when you hit 42.

You also seem to be stopping after the 11th number even if it isn't 42 wink

Doh! Sometimes it's the obvious things that we miss.

I re-did the program using a function that calls itself, submitted it, and it was accepted.

-Oscarian

Note that a simpler method of fixing it would just be to use

for (int i=0; ; i++)

or even simpler

while (true)

rather than getting into anything complicated like a recursive function (which would cause stack errors if the input was too big).

8 days later

Guys, even if any of those hideous solutions work, which is a lot to say for the very first and easiest problem ever to exist, they will land you a time of 0.01.
The I/Ostream library is SLOW as hell - depending on what you read/write to the stream, they are at LEAST 10 times slower than the standart reading/writing functions in stdio. Use stdio.h.

About the problem - try the following:

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

There are still better approaches to the problem.

20 days later

[quote="noix"]

#include <iostream>
int main(void) {
	char c, d=10;
	while(std::cin.get(c) && (c!='2' || d!='4') && std::cout.put(d)) 
		d=c;
}

[/quote]
This TEST solution is wrong, isn'it?
In case of input 8, 99, 4, 2, 10, 42, 3 it will output 8, 99.
I see that it's enough that it works for the test case, but as example answer it is not the best...

I agree it's not the best as an demonstrative example, but it does solve the problem exactly as specified. If you try the input you mentioned, you'll see it outputs the correct answer, not what you said it would.

1 month later

I did

#include <stdio.h>
int main(void){
   int Input = 0;
   scanf("%d",&Input);
   while(Input != 42){
      printf("%d\n" , Input);
      scanf("%d",&Input);
      }
   return 0;
   }

simple straight forward and easy to read. Now fi I could just get my PRIME1 code to run in the alloted time...

6 months later

can someone say what is the problem here.
JUDGE shows wrong answer

#include<iostream.h>
int main()
{
 int a;
 cin>>a;
 while(a!=42)
 {
             cout<<a;
             cin>>a;
 }
 return 0;
}
9 months later

#include<iostream>
using namespace std;
main() {
  int number=0;
  while(number!=42) {
    cin >> number;
    cout << number<< "\n";
  }
}

Please let me know what is wrong with my code.

2 months later

[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?