David12123
David12123

Reputation: 119

c++ error identifier not found for function?

this is my first steps in C++ , and I'm trying to run a function I need to use

but I get this error :

Severity Code Description Project File Line Suppression State Error C3861 'findAndRotateBlankNumbers': identifier not found ConsoleApplication2 c:\users\source\repos\consoleapplication2\consoleapplication2\consoleapplication2.cpp 29

and this is the code:

// ConsoleApplication2.cpp : Defines the entry point for the console application. //

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

enum States {
    needBlank,
    needNumber,
    collectNumbers,
};

bool finish = false;
int i = 0;

int main()

{


    char data[] = { "they're 2fast 96 345 6789 a11 24x 2424" };


    printf(data);
    findAndRotateBlankNumbers(data);
    printf(data);

    system("pause");





    return 0;
}


void findAndRotateBlankNumbers(char* ptr) {
    char* first;
    char* last;
    for (uint8_t state = needBlank; *ptr; ptr++) {
        switch (state) {
        case needBlank:
            if (*ptr == ' ') {
                state = needNumber;
            }
            break;
        case needNumber:
            if (isdigit(*ptr)) {
                state = collectNumbers;
                first = ptr;
            }
            else if (*ptr != ' ') {
                state = needBlank;
            }
            break;
        case collectNumbers:
        {
            bool swap = false;
            if (!isdigit(*ptr)) {
                last = ptr - 1;
                state = (*ptr == ' ' ? needNumber : needBlank);
                swap = true;
            }
            else if (!ptr[1]) {
                last = ptr;
                swap = true;
            }
            if (swap) {
                if (last != first) {
                    for (int8_t nums = (last - first + 1) / 2; nums--; ) {
                        char swap = *first;
                        *first++ = *last;
                        *last-- = swap;
                    }
                }
            }
            break;
        }
        }
    }

}

what is this ?


I have change the order of the code to like you suggested :

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

enum States {
    needBlank,
    needNumber,
    collectNumbers,
};

bool finish = false;
int i = 0;

void findAndRotateBlankNumbers(char* ptr) {
    char* first;
    char* last;
    for (uint8_t state = needBlank; *ptr; ptr++) {
        switch (state) {
        case needBlank:
            if (*ptr == ' ') {
                state = needNumber;
            }
            break;
        case needNumber:
            if (isdigit(*ptr)) {
                state = collectNumbers;
                first = ptr;
            }
            else if (*ptr != ' ') {
                state = needBlank;
            }
            break;
        case collectNumbers:
        {
            bool swap = false;
            if (!isdigit(*ptr)) {
                last = ptr - 1;
                state = (*ptr == ' ' ? needNumber : needBlank);
                swap = true;
            }
            else if (!ptr[1]) {
                last = ptr;
                swap = true;
            }
            if (swap) {
                if (last != first) {
                    for (int8_t nums = (last - first + 1) / 2; nums--; ) {
                        char swap = *first;
                        *first++ = *last;
                        *last-- = swap;
                    }
                }
            }
            break;
        }
        }
    }

}

int main()

{


    char data[] = { "they're 2fast 96 345 6789 a11 24x 2424" };


    printf(data);
    findAndRotateBlankNumbers(data);
    printf(data);

    system("pause");





    return 0;
}

but now I get other errors:

Severity Code Description Project File Line Suppression State Error C4703 potentially uninitialized local pointer variable 'first' used ConsoleApplication2 c:\users\source\repos\consoleapplication2\consoleapplication2\consoleapplication2.cpp 52

?

Upvotes: 0

Views: 7921

Answers (1)

perivesta
perivesta

Reputation: 4031

Your function findAndRotateBlankNumbers is defined after you want to call it in main. So the compiler does not know this function exists yet when compiling the main function. To fix this you can either

  • move the findAndRotateBlankNumbers function above main
  • add the function declaration at the top and leave the definition where it is: void findAndRotateBlankNumbers(char* ptr);

As to your second error:

The compiler is complaining that you declare the variable first without assigning a value to it at initialization. You should give it a sensible default value like char* first = nullptr;

Same with char* last = nullptr;

If your compiler does not support nullptr, use NULL instead;

Upvotes: 1

Related Questions