need help with coding ,

example : The input of argv[2] is declared as base if base argument is b2
how do I check that the number is in the range of 2 - 16.

argument 2 must contain the b i don't have the option of taking it out.

with my coding so far "X" even for range to 2-16 i believe it's because of the b. How can i just check the number ? in this string.




#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
if(argc=3)
{
int num1 = atoi(argv[1]);
char base = atoi(argv[2]);


if( base < 2 || base > 16 )
{cout << "X" << endl;

const string digits("0123456789ABCDEF");
bool is_neg = num1 <0 ;
string result;
}

}
system("PAUSE") ;
return 0;
}
Last edited on
First of all, next time you post your code, please use the code tags. They show up as "<>" in the Format box to the right.

As far as your code. In the line:

if(argc=3)

You are actually assigning 3 to arg. This will always return true, so you will always execute the following lines. You want the comparison operator "==".

In the line:
char base = atoi(argv[2]);

You are converting argv[2] trying to an integer value and placing it into a char variable. This is legal, but it's not what you want.

After you output "X", you make a bunch of declarations/assignments, and do nothing with them, so I don't know what you are trying to do with them.

To answer your specific question, you must take argv[2] and treat it as a char*. After you verify that the first character is 'b', then start at the second character and convert it to an integer. Something like:

1
2
3
4
5
6
7
8
9
char* bVar = argv[2];
int bVal;
if (bVar[0] == 'b')
{
    bVal = atoi(bVar + 1);
    cout << "bVal = " << bVal << endl;
}
else
{...}



(Before you are done you should also verify that all characters after the first are digits, but for now just assume the format is correct.)
Topic archived. No new replies allowed.