error: expected initializer before ‘sprintf’

Hi everyone! I'm starting (again) to write C (in this case C++), but it has been a long time since I do not use it, and besides, C++ is a bit different from C, which is what I learnt in the first place.

I just wanted to ask where are the flaws in this code, since I'm not able to get it to compile (I get the following errors):

exe_computefstatistic_C.c:53:12: error: expected initializer before ‘sprintf’
exe_computefstatistic_C.c:55:19: error: ‘command’ no se declaró en este ámbito
exe_computefstatistic_C.c:55:26: error: ‘system’ no se declaró en este ámbito

(of course, the three errors repeated in each 'if' statement.

Thanks for the help! :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>

// This script runs the program "ComputeFStatistic_v2". You have to substitute the parameters, 
// and it will analyze the desired SFTs

using namespace std;

int main()

{

double ALPHA = 2.0;
double DELTA = 1.0;
double F0 = 200.0;
double FREQ = 199.0;
double FBAND = 2.0;
int START_TIME = 1066055716;           // 17/10/13, 14:35:00 UTC
int INTEGRATION_TIME = 259200;         // 3 days
int NINETYDAYS = 7776000;              // 90 days
double VOVERC = 0.000106;              // v/c
int TSFT = 1800;                       // Time of 1 sft
int NSFTS = 144;                       // Number of sfts

double DPHI_MIN = 1/(VOVERC*F0*TSFT*NSFTS);
double PATCH = 10*DPHI_MIN;
int PATCH_SIZE = 11;
double TEMP_PATCH = PATCH/PATCH_SIZE;

int MIN_TIME = 1066055716;
int MAX_TIME = MIN_TIME + INTEGRATION_TIME;

int CAND = 1;
const char* SFTS = "'/home/david/demodulation/sfts/oneyear_signalonly_nospindowns/SFTS/*.sft'";
const char* EPHEMERIDES = "'/home/david/ephem'";
int N = 1;

for (int i = 0; i <= PATCH_SIZE - 1; i = i + 1)

    {

    for (int j = 0; j <= PATCH_SIZE - 1; j = j + 1)

        {

        double ALPHA_0= ALPHA - PATCH/2 + i*TEMP_PATCH;
        double DELTA_0= DELTA - PATCH/2 + j*TEMP_PATCH;

        if ( N < 10 )

           {
           const char* OUTPUT_FILE="/home/david/demodulation/sfts/oneyear_signalonly_nospindowns/FStatistic/demodulating_skyposition/secondtest/skypos-00$N-day0-$ALPHA_0-$DELTA_0-thirdtest.txt";
           const char* command
           sprintf(command, "lalapps_ComputeFStatistic_v2 --Alpha %d --Delta %d --Freq %d --FreqBand %d --refTime %i --outputFstat %s --DataFiles %s --ephemDir %s --minStartTime %i --maxEndTime %i --NumCandidatesToKeep %i", ALPHA_0, DELTA_0, FREQ, FBAND, START_TIME, OUTPUT_FILE, SFTS, EPHEMERIDES, MIN_TIME, MAX_TIME, CAND);

           system(command);
           }

        else if ( N >= 10  &&  N < 100 )

           {
           const char* OUTPUT_FILE="/home/david/demodulation/sfts/oneyear_signalonly_nospindowns/FStatistic/demodulating_skyposition/secondtest/skypos-0$N-day0-$ALPHA_0-$DELTA_0-thirdtest.txt";
           const char* command
           sprintf(command, "lalapps_ComputeFStatistic_v2 --Alpha %d --Delta %d --Freq %d --FreqBand %d --refTime %i --outputFstat %s --DataFiles %s --ephemDir %s --minStartTime %i --maxEndTime %i --NumCandidatesToKeep %i", ALPHA_0, DELTA_0, FREQ, FBAND, START_TIME, OUTPUT_FILE, SFTS, EPHEMERIDES, MIN_TIME, MAX_TIME, CAND);

           system(command);
           }

        else if ( N >= 100 )

           {
           const char* OUTPUT_FILE="/home/david/demodulation/sfts/oneyear_signalonly_nospindowns/FStatistic/demodulating_skyposition/secondtest/skypos-$N-day0-$ALPHA_0-$DELTA_0-thirdtest.txt";
           const char* command
           sprintf(command, "lalapps_ComputeFStatistic_v2 --Alpha %d --Delta %d --Freq %d --FreqBand %d --refTime %i --outputFstat %s --DataFiles %s --ephemDir %s --minStartTime %i --maxEndTime %i --NumCandidatesToKeep %i", ALPHA_0, DELTA_0, FREQ, FBAND, START_TIME, OUTPUT_FILE, SFTS, EPHEMERIDES, MIN_TIME, MAX_TIME, CAND);

           system(command);
           }

        N = N + 1;
        sprintf("%i,%i", i,j);

        }

    }

return 0;

}
closed account (o3hC5Di1)
Hi there,

You are missing semicolons at these lines:

const char* command ; //<---

In order to use system(), you need to #include <stdlib.h>
Please note though that using system() is advised against: http://www.cplusplus.com/forum/articles/11153/

All the best,
NwN
If you're going to write a long C program (if not for the <iostream> and using namespace std this would be C as it doesn't have a trace of C++ methodology in it) to execute a few system commands... surely a Bash, Perl, or Python script would be better for this, no?

In response to your question, though, you forgot to include (I'm assuming you want the C++ versions of the headers and not the C versions, which you honestly probably should be using) <cstdio> and <cstdlib>. <iostream> here is completely superfluous as you never use it.

Also, a few notes.
1. variable = variable + 1 = ++variable
2. i <= PATCH_SIZE - 1 = i < PATCH_SIZE
3. Most of the stuff in your if statements could have gone outside of them. You have a lot of unnecessary code that looks copy/pasted and a few lines are missing semicolons because of that.
4. Some concatenation could have made your code a lot cleaner wherever you have things to do with OUTPUT_FILE.
5. command is a const char*. This isn't going to play well with your sprintf.
6. Line 79.
7. system() is considered evil by most.

-Albatross
Topic archived. No new replies allowed.