is this a good programme?

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int x;
char A,B,C,D;
cout<<"please input an integer between 0 and 35"<<endl;
for(x=0;x<=9;)
{
cin>>x;
cout<<x;
if(x>=10)
{
cout<<static_cast<char>('A'+ (x-10));

}

}
getch();
return 0;


}
I guess you are mixing old C++ code with new one since conio.h is not used in C++ anymore and hence no getch() in program.

http://www.cplusplus.com/reference/clibrary/
Last edited on
Is it good? Well, it's had to say, is it isn't clear what it is supposed to do.
There are unused variables char A,B,C,D;
The for loop looks a bit odd. I think in this case a while or do-while would be more legible.
What happens if the user inputs something outside the requested range?
I won't say this is "better" as I'm not sure how the program was intended to work.
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
#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int x = -1;
    
    do
    {
        cout << "please input an integer between 0 and 35" << endl;    
        cin >> x;
        if (!cin)
        {
            cin.clear();
            cin.ignore(1000, '\n');
            x = -1;
        }
    } while (x < 0 || x > 35);
    
    cout << x;
    cout << static_cast<char>('A'+ (x-10));      
    
    getch();
    return 0;
} 
Topic archived. No new replies allowed.