Q: scanf error in C++ - program doesn't respond Okay that is weird. When I remove int count = 1 and count++; from my code,
the program gives no error. Also if I remove scanf and do not remove
count++; program gives no error with count++;. They have nothing to do with
each other why is this happening?
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <math.h>
int IS_prime(int num) {
    int isprime = 0;
    for (int i = 2; i <= sqrt(num); i += 2) {
        if (i % 2 == 0)
            i++;

        if ((int(num) % i) == 0) {
            return isprime = 1;
            break;
        }
    }

    return isprime;
}
int main(int argc, char **argv) {
    int a = 0;

    char *buffer;

    printf("Enter your sentence:\n");
    scanf("%[^\n]", buffer);

    char array[1000] = " ";
    char temp[2] = " ";

    int count = 1;

    for (int i = 0; i < 100; i++) {
        array[i] = buffer[i];
        a = array[i];
        int PrimeFind = IS_prime(a);

        if (a % 2 == 0) {

            printf("%c", a);
            // Do nothing
        } else if (PrimeFind == 0 && a % 1 == 0) {

            printf("%c", a);

            count++;
        } else {
            a++;
            temp[0] = a;
            printf("%c", a);
        }
    }
}


A: char *buffer;

printf("Enter your sentence:\n");
scanf("%[^\n]", buffer);

buffer is not initialized there. It has a "random" value. (indeterminate is the technical term).
UPDATE
Just for fun, here's a C++ version:
Live On Coliru
#include <iostream>
#include <cmath>
#include <algorithm>

bool is_prime(int num) {
    if (num % 2 == 0)
        return false;

    for (int i = 3; i <= sqrt(num); i += 2)
        if (num % i == 0)
            return false;

    return true;
}

int main() {

    //// test your functions!
    // for (int i = 1; i < 100; ++i) { if (is_prime(i)) std::cout << i << " "; } return 0;

    std::cout << "Enter your sentence: ";
    std::string line;
    if (std::getline(std::cin, line)) {

        std::string encoded;

        size_t count = 0;

        std::transform(
            line.begin(), line.end(), 
            back_inserter(encoded),
            [&count] (uint8_t ch) -> char {
                if (ch % 2 == 0)
                    return ch; // Do nothing

                if (is_prime(ch) && ch % 2 == 1) {
                    count++;
                    return ch;
                } 

                return ch + 1;
            });

        std::cout << "Result: " << encoded << "\n";
        std::cout << "Count:  " << count   << "\n";
    }

}

When typing the sentence The lazy cow jumped over the seedy moon! the output is:
Enter your sentence: The lazy cow jumped over the seedy moon!
Result: The lazz dpx jvmped pver the teedz mppn"
Count:  9


A: Okay this fixed the problem.
I changed this:
char *buffer;

printf("Enter your sentence:\n");
scanf("%[^\n]", buffer);

To this:
char buffer[100] = " ";

printf("Enter your sentence:\n"); 
fgets(buffer, 100, stdin);

Something is wrong with "scanf" but I could not figure out. fgets is working perfectly. Thank you for your efforts.
