1 / 3
Jun 2007

The code works perfectly fine in my gcc 4.0.0-8...any reasons why online judge is giving Wrong Answer?

#include <iostream>
#include <iterator>
#include <limits>
#include <vector>
int main()
{
    int number = 0;
std::vector<int> vec;
std::cout << "Enter integers[0 - 99] till you find the\nAnswer to Life, the Universe, and Everything...\n";
while(true)
{
    std::cin >> number;
    std::cin.ignore(std::numeric_limits<int>::max(), '\n');

    if ( !std::cin or std::cin.gcount() != 1 or number < 0 || number > 99)
    {
        std::cout<<"Invalid Input...Input Processing Finished\nOutput:\n";
        break;
    }
    else
    {
        vec.push_back(number);
    }
}

for( std::vector<int>::const_iterator cIter = vec.begin() ; cIter not_eq vec.end() ; ++cIter)
{
    if(42 == *cIter)
    {
        break;
    }
    else
    {
        std::cout << *cIter << std::endl;
    }

}
return 0;
}
  • created

    Jun '07
  • last reply

    Jun '07
  • 2

    replies

  • 141

    views

  • 2

    users

You should output what is specified in the output specification and nothing more. This "Enter..." text is probably the reason for WA.
[quote="sps"]

if ( !std::cin or std::cin.gcount() != 1 or number < 0 || number > 99)
        {
            std::cout<<"Invalid Input...Input Processing Finished\nOutput:\n";
            break;
        }

[/quote]
The input is guaranteed to be valid, so you don't need this at all.
(It can't hurt, though.)

Noix

Suggested Topics

Topic Category Replies Views Activity
C and C++ 0 17 9d

Want to read more? Browse other topics in C and C++ or view latest topics.