-1
  1. Is this the only way to pick up a string? Can not you take character by character into an array?
  2. I get an error accessing memory. The code and the error are attached.
void main()
{
    int counA = 0, countB = 0, Na, Nc, i, index;
    printf("enter Na and Nc\n");
    scanf_s("%d%d", &Na, &Nc);
    char DNA[1000], SEQ[1000], str[1000];

    printf("enter mulekula\n");
    scanf_s("%s", str);
}

input:

2
3
AAA

message I get:

exception thrown...
Zoe
  • 27,060
  • 21
  • 118
  • 148
  • 1
    Post the code as text. – imreal May 29 '19 at 15:40
  • 1
    Paste code as text, not as an image. – Reticulated Spline May 29 '19 at 15:40
  • Where is your code? There isn't even a picture of your code. Paste your code into the question and take 2-3 minutes and learn how to format questions properly. It's really easy. Read this: [ask] and this: [mcve] – Jabberwocky May 29 '19 at 15:42
  • There is still no code. We cannot read your mind. Please [edit] your question and put your code _into your question_. – Jabberwocky May 29 '19 at 15:47
  • 1
    You are using `scanf_s` with `%s` specifier incorrectly. *"Unlike `scanf` ... `scanf_s` ... requires the buffer size to be specified for all input parameters of type c, C, s, S, or string control sets that are enclosed in []. The buffer size in characters is passed as an additional parameter immediately following the pointer to the buffer or variable."* The compiler should be issuing a warning about this. – Weather Vane May 29 '19 at 16:00
  • You're doing it wrong, read carefully the documentation of [`scanf_s`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/scanf-s-scanf-s-l-wscanf-s-wscanf-s-l), or just use `scanf` (without the `_s`) and ignore the Microsoft warnings. – Jabberwocky May 29 '19 at 16:01
  • "Is this the only way to pick up a string?" No, you can use `fgets()` but you need to [remove any trailing newline](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221). – Weather Vane May 29 '19 at 16:07
  • @Jabberwocky it is better to `#define _CRT_SECURE_NO_WARNINGS` rather than hunt though warnings for relevant ones. – Weather Vane May 29 '19 at 16:13
  • I added the current line you said: #define _CRT_SECURE_NO_WARNINGS And still requires me to use: s_scanf () – Yossi Alfiya May 29 '19 at 16:16
  • It must be *before* the `#include` files.I also `#define _CRT_SECURE_NO_DEPRECATE` and `#define _CRT_NONSTDC_NO_DEPRECATE`. – Weather Vane May 29 '19 at 16:20
  • i added and still i get this message error: Severity Code Description Project File Line Error C4996 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. Project5 c:\users\user\documents\visual studio 2015\projects\project5\project5\dna.c 22 – Yossi Alfiya May 29 '19 at 16:20
  • It must be ***before*** the `#include` files. But having reverted to `scanf` you must still restrict the input length with `scanf("%999s", str);` It's true that the function is unsafe, but no function is safe when used incorrectly, including MS's allegedly "safe" (and non-portable) version". – Weather Vane May 29 '19 at 16:21
  • It works!!! Thank you. But I still did not understand why I could not pick up characters in the following way?char DNA[1000]; for (i = 0; i < 1000; i++) { scanf("%c", &DNA[i]); } – Yossi Alfiya May 29 '19 at 16:30
  • Please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). – Weather Vane May 29 '19 at 17:51

1 Answers1

0

fgets can help , see this code

#include<stdio.h>
#include<string.h>
int main()
{
chat str[1000];

fegts(str,sizeof(str),stdin);

str[strlen(str)-1]='\0';
return 0;

}

This code will take input from console till you press enter , but take care that fgets includes the new line character('\n') with input so we need to remove it and end the string with null character ('\0') .

This is the reason for this line of code str[strlen(str)-1]='\0'; so strlen(str) will return the length of string including new line character.

for example :

if input was "fo" , so it stored in str as follow:

str[0] = 'f' , str[1]='o' , str[2]='\n' and str[3]='\0'. So strlen(str)-1 will return 2 , so str[2] will be assigned to null character '\0'

And finally the string will be as follow:

str[0] = 'f' , str[1]='o' and str[2]='\0'.

Fouad
  • 39
  • 3