-1

I just compiled this code, and it showed me this error:

Exception thrown at 0x0F2FC4DA (ucrtbased.dll) in Sample7.exe: 0xC0000005: Access violation reading location 0x97979436.

I literally have no idea what this error means as I've just been using C for a couple of months. I've also tried looking on any other websites to look for help, but I didn't find any.

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
    int i = 0, n, length = 0;
    char *abc;
    printf("\n Enter size of array: ");
    scanf_s("%d", &n);
    abc = new char[n];
    printf("\n Enter symbols: ");
    scanf_s("%s", abc);
    length = strlen(abc);
    for (i = 0; i <= n; i++)
    {
        printf("\n Your array: ", abc);
        while (length = 10)
        {
            if (abc[i] >= 'A' && abc[i] <= 'Z')
            {
                abc[i] = ' ';

            }
            printf("\n Your array after deleting A-Z symbols",abc);
        }

    }
    delete[]abc;
    _getch();
    return 0;
Blackwood
  • 4,504
  • 16
  • 32
  • 41
PTaHHHa
  • 67
  • 1
  • 11

3 Answers3

1

You are receiving this error because you are accessing memory out of allocated space. My guess is you are accessing index the char array out side of it's boundary .You need to debug your code line by line to see where this happens.

On the first look of your code, I found the following mistakes.

for (i = 0; i <= n; i++) I think you meant: for (i = 0; i < n; i++)

also while (length = 10) should be while (length == 10)

alt255
  • 3,396
  • 2
  • 14
  • 20
1

First of all your main culprit is scanf_s("%s", abc); because while your are reading a string through scan_f your need to provide the size of the string like scanf_s("%s", abc, n);. Also you need few correction in your code as well. You enter the size of an array from consul. For example Enter size of array: 10 and you entered 10 here. Now size of the array is 10 hence your loop should continue from 0-9 total 10 location, hence your for loop should be for (i = 0; i <= n; i++). Second your while loop while (length = 10) this will always true and hence it become a infinite loop. Hence it should be while (length = 10) but you don't even this while loop at all. 3rd your printf statement 1st should be printf("\n Your array: %s", abc); and second one should be printf("\n Your array after deleting A-Z symbols %s ",abc); but this statement should be at the end of the program after delete statement.

I have corrected your program below. Try with this:-

#include "stdafx.h"
#include "stdafx.h"
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
  int i = 0, n, length = 0;
  char *abc;
  printf("\n Enter size of array: ");
  scanf_s("%d", &n);
  abc = new char[n];
  printf("\n Enter symbols: ");
  scanf_s("%s", abc, n);//your scanf_s function was wrongly define.This one is correct.
  printf("\n Your array1: %s", abc);
  for (i = 0; i < n; i++)
  {
    printf("\n Your array: %s", abc);
    //while (length == 10) You don't need this while loop at all
    //{
        if (abc[i] >= 'A' && abc[i] <= 'Z')
        {
            abc[i] = ' ';

        }

      //}
        printf("\n Your array after deleting A-Z symbols :%s", abc);
}
delete[]abc;
_getch();
return 0;
}
Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17
1

You might have the write violation due to attempting to change a string literal. You cannot modify string abc if it was specified as:

char *abc = "String Literal is Read-Only";

Below I used C's malloc to create a dynamically sized string buffer:

    int i = 0, n, length = 0;
    char* abc;
    printf("\n Enter size of array: ");
    scanf_s("%d", &n);
    abc = malloc(n * sizeof(char));
    //  C++: abc = new char[n];
    printf("\n Enter symbols: ");
    // symbol size should be < buffer abc size n
    scanf_s("%s", abc, n);  
    length = strlen(abc);
    printf("\n Your array: %s", abc);
    for (i = 0; i < length; i++)
    {
            if (abc[i] >= 'A' && abc[i] <= 'Z')
            {
                abc[i] = ' ';
            }
    }
    printf("\n Your array after deleting A-Z symbols\n %s", abc);
Leon Chang
  • 669
  • 8
  • 12