Code treats a number as separate digits

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();
}
The main function has several special properties:
....1) It cannot be used anywhere in the program
........a) in particular, it cannot be called recursively
........b) its address cannot be taken
http://en.cppreference.com/w/cpp/language/main_function


Something like this:

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

int main()
{
    try_again:

        std::cout << "enter an integer: " ;

        int number ;
        if( std::cin >> number ) // a number was entered
        {
            std::cout << "    decimal: " << std::dec << number << '\n'
                      << "hexadecimal: " << std::showbase << std::hex << number << '\n'
                      << "      octal: " << std::oct << number << '\n' ;
        }

        else // input error
        {
            std::cout << "error. I wanted a number.\n" ;

            // http://en.cppreference.com/w/cpp/io/basic_istream/ignore
            std::cin.clear() ; // clear the failed state of cin

            // http://en.cppreference.com/w/cpp/io/basic_istream/ignore
            std::cin.ignore( 1000000, '\n' ) ; // throw the junk away

            // this is just to needle the 'goto is (always) unmitigated evil' kiddies
            goto try_again ; // try again
        }
}
Alright, got this, though why do I need to put exactly 1000000 inside of ignore?


> why do I need to put exactly 1000000 inside of ignore?

You do not need to put exactly 1000000 inside of ignore; just put in some reasonably large number
(which is larger than the maximum number of characters that the user would have typed in.)

This should work quite well:
1
2
3
// extract and discard characters till a new line is found 
// (up to a maximum of 1000 characters)
std::cin.ignore( 1000, '\n' ) ; // throw the junk away 
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
#include <iostream>
#include <string>
#include <vector>
using namespace std;

//======================================================================

string myBase( int n, int base ) { return n == 0 ? "" : myBase( n / base, base ) + to_string( n % base ); }

//======================================================================

bool output( vector<int> base )
{
   int number;
   cout << "Enter a positive integer: ";
   
   if ( cin >> number )
   {
      for ( int r : base ) cout << "Base " << r << ": " << myBase( number, r ) << '\n';
      return true;
   }
   else           // tidy up in order to try again
   {
      string dummy;
      cin.clear();
      getline( cin, dummy );
      return false;
   }
}

//======================================================================

int main()
{
   vector<int> base = { 10, 16, 8, 2, 5 };
   while ( !output( base ) );
}

//====================================================================== 


Enter a positive integer: sos
Enter a positive integer: a2b
Enter a positive integer: 23
Base 10: 23
Base 16: 17
Base 8: 27
Base 2: 10111
Base 5: 43
Topic archived. No new replies allowed.