[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 807
  • Last Modified:

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.
0
scottd1978
Asked:
scottd1978
  • 5
  • 4
1 Solution
 
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
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now