Extracting Numbers from a C-String

I'm trying to write a program that extracts certain pieces from a c-string. The c-string consist of: {'9', '1', '8', '0', '0', 'w', '9', '4', '0', '7', '7', '0', '\0'}.

I'm trying (unsuccessfully so far) to pull out the part after the 'w'. Specifically, just the '94' and the '0770', but I don't know of a function or way to do this. Any ideas or suggestions. Below is the code I have so far and you can kind of see what I'm attempting.

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

using namespace std;

int main() {
    
    int strLength;
    string custNumber, year, workOrderNumber;
    char workOrder[] = {'9', '1', '8', '0', '0', 'w', '9', '4', '0', '7', '7', '0', '\0'};
    char custNum[10] = { '\0'};
    char * wPointer;
    
    strLength = strlen(workOrder);
    cout << "The length is " << strLength << endl;
    
    wPointer = (char*) memchr (workOrder, 'w', strlen(workOrder));
    if (wPointer!=NULL)
        printf("The location of 'w' is %d \n", wPointer-workOrder);
    
    custNumber = strncat(custNum, workOrder, 5);
    cout << "The customer number is " << custNumber << endl;
    
    
    
    cout << "The year of the order is " << year << endl;
    cout << "The order number is " << workOrderNumber << endl;
    
    return 0;
}
you're writing C and pretending it's C++ :-)

What you want is strncpy, not strncat in line 21
Topic archived. No new replies allowed.