scanf error in C++ - program doesn't respond -


okay weird. when remove int count = 1 , count++; code, program gives no error. if remove scanf , not remove count++; program gives no error count++;. have nothing each other why happening?

#include <stdio.h> #include <string> #include <stdlib.h> #include <math.h> int is_prime(int num) {     int isprime = 0;     (int = 2; <= sqrt(num); += 2) {         if (i % 2 == 0)             i++;          if ((int(num) % i) == 0) {             return isprime = 1;             break;         }     }      return isprime; } int main(int argc, char **argv) {     int = 0;      char *buffer;      printf("enter sentence:\n");     scanf("%[^\n]", buffer);      char array[1000] = " ";     char temp[2] = " ";      int count = 1;      (int = 0; < 100; i++) {         array[i] = buffer[i];         = array[i];         int primefind = is_prime(a);          if (a % 2 == 0) {              printf("%c", a);             // nothing         } else if (primefind == 0 && % 1 == 0) {              printf("%c", a);              count++;         } else {             a++;             temp[0] = a;             printf("%c", a);         }     } } 

char *buffer;  printf("enter sentence:\n"); scanf("%[^\n]", buffer); 

buffer not initialized there. has "random" value. (indeterminate technical term).

update

just fun, here's c++ version:

live on coliru

#include <iostream> #include <cmath> #include <algorithm>  bool is_prime(int num) {     if (num % 2 == 0)         return false;      (int = 3; <= sqrt(num); += 2)         if (num % == 0)             return false;      return true; }  int main() {      //// test functions!     // (int = 1; < 100; ++i) { if (is_prime(i)) std::cout << << " "; } return 0;      std::cout << "enter 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; // 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 sentence the lazy cow jumped on seedy moon! output is:

enter sentence: lazy cow jumped on seedy moon! result: lazz dpx jvmped pver teedz mppn" count:  9 

Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -