identify floating or alphabets

hello
i have this homework where i am implementing a code which does error checking

so basically i have 3 variables and i assigned them as integer. so my error check is asking that if i type a value for instance 1.2 it should output "X"...


any suggestions pls helpppppp!!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int a;
cin>>a;
if(!cin) // well this
{
cout<<"X"<<endl; 
}


system("PAUSE");
return 0;
}

if i understood you good
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
float a;
cin>>a;
int checker;
checker=a;
// a=1.2    
if(!cin||((checker+1)!=(a+1)&&(checker-1)!=(a+1))||a==0) // well this
{
cout<<"X"<<endl; 
}




system("PAUSE");
return 0;
}

here friend hope that this helps
it basicly conversion like this loses the decimals
checker=a// so if a is 2.5 the checker will be 2
but if we add a=2 checker will be 2 also so. . .
Last edited on
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
#include "stdafx.h"
#include<iostream>
#include<cstdlib>
#include <cmath>
#include<stack>
#include<conio.h>


using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int remainder, quotient, base,i; 
int checker;
float quotient2;


cout << "Please enter a quotient: ";
cin >> quotient2;
checker=quotient2;
if (!cin||quotient2 == 0||((checker+1)!=quotient2+1)||((checker-1)!=quotient2-1) )
{
cout << "X" << endl;
return 0;
}
quotient=quotient2;
cout << "Please enter a Base: ";
cin >> base;


while (quotient > 0)	
{
remainder = quotient % base;
quotient /= base;
cout<< remainder << endl;
}
cout <<endl;

system("PAUSE");

return 0;
}


HERE :P
Last edited on
it worked but give a failure error
""Run-Time Check Failure #3 - The variable 'quotient' is being used without being initialized.""
Last edited on
are you sure ?:D
i tested this code it worked good?
copy paste the above code?
did you copy this ???
***
quotient=quotient2;
***
Last edited on
yeh works perfectly i just did add quotient2 in there. but thanks alot friend..
so by now you should see that i am converting decimals to binary. so is there any way that i could output my value in reverse..
for instance denominator 12, divisor 2, i get 0011 but i want the output to be reverse of that.. so like 1100.. :

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
#include "stdafx.h"
#include<iostream>
#include<cstdlib>
#include <cmath>
#include<stack>
#include<conio.h>


using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int remainder, quotient, base,i; 
int checker;
float quotient2;


cout << "Please enter a quotient: ";
cin >> quotient2;
checker=quotient2;
if (!cin||quotient2 == 0||((checker+1)!=quotient2+1)||((checker-1)!=quotient2-1) )
{
cout << "X" << endl;
return 0;
}
quotient=quotient2;
cout << "Please enter a Base: ";
cin >> base;

int a[100000];
int rev[100000];
int zero=0;
while (quotient > 0)	
{
	
remainder = quotient % base;
quotient /= base;
a[zero]=remainder;
cout<< remainder << endl;

zero++;
}
cout<<":::::::::::::::\n";
int zero2=0;
while(zero>0)
{
rev[zero2]=a[zero-1];
cout<<rev[zero2]<<endl;
zero2++;
zero--;
}
cout <<endl;

system("PAUSE");

return 0;
}



something like this :))
WOOOW ! my friend you are a genious ... you are really goood

well i wana have just one output. the reversed answer
xD
remove the cout in 39 line
and in 43
have fun :)
Last edited on
i swear you are very good.
so one more thing sorry.

like this is actually like an error checking homework. so at the end i will have to alter it and get all the "cin" functions removed. so can you give me alternative for "" !cin"" function pls
u ll use scanf? explain me better
i need to go ,
i will answer you later :)
give me an example before i get back,:P
ok.. wel its like an error checking homework..
where you do error check..
example.
quotient 12 base 2 output reversed 1100. true
quotient 0 base (value) invalid because its not right.true (output invalid)

again having quotient as an integer than writing a value of 1.2 which is a float n is wrong so output "X".

but you are not allow to ask user's input..
i write all my programs using argc().. so my teacher just basically insert my code into his unknown program and it goes through the error check..
get it>??? :D
its a bit hard to explain..
my friend can you pls explain the reverse thingi that you did with the output pls..
int a[100000];
int rev[100000];
int zero=0;
while (quotient > 0)
{

remainder = quotient % base;
quotient /= base;
a[zero]=remainder;
cout<< remainder << endl;

zero++;
}
cout<<":::::::::::::::\n";
int zero2=0;
while(zero>0)
{
rev[zero2]=a[zero-1];
cout<<rev[zero2]<<endl;
zero2++;
zero--;
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
int a[100000]; // i make a array of 100000 max elements
int rev[100000]; // same
int zero=0; // its the pointer of the array "a"
while (quotient > 0)	 // this is yours
{

remainder = quotient % base;
quotient /= base;
a[zero]=remainder; // so this puts in to the array "a"  0 0 1 1
cout<< remainder << endl;

zero++; // this is for what place the remainder to be example 
// 0 (0)
// 0 (1)
// 1  (2)
// 1  (3)
}
cout<<":::::::::::::::\n"; // separates
int zero2=0; // second pointer to 0
while(zero>0) // so in the first loop for 0011 the zero was adding 1+ every 
// time , we will use it to transfer it to the rev (reverse) array
{
rev[zero2]=a[zero-1]; // reverse array
// the first number will be the last in the a array
// the second number will be the last - 1 in the a array
// and so on
cout<<rev[zero2]<<endl;
zero2++; // zero2 ++ so adding the address first,second,third . . .
zero--; // zero -- so the last -1 , last -2 and so on




but if u declare this as int u cant type in characters
so u need a condition that doesnt allow float numbers and zero ?
if so just remove the '!cin' and make quotient2 to be quotient
and quotient to be quotient2.
and it shud be good for running :D
sry for my late answer
no worries my friend i fall aslee hehe :P
in my if statement. i want to have a restriction my friend.
like if its 2e2 it should continue to run the program which it is doing so, but if its for insistance .e2 i should output X
for characters?
make me an in example

in

2
2

out
0
1

in
1.2

out
X

something like that?

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
#include "stdafx.h"
#include<iostream>
#include<cstdlib>
#include <cmath>
#include<stack>
#include<conio.h>


using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int remainder, quotient2, base,i; 
int checker;
float quotient;


cout << "Please enter a quotient: ";
// ur value
checker=quotient;
if (quotient == 0||((checker+1)!=quotient+1)||((checker-1)!=quotient-1) )
{
cout << "X" << endl;
return 0;
}
quotient2=quotient;
cout << "Please enter a Base: ";
cin >> base;

int a[100000];
int rev[100000];
int zero=0;
while (quotient2 > 0)	
{
	
remainder = quotient2 % base;
quotient2 /= base;
a[zero]=remainder;
cout<< remainder << endl;

zero++;
}
cout<<":::::::::::::::\n";
int zero2=0;
while(zero>0)
{
rev[zero2]=a[zero-1];
cout<<rev[zero2]<<endl;
zero2++;
zero--;
}
cout <<endl;

system("PAUSE");

return 0;
}


Last edited on
ok don't worry about that i got it working..
but have another problem..
what does if(argc==3) mean?

what does if(argv[2][i]) ?

Topic archived. No new replies allowed.