atoi not working right???

I'm using atoi to conver a single character into an integer, which works fine the first time.  When I do this a second time though, it keeps the first value for some reason.  Here is the code.      
      // Accept user input
      do{
      cout << "Enter start address(0-7): ";
      cin >> startc;

      // convert the character to an integer
      starti = atoi(&startc);
      }while (starti < 0 || starti > 7 || !(isdigit(startc)));

      do{
      cout << endl << "Enter end address(0-7): ";
      cin >> endc;
      // convert the character to an integer
      endi = atoi(&endc);
      }while (endi < 0 || endi > 7 || !(isdigit(endc)));

When I say it keeps the first value, I mean this:  If I enter 3 for startc, and then enter 2 for endc, endc shows up with 32.
scottd1978Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nietodCommented:
>> I'm using atoi to conver a single character
>> into an integer,
atoi()  doesn't convert a single character.  it converts a string of characters that are stored in a character array.  This string is terminated by a character whose value is NUL (0).  

If you specify a pointer to a single characacter to atoi(), the code will compile, but it will not work correctly, as you can see.  You must specify a pointer to a character array containing a propper NUL terminated string.

so

char one1 = '1';
char one[2] = "1";

atoi(&one1); // error
atoi(one2); // okay.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
scottd1978Author Commented:
Thanks billy!
0
SuperMarioCommented:
Yes, take the reference parameter out of there and make it a direct argument

int starti = atoi(startc);
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

nietodCommented:
Only if startc is a NUL terminated string.  Not if it is a pointer to a single char.
0
scottd1978Author Commented:
I used the array idea. // Accept user input
     32       do{
     33       cout << "Enter starting address(0-7) for your omega switch: ";
     34       cin >> startc[0];
     35       startc[1] = 0;
     36
     37       // convert the character to an integer
     38       starti = atoi(startc);
     39       }while (starti < 0 || starti > 7 || !(isdigit(startc[0])));
     40
     41       do{
     42       cout << endl << "Enter ending address(0-7) for your omega switch: ";
     43       cin >> endc[0];
     44       // convert the character to an integer
     45       endi = atoi(endc);
     46       }while (endi < 0 || endi > 7 || !(isdigit(endc[0])));
0
nietodCommented:
Well, that looks like it shoudl work, but I have to wonder why you do it that way.

If you want to read a numerical value, juse read it directly, like

int X;
cin >> X;

If you want to read a string then use the getline() member function like

const int StrLen = 10;
char Str[StrLen];
cin.getline(Str,StrLen,'\n');
0
scottd1978Author Commented:
I had to test for non-number characters ... do you know of a better way to do that?  oh ... I forgot about the getline ... haven't done cpp in a while.
0
nietodCommented:
>> I had to test for non-number characters
Can you be more specific?
i.e test when reading a number?
test after a number?
are spaces and punctuation okay?

It depends on the details, but usually the approach to input validation is to read the input as a string, then validate the string.  It tends to give the programmer more "control".
0
scottd1978Author Commented:
exactly what the line of code shows.
while (starti < 0 || starti > 7 || !(isdigit(startc[0])));

I have to have a number between 0 and 7.  I would like to stop spaces and more than a single number from being enterd.
0
nietodCommented:
I should have re-read the question history....

it depends ont he rules you want to set for the user.  For example, the following says that the user must type a digit character from 0 to 7 on the line.  They may type leading spaces or trailing space, but no other characters.  So "  0   " is fine, but " 0  3  " is not.  Even "01" is not.  

string Lin;
do{
    cout << endl << "Enter ending address(0-7) for your omega switch: ";
    getline(cin,Lin);
    int BgnOff = Lin.find_first_not_of(' ');
    int EndOff = Lin.find_last_not_of(' ');
    Lin = Lin.subStr(BgnOff,EndOff-BgnOff+1); // Remove leading and trailing spaces.
   if (Lin.length() == 1 && '0'<= Lin[0] && Lin[0] <- '7')
  {
      endi = Lin[0] - '0';
  }
  else
  {
   cout << " Invalid address" << endl;
   }
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.