Terminate if no input. Help
I need to find a way to terminate this code if there is no input.. Can anyone help?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
#include <iostream>
#include <string>
#include <cmath>
#include <cstring>
using namespace std;
int main (void)
{
int counter=0;
for (int m=0; m<100; m++)
{
char x [100];
cin >> x;
double a= strlen(x)-1;
int e= a-1;
int num1=0, num2=0,num3=0, result, temp=0, temp2=0, temp3=0;
int b,c,d;
bool plus=false;
bool minus=false;
for (int n=0; n<a; n++ )
{
if (x[n] == '+')
{
b=n;
plus=true;
}
if (x[n] == '-')
{
b=n;
minus=true;
}
if (x[n] == '=')
{
c=n;
d= c-1;
}
if (x[n]=='?')
{
continue;
}
}
for (int p=0; p<b; p++)
{
float h= b-p-1;
float i =pow(10, h);
temp= (x[p]-'0') * i ;
num1+= temp+num1;
}
for (int q=b+1; q<c; q++)
{
float f= c-q-1;
float j= pow(10, f);
temp2= (x[q]-'0') * j;
num2+= temp2+num2;
}
for (int r=c+1; r<a+1; r++)
{
float g= a-r;
float k= pow(10, g);
temp3= (x[r]-'0') *k;
num3+= temp3+ num3;
}
if (plus== true && num1 + num2 == num3)
{
counter= counter+1;
}
if (minus== true && num1-num2==num3)
{
counter= counter+1;
}
else
{
counter = counter;
}
}
cout << "\n" << counter << "\n";
return 0;
}
|
if i were not mistaken,, NO.. because you cannot leave cin if you only press enter on the console ?
so how do you fix it? Is it possible to use the "getline" function?
yes, in fact i've tried a code using getline, take a look:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string buffer;
getline ( cin, buffer );
if ( buffer[0] == '\0' )
cout <<"\n\nWorked";
else
cout <<"\n\nDidn't worked";
return 0;
}
|
Last edited on
Topic archived. No new replies allowed.