views:

144

answers:

4
/*-> struct sam set_of_data[4]  -<*/
int main (void)
{
    int k = 0;
    for(i = 0; i < 4; ++i)
    { 
        char nm; 
        double thelow, theupp; double numbers[200];

        scanf("%c %lf %lf", &nm, &thelow, &theupp);
        for (k = 0; 
            scanf("%lf", &numbers[k]) != 0; 
            ++k) ;
        set_of_data[i] = construct_struct(nm, thelow, theupp, numbers, k);
    } 
}
    ..
/*** helper function to construct data structure***/

sam_t construct_struct(char name, double thelow, double theupp, double *numbers, int k)
{   
    sam_t stn;     
    stn.name = name;
    stn.the_lower_limit = thelow;
    stn.the_upper_limit = theupp;

    for (i = 0; i < k && numbers[k] != '\n' && numbers[k] != '\0'; ++i)
        stn.numbers[k] = numbers[k];

    return stn;
} 

these two funtion takes only half of the inputs(lines starting with D and B didnot be taken,why?

     inputs:
     C 3.25 18. 0.01 .01 .02 .04 .08 .02 .02 .05 .065 .08 .1 .13 .2 .05 .04 .04 .03 .01 .005 .0
     A 0 7.5 .054 .031 .016 .008 .116 .124 .147 .155 .039 .023 .016 .008 .124 .062 .031 .016 .008 .008 .008 .006
     D -1.5 0.5 .012 .025 .05 .1 .1 .1 .025 .012 0 0 0 .012 .025 .1 .2 .1 .05 .039 .025 .025
     B 1 3 .117 .058 .029 .015 .007 .007 .007 .015 .022 .029 .036 .044 .051 .058 .066 .073 .080 .088 .095 .103
A: 

I'm getting compiler errors here...where have you declared i, for example?

RankWeis
i declared at global
gcc
why did you declare i as global?
Peter Miehle
A: 

Looking at the inputs, you have 18. in the first line for C, try changing that to 18 without a dot.

tommieb75
A: 

i do not like side effects.

you increase the global variable i in both functions. I get a headache just by looking at the source.

sorry no help from me

Peter Miehle
This should be a comment.
KennyTM
in my opinion the ++i on the global variable i which is the main iterator in main() and the iterator in construct_struct() is the source of error for this problem, so why the downvote, only because i am a bit rude to force him to rethink his design?
Peter Miehle
A: 

The scanf function consumes input from the standard input. You will read numbers until you find the next character ("B"), so it is consumed, error is returned. But the next scanf call won't see it, but the number that comes after it, so it skips all the way to the next letter again.

You should instead use something to read in the file line by line, then use sscanf on the individual lines.

kaizer.se