I'm having a similar problem and submitted following solution. When I ran it on http://ideone.com/22 it worked fine but still SPOJ saying it as wrong answer
Please correct me if anything wrong in my solution.
compiled with C++ 5.1
int main() {
int i;
std::cout<<"Enter your input number (42 is for exit)"<<endl;
do
{
std::cin>>i;
if( std::cin.fail() )
{
std::cout<<"Wrong input, numeric numbers only accepts"<<endl;
}
else
{
std::cout<<i<<endl;
}
}while(42 != i);
std::cout<<"Successfully exiting"<<endl;
return 0;
}
include
using namespace std;
int main()
{
int a;
while(1)
{
cin >>a;
if(a==42)
break;
else
cout <}
}
Hi, you are using else. You're supose to stop the outstream after reading numer 42.
Second is that can see cout<, that is probably why.
Good luck.
P.S. There's a good answer with the code above.
Simple solution in C++
include
include
using namespace std;
int main(){
int a[1000];
for(int i=0;i<1000;i++){
cin>>a[i];
}
for(int i=0;i<1000;i++){
if(a[i]!=42)
cout< else
break;
}
return 0;
}
My solution doesn't pass your tests. It compiles on my local system and seems to satisfy the requirements. I am using mingw492_32 compiler on my computer.
Question: How do I wrap this in a code block where it picks up my includes and angle brackets accurately?
(pound symbol)include < iostream>
(pound symbol)include < list>
using namespace std;
int main(void) {
int val;
list< int> array;
cout << "Input:" << endl;
while (true) {
cin >> val;
if (val > 99 || val < -99)
continue;
if (val == 42)
break;
array.push_back(val);
}
cout << "\n\nOutput:" << endl;
for (list<int>::iterator iter = array.begin(); iter != array.end(); iter++)
cout << *iter << endl;
return 0;
}
Please keep in mind that this is my first day working with c++ (or any language). Having only a limited knowledge of using the iostream header, this is what I came up with:
#include <iostream>
using namespace std;
int main()
{
int a;
cin >> a;
while (a != 42)
{
cout << a << endl;
cin >> a;
}
return 0;
}
#include<iostream>
#include <list>
#include <iterator>
using namespace std;
void outputNumber(list <int> g){
list <int> :: iterator it;
for(it = g.begin(); it != g.end(); ++it) {
if(*it==42){
break;
}else{
cout<<*it<<endl;
}
}
}
int main(){
int myints[] = {1,2,88,42,99};
std::list<int> testInputs (myints, myints + sizeof(myints) / sizeof(int) );
std::cout << "The contents of the tested inputs are: ";
for (std::list<int>::iterator it = testInputs.begin(); it != testInputs.end(); it++)
std::cout << *it << ' ';
std::cout << '\n';
outputNumber(testInputs);
return 0;
}
my codes have output the correct answers, but the compiler does not accept it…