Click here to Skip to main content
15,038,466 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm writing a C code with the idea create a function input numbers into 2 arrays(Read_data) function. Then I will call it in the main function and print out those 2 arrays' elements. My relative code is as below:
 #include<stdio.h>
 #include<conio.h>
 #include<math.h>
 #define NUMBER 3
 void Read_data(double* x, double* y)
 {
// 座標値格納配列宣言
 for (int i = 0; i < NUMBER; i++)
  {
    printf_s("座標値xを入力してください:\n");
    printf_s("x[%d]= ", i);
    scanf_s("%3f", &x[i]);
  }
 for (int i = 0; i < NUMBER; i++)
  {
    printf_s("\n座標値xを入力してください:\n");
    printf_s("y[%d]= ", i);
    scanf_s("%3f", &y[i]);
  }
 }
int main()
 {
  double X[NUMBER], Y[NUMBER];
  Read_data(X,Y);
  for (int i = 0; i < NUMBER; i++)
  {
    printf("X[%d]= %f\n", i, X[i]);
  }
  for (int i = 0; i < NUMBER; i++)
  {
    printf("Y[%d]= %f\n", i, Y[i]);
  }

But when I compiled it first I input the numbers and then it appears very strange numbers that I don't know what type of errors cause this.
I will text out those number because I can't paste the picture there
座標値xを入力してください:
x[0]= 5
座標値xを入力してください:
x[1]= 6
座標値xを入力してください:
x[2]= 7

座標値xを入力してください:
y[0]= 8

座標値xを入力してください:
y[1]= 3

座標値xを入力してください:
y[2]= 9
X[0]= -92559604497122534761720676471189149555955165722294310802554880.000000
X[1]= -92559604521067777587750189883038321855178746716337109586673664.000000
X[2]= -92559604545013020413779703294887494154402327710379908370792448.000000
Y[0]= -92559604568958263239809216706736666453625908704422707154911232.000000
Y[1]= -92559604425286806283632136235641632658284422740165914450198528.000000
Y[2]= -92559604580930884652823973412661252603237699201444106546970624.000000


What I have tried:

I have separated that Read_data() function into 2 functions each input 1 array but It still not work out.
If anyone can spot any problem in my code above then I will be very grateful!
Thank you!
Posted
Updated 17-May-21 2:16am
v2

1 solution

When I compiled your code I received the following error message:
cp.cpp(13): warning C4477: 'scanf_s' : format string '%3f' requires an argument of type 'float *', but variadic argument 1 has type 'double *'

i changed the format strings and all worked correctly.
   
Comments
Member 14629414 17-May-21 8:22am
   
@Richard MacCutchan Oh I'm using Visual Studio 2019 community as usual:)) I will follow yours and try to fix it then I will tell you the results! Thank you
Member 14629414 17-May-21 8:25am
   
@Richard MacCutchan How stupid I'm!!!!!
Richard MacCutchan 17-May-21 8:42am
   
Don't feel bad, it is an easy mistake.
Member 14629414 17-May-21 8:52am
   
@Richard MacCutchan I wonder what editor/compiler you're using . My VS 2019 didn't show that errors or I don't know how to search for it?
Richard MacCutchan 17-May-21 9:12am
   
I am using VS 2019. Make sure that the compiler warning level is set to 3 or above in your project settings. If compiling at the command line use -W3 in the options.
Member 14629414 17-May-21 9:13am
   
Thank you so much!
Patrice T 17-May-21 8:50am
   
That is how one learn "Trial and error"
Richard MacCutchan 17-May-21 9:12am
   
It's certainly how I learn.
Patrice T 17-May-21 9:22am
   
Me too

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900