Jump to content

C++ How to assign an number (character type) in a char array to an actual integer value?

MikeHoward

My goal is to read the first line of my colors.txt file and assign that integer value to `blue`. However, in my assignment I am instructed to use a char array `red` in order to assist in getting the first line of code. I have succeeded in doing so, but am having trouble with assigning it to `blue`.

    int main() {
        ifstream col; 
        char red[50]; 
        int blue = 0; 
        
        col.open("colors.txt");
        col.getline(red, 2, '\n');
        cout << "Blue:  " << red << endl; 
    }



**colors.txt file below**

 

9
8
7
6

So far I have thought about using the atoi function, but I'm not exactly sure if it will work in this case. Would it work if I copied the character array into the heap and then used a pointer to get the value?

Link to comment
Share on other sites

Link to post
Share on other sites

After you read a number to red array you can use atoi on it, you don't need to allocate another array for it.

 

Link to comment
Share on other sites

Link to post
Share on other sites

The easy way out:

 

#include <iostream>
#include <fstream>

int main() {
  ifstream col; 
  char red[50]; 
  int blue = 0; 

  col.open("colors.txt");
  col.getline(red, 2, '\n');
  // Use atoi to convert C-Style-String to an int value. 
  // This is NOT recommended though, because it doesn't handle errors well,
  // see here under "Exceptions": http://www.cplusplus.com/reference/cstdlib/atoi/
  blue = atoi(red); 
  cout << "Blue:  " << blue << endl; 
}

And a bit more complex, but correct:

#include <iostream>
#include <fstream>
#include <stdlib.h>     /* strtol */
#include <ctype.h>      /* isspace */

int main() {
    ifstream col; 
    char red[50]; 
    int blue = 0; 

    col.open("colors.txt");
    col.getline(red, 2, '\n');

    // "endp" points to the end where conversion stopped
    // see http://www.cplusplus.com/reference/cstdlib/strtol/         
    char *endp;
    blue = strtol(red, &endp, 10);
    // Check, whether the whole string was converted into a number
    // If that's the case, endp points towards the character directly after the number part.
    // That should be a whitespace or a 0 if the number has been converted completely.
    if (*endp == 0 || isspace(*endp))
      cout << "Blue:  " << blue << endl; 
    else
      cerr << "Invalid number: Unconvertable characters detected!" << endl;
}

HTH ;-)

Edited by Questargon
Handling eventual whitespaces after the number part of the string.

CPU Ryzen 7 5800X | MoBo MSI B550 Gaming Plus | RAM 32GB Teamgroup @3600/18 | GPU EVGA RTX 3070 Ti FTW | Case Enthoo Pro M SE
PSU bq! Straight Power 11 Plat. 750W CM | Cooling Scythe Fuma 2 & 5x Corsair ML140 | Sound SB Z Retail | Storage Samsung 970 EVO 500GB
Display(s) Iiyama GB3461WQSU, Dell 24", LG 34UM95 | Keyboard Kinesis Freestyle Edge | Mouse Logitech G900 Chaos Spectrum | OS Windows 11

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×