Hello,
Before I begin, please be aware that I started using C++ as early as 3 days ago, therefore it is required that you remain patient.
I made a program that would convert a value that I enter into different bases. If it was not a number that was typed in, it would crash, which made me want to tell it to distinguish between numbers and letters. For that purpose, I used ASCII.
I ended up with this thing below. Whenever I enter a value, for example "qwerty", it prints "ERROR. I WANTED A NUMBER." as many times as there are letters. Similarly with numbers and "256" would look like:
Enter x: 256
Base 10: 2
Hexadecimal: 2
Base 8: 2
Enter x: Base 10: 5
Hexadecimal: 5
Base 8: 5
Enter x: Base 10: 6
Hexadecimal: 6
Base 8: 6
Enter x:
To fix this, I've tried various methods, that explains why there are so many files included. Tried to use string, stringstream etc., but for some reason stoi is not recognized as well as atoi.
Please, help me.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <iostream>
#include <iomanip>
#include <ctype.h>
#include <cstdio>
#include <string>
#include <sstream>
#include <conio.h>
using namespace std;
int main()
{
char x;
cout<<"Enter x: ";
cin>>x;
if(int(x)<48 || int(x)>57){cout<<"ERROR. I WANTED A NUMBER."<<endl;
return main();
}
if(int(x)>48 || int(x)<57) {
cout<<"Base 10: "<<dec<<x<<endl;
cout<<"Hexadecimal: "<<hex<<x<<endl;
cout<<"Base 8: "<<oct<<x<<endl;
return main();
}
|