A lot of the times when I code lets say I have only one cin>> I run my code it compiles but I can do infinite inputs even though in my code I have one input. What is the reason for this?
I checked for infinity loops found none for the specific test case I was working with.
Here's the code if you want to see.
#include <iostream>
using namespace std;
int main()
{
long long a,b,c;
cin>>a>>b>>c;
long long mx,mn;
if(b>=a)
{
b=mx;
a=mn;
}
if(a>b)
{
a=mx;
b=mn;
}
int x;
int r=c;
for(int i=mn;i<=mx;i )
{
x=i;
if(x-mn==mx-x)
{
for(int j=0;j<x;j )
{
c =r*r;
}
cout<<c (mx-mn);
return 0;
}
}
}
CodePudding user response:
The problem lies down here, you are re-assigning values of a
and b
with some unassigned variables.
if(b>=a){
b=mx;
a=mn;
}
if(a>b){
a=mx;
b=mn;
}
Rather you may want to initialize max
and mn
with newly assigned values a
, b
if(b>=a){
mx=b;
mn=a;
}
if(a>b){
mx=a;
mn=b;
}
CodePudding user response:
I am not sure what you mean but infinite input
here, so I am guessing you have cin >> a >> b>> c
and you passed 1 2 3 4
as input then your input actually stored in input file buffer stdin
and cin take needed part from it one by one so cin end taking first three integers and leaving the rest making it look like infinite input.
adding mistake in your code as also mentioned by @Papai from BEKOAIL
if(b>=a){
b=mx;
a=mn;
}
if(a>b){
a=mx;
b=mn;
}
by doing this you are changing value of a
and b
with garbage
value,
where value of mx
and mn
remains uninitialized with garbage
value.
correct way of doing it:
if(b>=a){
mx=b;
mn=a;
}
if(a>b){
mx=a;
mn=b;
}