ASCII character converter

Mar 3, 2014 at 10:58am
I made an ASCII character converter. You can input an int and it gives you the character with the same value or opposite.

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
#include<limits>
#include<iostream>

using namespace std;

int main()
{
    char ans;
    char convertor;
    char character;
    int integer;
    convertor_get:
    cout<<"Please choose convertor int to char'a', char to int'b'"<<endl;
    cin>>convertor;
    if(convertor=='a' || convertor=='A')
    {
        cout<<"Enter your int"<<endl;
        cin>>integer;
        character=integer;
        cout<<"The char is:"<<character<<endl;
        cout<<"Would you like to continue? 'y' or 'n'"<<endl;
        ans_get:
        
        cin>>ans;
        if(ans=='y' || ans=='Y')
        {
           
            goto convertor_get;

        }
        else if (ans=='n' || ans=='N')
        {
            return 0;
        }
        else
        {
            cout<<"Invalid input, please write again"<<endl;
            goto ans_get;
        }
    }
    else if(convertor=='b' || convertor=='B')
     {
         cout<<"Enter your char"<<endl;
         cin>>character;
         integer=character;
         cout<<"The int is:"<<integer<<endl;
         cout<<"Would you like to continue? 'y' or 'n'"<<endl;
         cin>>ans;
         if(ans=='y' || ans=='Y')
        {
            goto convertor_get;
        }
        else if (ans=='n' || ans=='N')
        {
            return 0;
        }
        else
        {
            cout<<"Invalid input, please write again"<<endl;
            goto ans_get;
        }
     }
     else
     {
         cout<<"Invalid input, please write again:"<<endl;
         goto convertor_get;
     }
}



Please choose convertor int to char'a', char to int'b'
a
Enter your int:
63
The car is: ?
Mar 3, 2014 at 11:35am
do you have any questions?

By the way: line 60 is wrong
Mar 3, 2014 at 12:55pm
Whoops i forgotten to write the question. The question is when i choose the int to character converter and when it asks me to write an integer i put a character example(Console:Enter an integer User: a) it goes to an infinite loop. Why?
Also line 60 is correct!
Last edited on Mar 3, 2014 at 12:56pm
Mar 4, 2014 at 6:59am
it goes to an infinite loop. Why?
The reason is that after entering incorrect characters cin is in error state. You can determine this with bad(). Read this:

http://www.cplusplus.com/reference/ios/ios/bad/

call clear to get out of the error state:

http://www.cplusplus.com/reference/ios/ios/clear/
Topic archived. No new replies allowed.