1 / 103
Jun 2004

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

    Jun '04
  • last reply

    Oct '22
  • 102

    replies

  • 10.5k

    views

  • 57

    users

  • 6

    likes

  • 8

    links

Frequent Posters

There are 102 replies with an estimated read time of 9 minutes.

3 years later

I did it not so simple, more interesting, and checkman said I was wrong. It is strange. here is the code

include

using namespace std;

int main(int argc, char *argv[])
{
int num[10]; // massive of integers
int count=10; // number of ints to output
cout << "input not more than 10 \n";
cout << "numbers value '42' = stop\n";
for (int n=0; n<10; n++)
{
cout << endl << n+1 << ". ";
cin >> num[n];
if (num[n]==42) {count=n; n=12;
cout << "\nStop command detected! \n";
cout << "Numbers to output = " << count << endl; }
}

cout << endl << endl << "Your output\n";
for (int a=0; a<count; a++)
{
    cout << num[a] << endl;
}

return 0;

}

everything is clear for me, why he said it is wrong ?

Could not you answer to the mail where is the wrong.
Of course if you will have free time.
Thanks.

For the sample input:

1
2
88
42
99

You have to output this:

1
2
88

You are outputting this:

input not more than 10 
numbers value '42' = stop
1. 
2. 
3. 
4. 
Stop command detected! 
Numbers to output = 3
Your output
1
2
88

These are obviously not the same. You have to output exactly what is required, not all that extra text.

Thanks. I did it whith loop, he accept. Sorry for unexperienced question.

8 months later

I'm having a similar problem when submitting a solution for TEST,
I haven't included extra messages on the output , yet i got wrong answer
Here's my source code, I would appreciate any help
smile

#include <iostream>
using namespace std;
int main()
{
	int number;
    while(true)
{
	cin >> number;
	if(number == 42)
		break;
	cout << number;
}
}
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.