3x syntax errors :S, Help a student finalise an assignment

This is a discussion on 3x syntax errors :S, Help a student finalise an assignment within the C Programming forums, part of the General Programming Boards category; Hi everyone, This is my first post so dont expect anything fantastic. Anyway, As part of my university degree I ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    7

    3x syntax errors :S, Help a student finalise an assignment

    Hi everyone,
    This is my first post so dont expect anything fantastic. Anyway, As part of my university degree I have to do a short programming course based around C. Now, I'm doing an assignment and because I havnt been programming for years I simply cannot figure out 3 syntax errors due to inexperience. My assignment is finished in every other aspect but I need the syntax errors sorted other wise I can only get 30%(!) for the whole thing. Please help me finish this off

    These are the errors:

    Data_Massage.c:147: error: syntax error before "fscanf"
    Data_Massage.c:149: error: incompatible type for argument 1 of "feof"
    Data_Massage.c:203: error: incompatible types in assignment

    And this is my code, I've hightlighted the error points

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char **argv)
    {
    FILE * output = stdout;
    FILE * input = stdin;
    
    int i;
    int j;
    int data[i];
    double maximum;
    double minimum;
    double average;
    double value;
    int endvalue;
    int counter;
    double gtvalue;
    double valuedata[counter];
    int feof(FILE *stream);
    //detecting the number of Command Line Arguments
    {
            if (argc<2)
    printf("There are not enough command line arguments. Please restart the program and include more than 1 command line argument. If you are unsure of what command line arguments are avalible, type '-help' after the 'a.out' statement");
            return 1;
    }
    //Searching the 'help' command line argument
    for (i=0;i<argc;i++)
            {
            i=0;
            if (strcmp(argv[i],"-help")==0);
                    printf("blah");
                    return 1;
            }
    {
    endvalue = -1;
    counter = 0;
    while (endvalue != counter) do
            fprintf(output,"%c","Please input next value");
            fscanf(input,"%d\n" , value);
    
            if (feof(value) != 0)
            {
            endvalue = counter;
            }
            else
            {
            valuedata[counter]=value;
            counter=counter+1;
            }
    }
    for (i=0;i<argc;i++)
            {
            if (strcmp(argv[i],"-max")==0)
                    {
                    i=0;
                    maximum=-999999999;
                    for (i=0;i<endvalue;i++)
                            if (data[i]>maximum)
                                    maximum=data[i];
                    }
            if (strcmp(argv[i],"-min")==0)
                    {
                    i=0;
                    minimum=999999999;
                    if (data[i]<minimum);
                                    minimum=data[i];
                    }
            if (strcmp(argv[i],"-average")==0)
                    {
                    i=0;
                    average=0;
                    {
                    for (i=0;i<endvalue;i++)
                            average=average+data[i];
                    }
                    average=average/endvalue;
                    }
            if (strcmp(argv[i],"-gta")==0)
                    {
                    {
                    i=0;
                    average=0;
                    {
                    for (i=0;i<endvalue;i++)
                            average=average+data[i];
                    }
                    average=average/endvalue;
                    i=0;
                    j=0;
                    for (i=0;i<endvalue;i++)
                            if (data[i]>average)
                                    j=j+1;
                    }
            if (strcmp(argv[i],"-gt")==0)
                    {
                    value=argv[i];
                    i=0;
                    j=0;
                    for (i=0;i<endvalue;i++)
                            if (data[i]>gtvalue)
                                    j=j+1;
                    }
            }
    }
    I've formatted it to what was suggested(I think) and to the specifications of the course outline (hence the large amount of indents).

    Please help, all my tutors are on holiday and the course director has made it blatently obvious that he wont help us.

    Thanks in advance for your time
    Alex

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Those are quite simple to fix.

    The first one is because you are missing braces. And your indentation and braces need major help. That code is very hard to read.

    Code:
    void Foo()
    {
      if (cond)
      {
        //Statements here
      }
    
      while (cond}
      {
        //Statements here
      }
    
      do
      {
         //Statements here
      } while (cond);
      
      for (int i=0;i<10;i++)
      {
         //Statements here
      }
    }
    Some people use this format for for loops.
    Code:
    for (int i=0;i<10;i++) {
      //Statements here
    }
    It is a matter of personal preference, but I prefer not to use this format.

    Just an example. Also we don't do homework. Half of us here don't have the opportunity to go ask the professor or fellow students and must solve our own problems through research on the internet (think www.google.com), books (think www.amazon.com), websites and trial and lots of error. If we can solve them this way, I'm sure you can as well.
    You won't learn anything if we spoon feed you answers.

    If you get 30% on the assignment, that is what you deserve - and from the looks of your code, 30% is about right. 30% would mean that you don't understand the principle the assignment is teaching, and that seems to be correct in this case. Do some studying.
    Last edited by VirtualAce; 07-24-2006 at 11:08 PM.

  3. #3
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    I will help you out with your first error because the error message is pretty misleading. It's actually caused by an earlier statement which you did not use correctly, remember that in C do while exists (not while do) and it takes the form:
    Code:
    do
    {
        variable declarations
        statements
    } while(condition);
    fscanf(input,"%d\n" , value); isn't incorrect syntax (although you're probably going to get a segfault). Also you could use scanf and printf rather than fscanf fprintf.
    You should look up both scanf and printf because you have slight problems.
    Lookup feof again (you have it declared at the top of your program, which is also unnecesarry), value is a double not a FILE *.

    You are declaring variables (specifically valuedata and data) as variably sized arrays which is only supported by c99--also the values you are using (i and counter) are uninitialized and probably have junk in them. You should look up array declaration syntax.
    Lastly argv[i] is a char *, value is a double, this is why you get an error.

    You have more work left on this app than you think.

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    7
    Bubba; Thanks for the pointer with the alternate "for" loop format, I prefer the other one simply because its a bit more compact and neater

    Valis; Ok, After the help you gave me, I solved every error there was and now I have only 3 warnings to get rid of.

    I feel kind of stupid actually, One of them was me forgeting about the "atoi" command. The 3 warnings are nothing major but I dont understand what this means:

    Data_Massage.c:151: warning: passing argument 1 of &#226;feof&#226; makes pointer from integer without a cast

    Im not asking how to fix it, just wondering what it means

    Thanks guys
    Alex

  5. #5
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    It means you are passing an integer to feof instead of what it expects, a FILE * that is. So:
    Code:
    int imNotAFilePointer;
    feof(imNotAFilePointer);
    In some instances this is ok, but as a general rule you can assume you have made a mistake.

  6. #6
    Registered User
    Join Date
    Jul 2006
    Posts
    7
    Thanks for your help thus far. I dont want to take up to much of anyone's time but can anyone spot an obvious error with this code? Im getting a segmentation fault :s . Any ideas?
    Code:
    #include <stdio.h>
    #include <string.h>
    int main(int argc, char **argv)
    {
    
    int i=0;
    int j=0;
    int data[i];
    double maximum;
    double minimum;
    double average;
    double value;
    int endvalue;
    int counter;
    double gtvalue;
    
    //detecting the number of Command Line Arguments
    {
            if (argc<2)
    printf("There are not enough command line arguments. Please restart the program and include more than 1 command line argument. If you are unsure of what command line arguments are avalible, type '-help' after the 'a.out' statement. Otherwise, please refer to the help document\n");
            return 1;
    }
    //Searching the 'help' command line argument. If found it will display the simplistic help snippet
    for (i=0;i<argc;i++)
            {
            if (strcmp(argv[i],"-help")==0)
                    {
                    printf("%s","The following Command Line Arguments are avalible for use with this program;");
                    printf("%s","'-max' will find the maximum value of the supplied number.");
                    printf("%s","'-min' will find the minimum value of the supplied numbers.");
                    printf("%s","'-average' will find the average of the set of supplied numbers.");
                    printf("%s","'-gta' will find the number of values greater than the average.");
                    printf("%s","'-gt' will find the number of values greater than the number specified after it in the format '-gt XXX' where XXX is your number.");
                    return 1;
                    }
            }
    {
    
    counter = 0;
            if (feof(NULL) != 0)
            {
            endvalue = counter;
            return 1;
            }
            else
            {
            counter=counter+1;
            }
    }
    for (i=0;i<argc;i++)
            {
            //This If statement searches for the presence of the "-max" CLA, If it is present it finds the maximum number and outputs this to the user
            if (strcmp(argv[i],"-max")==0)
                    {
                    i=0;
                    maximum=-2147482648;
                    for (i=0;i<endvalue;i++)
                            if (data[i]>maximum)
                                    maximum=data[i];
                    printf("%s","The maximum of the values supplied is; %lf, &maximum");
                    }
            //This if statement find the presence of "-min". If present, it finds the minimum and outputs it to the user
            if (strcmp(argv[i],"-min")==0)
                    {
                    i=0;
                    minimum=2147482648;
                    if (data[i]<minimum);
                                    minimum=data[i];
                    printf("%s","The minimum of the values supplied is; %lf, &minimum");
                    }
            //This if statement detects the "average" CLA and, if poresent, finds the average and displays to the user
            if (strcmp(argv[i],"-average")==0)
                    {
                    i=0;
                    average=0;
                    {
                    for (i=0;i<endvalue;i++)
                            average=average+data[i];
                    }
                    average=average/endvalue;
                    printf("%s","The average of the values supplied is; %lf, &average");
                    }
            //The if statement detects the "-gta" CLA and acts up on its presence by finding the number of values greater than the average
            if (strcmp(argv[i],"-gta")==0)
                    {
                    i=0;
                    average=0;
                    {
                    for (i=0;i<endvalue;i++)
                            average=average+data[i];
                    }
                    average=average/endvalue;
                    i=0;
                    j=0;
                    for (i=0;i<endvalue;i++)
                            if (data[i]>average)
                                    j=j+1;
                    printf("%s","The number of values greater than the average is; %i, &j");
                    }
            //The number of values greater than a specified value is found by this stamement and further processing.
            if (strcmp(argv[i],"-gt")==0)
                    {
                    value=atoi(argv[i]);
                    i=0;
                    j=0;
                    for (i=0;i<endvalue;i++)
                            if (data[i]>gtvalue)
                                    j=j+1;
                    printf("%s","The number of values greater than the specified value is; %i, &j");
                    }
            }
    }
    Really sorry that people are having to read this, I just cant see the error.

    Alex

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    123
    Code:
    //detecting the number of Command Line Arguments
    {
            if (argc<2)
    printf("There are not enough command line arguments. Please restart the program and include more than 1 command line argument. If you are unsure of what command line arguments are avalible, type '-help' after the 'a.out' statement. Otherwise, please refer to the help document\n");
            return 1;
    }
    I think that's your error, because you're defining a block between { ... } and the 'if' is only effective for the printf statement. The 'return 1' will always be executed, thus your program will do nothing!!

    I think what you mean is this....

    Code:
    //detecting the number of Command Line Arguments
    if (argc < 2)
    {
    	printf("There are not enough command line arguments. Please restart the program and include more than 1 command line argument. If you are unsure of what command line arguments are avalible, type '-help' after the 'a.out' statement. Otherwise, please refer to the help document\n");
    	return 1;
    }
    Some of your indentation isn't very nice again; this is the root of a lot of common problems such as this one in C.

    And if anyone can tell me how to enter a TAB character without having to type it into Notepad and then copy and paste it into here I'd be greeatful

    EDIT: Here's another error:

    Code:
    	if (strcmp(argv[i],"-min")==0)
    	{
    		i=0;
    		minimum=2147482648;
    		if (data[i]<minimum);
    		minimum=data[i];
    		printf("%s","The minimum of the values supplied is; %lf, &minimum");
    	}
    The semicolon after the 'if' means that the statement will do nothing!!!

    You seem to enjoy defining unnecessary blocks, as in the following:

    Code:
    if (strcmp(argv[i],"-average")==0)
    {
    	i=0;
    	average=0;
    	{
    		for (i=0;i<endvalue;i++)
    			average=average+data[i];
    	}
    	average=average/endvalue;
    	printf("%s","The average of the values supplied is; %lf, &average");
    }
    This can be simplifed down ....

    Code:
    if (strcmp(argv[i],"-average")==0)
    {
    	i=0;
    	average=0;
    	for (i=0;i<endvalue;i++)
    		average=average+data[i];
    	average=average/endvalue;
    	printf("%s","The average of the values supplied is; %lf, &average");
    }
    I've made no attempt here to determine whether the effect achieved is the effect desired, but I think you need to work on your indentation and code style a bit. Then you might find it comes clearer to you.
    Last edited by Driver; 07-25-2006 at 06:34 AM.
    I think you can put a signature here.

  8. #8
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    Code:
    printf("%s","The number of values greater than the average is; %i, &j")
    Still having trouble with printf.
    It should be this:
    Code:
    printf("The number of values greater than the average is: %i", j);
    Remember that &j is the address of j, if j is at address 0x025b8a6c and has the value 22 then you want to say the value greater than the average is 22, not 39,553,644. You have unnecessary %s in all your printfs and none of your values will actually be displayed because you are putting &j, &average inside a string (also when they do display you won't be seeing the thing you want, not the addresses).

  9. #9
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Although it seems wrong to post the code again, I believe that having the code
    formatted correctly will be a big help to you. The format is my own style that I
    use when coding, with an emphasis on effectively using space. You can of
    course develop you own format, but for the moment you can use this. The code
    below is the latest incarnation of your code, with a few modifications as follows:

    1. Unnecessary braces have been removed, and some cases where braces
    were inappropriately placed have been remedied.
    2. Comments were removed, since they were ineffective.
    3. Your really long error message for too few arguments was shortened.
    4. All printfs have been layed out more correctly, removing & in parameters to
    be passed to printf.
    5. stdlib.h was included, this is needed to use atoi.
    6. Initialised all variables - this is good practice as it prevents a variable being
    used without a value.

    These modifications are fairly trivial, but because your code was in such a state,
    it took about 20 minutes to bring it to this point where your more serious coding
    errors can be identified and fixed. Here is the code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(int argc, char **argv)
    {
    	int i = 0;
    	int j = 0;
    	int data [i];
    	double maximum = 0.0;
    	double minimum = 0.0;
    	double average = 0.0;
    	double value = 0.0;
    	int endvalue = 0;
    	int counter = 0;
    	double gtvalue = 0.0;
    
    	if (argc < 2)
    	{
    		printf ("There are not enough command line arguments.\n");
    		return 1;
    	}
    
    	for (i = 0; i < argc; i++)
    	{
    		if (strcmp (argv [i], "-help") == 0)
    		{
    			printf ("The following Command Line Arguments are avalible for use with this program;");
    			printf ("'-max' will find the maximum value of the supplied number.");
    			printf ("'-min' will find the minimum value of the supplied numbers.");
    			printf ("'-average' will find the average of the set of supplied numbers.");
    			printf ("'-gta' will find the number of values greater than the average.");
    			printf ("'-gt' will find the number of values greater than the number specified after it in the format '-gt XXX' where XXX is your number.");
    			return 1;
    		}
    	}
    
    	if (feof (NULL) != 0)
    	{
    		endvalue = counter;
    		return 1;
    	}
    
    	else
    	{
    		counter = counter + 1;
    	}
    
    	for (i = 0; i < argc; i++)
    	{
    		if (strcmp (argv[i], "-max") == 0)
    		{
    			i = 0;
    			maximum = -2147482648;
    			for (i = 0; i < endvalue; i++)
    				if (data [i] > maximum)
    					maximum = data[i];
    
    			printf("The maximum of the values supplied is; %lf", maximum);
    		}
    
    		if (strcmp (argv[i], "-min") == 0)
    		{
    			i = 0;
    			minimum = 2147482648;
    			if (data [i] < minimum)
    				minimum = data [i];
    
    			printf ("The minimum of the values supplied is; %lf", minimum);
    		}
    
    		if (strcmp (argv [i], "-average") == 0)
    		{
    			i = 0;
    			average = 0;
    
    			for (i = 0; i < endvalue; i++)
    				average = average + data [i];
    
    			average = average/endvalue;
    			printf ("The average of the values supplied is; %lf", average);
    		}
    
    		if (strcmp (argv[i], "-gta") == 0)
    		{
    			i = 0;
    			average = 0;
    			
    			for (i = 0; i < endvalue; i++)
    				average = average + data [i];
    
    			average = average/endvalue;
    			i = 0;
    			j = 0;
    			
    			for (i = 0; i < endvalue; i++)
    				if (data [i] > average)
    					j = j + 1;
    
    			printf ("The number of values greater than the average is; %i", j);
    		}
    
    		if (strcmp (argv [i], "-gt") == 0)
    		{
    			value = atoi (argv [i]);
    			i = 0;
    			j = 0;
    
    			for (i = 0; i < endvalue; i++)
    				if (data [i] > gtvalue)
    					j = j + 1;
    
    			printf ("The number of values greater than the specified value is; %i", j);
    		}
    	}
    }
    Now for the help: My work so far is probably the most helpful thing you'll get here,
    at least from me that is. That is because now you have some code which people
    won't run from when they see it, and will instead take the time to look at what's
    wrong as opposed to the horrible formatting.

    There are still a lot of errors in your code, and I'm surprised your compiler isn't
    complaining more about the code you posted, which compiler are you using might
    I ask?

    I'm not going to fix errors for you, but instead try and help you figure them out for
    yourself, and so I've composed a few "intelligent" questions that might help you
    find answers:

    1. What's going on here? What is it that you are attempting to achieve with this
    code?

    Code:
    if (feof (NULL) != 0)
    	{
    		endvalue = counter;
    		return 1;
    	}
    
    	else
    	{
    		counter = counter + 1;
    	}
    2.

    int i = 0;
    int j = 0;
    int data [i];

    you were already told that the array declaration is a bit dodgy. Now, assuming that
    your compiler actually allows this statement, why would you declare an array to hold
    0 elements?

    3. What happens if a user enters more than one valid command, eg -max 42 456 2 -min ...?

    4. The values entered are in argv, which is a char **, now where in your program do
    you access these values, better still, where do you convert them into their correct types?

    5.

    maximum = -2147482648;
    minimum = 2147482648;

    can you guarantee that maximum and minimum can store values that big? Can you
    think of a better way to find the maximum/minimum of a set of numbers?

    You should also look at all the occurances of "data [i]" in your code - as established
    already there is no information in this variable, but your code assumes otherwise

    >>If you get 30% on the assignment, that is what you deserve

    You should be so lucky: I wouldn't give this more than 15 - 20% because you made
    some really serious logical mistakes.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  10. #10
    Registered User
    Join Date
    Jul 2006
    Posts
    7
    There are still a lot of errors in your code, and I'm surprised your compiler isn't
    complaining more about the code you posted, which compiler are you using might
    I ask?

    I'm not going to fix errors for you, but instead try and help you figure them out for
    yourself, and so I've composed a few "intelligent" questions that might help you
    find answers:

    1. What's going on here? What is it that you are attempting to achieve with this
    code?

    Code:
    if (feof (NULL) != 0)
    	{
    		endvalue = counter;
    		return 1;
    	}
    
    	else
    	{
    		counter = counter + 1;
    	}
    2.

    int i = 0;
    int j = 0;
    int data [i];

    you were already told that the array declaration is a bit dodgy. Now, assuming that
    your compiler actually allows this statement, why would you declare an array to hold
    0 elements?

    3. What happens if a user enters more than one valid command, eg -max 42 456 2 -min ...?

    4. The values entered are in argv, which is a char **, now where in your program do
    you access these values, better still, where do you convert them into their correct types?

    5.

    maximum = -2147482648;
    minimum = 2147482648;

    can you guarantee that maximum and minimum can store values that big? Can you
    think of a better way to find the maximum/minimum of a set of numbers?

    You should also look at all the occurances of "data [i]" in your code - as established
    already there is no information in this variable, but your code assumes otherwise
    1. I should've updated the code, sorry. Anyway, what im trying to achieve by this section of code is establish the end of a file that is used as input into the program. It's meant to replace input from the keyboard and read straight from the file(apparently this is possible as part of the stdin file). The counter function is used to establish the "endvalue" for use later in the program. This also doubles in use by making the array increase until the end of file is reached. The do statement scans the current value while the NULL value isnt detected and inputs that into the array at the current value.
    Code:
     do
        {
                    scanf("%lf", &value);
        }
             while (feof(NULL) == 0);
    
            if (feof(NULL) != 0)
            {
                    endvalue = i;
                    return 1;
            }
            else
            {
                    data[i]=value;
                    i++;
            }
    <cue segmentation fault> Not too sure where thats coming from, reasonably certain that im telling it to read something but its not there, not quite sure how to fix it though. The idea is to read from a file by using a "<", in this case "</usr/units/ep100/ass_sup.061/test_data". I know im missing a line of code pointing to which file to use, would it be
    Code:
    scanf(FILE *, &value)
    ?? or something along those lines? Everything else compliles with no errors or warnings, unbelieveable but true. The whole read code is above.

    2. Re-arranged the array declaration so that it doesnt become set at 0 and increases dynamically(see 1)

    3. I'm not sure what your getting at here but i presume its multiple uses of the same counter? I inserted more code so that all counters and re-initialised when starting a new "if" statement

    4. Again, outdated code doesn't help. All these are converted by using the "atoi" function and then implemented.

    5. Re-set max and min to 0. I was tired when I wrote this part of code and so I set it to the highest and lowest possible, it didnt occur to me that all I had to do was set it to zero and if it was lower than zero would be the max anyway.

    Thanks a lot for the formatting tips. Finally, I have absolutly no idea what compliler im using. All I know is that the server is running Fedora 4 as its core.

    Alex
    Last edited by Chickenhawk; 07-26-2006 at 04:50 AM.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    123
    Anither thing I notice is that you have a loop variable called i, and you're setting it inside the loop using inner loops. This will also cause the program to fail; have a second loop counter (called j) declared as an int.

    Consider this code here:
    PHP Code:
    if (strcmp (argv[i], "-max") == 0)
    {
        
    0;
        
    maximum = -2147482648;
        for (
    0endvaluei++)
            if (
    data [i] > maximum)
                
    maximum data[i];

        
    printf("The maximum of the values supplied is; %lf"maximum);
    }
    if (
    strcmp (argv[i], "-min") == 0)
    {
        
    0;
        
    minimum 2147482648;
        if (
    data [i] < minimum)
            
    minimum data [i];

        
    printf ("The minimum of the values supplied is; %lf"minimum);

    How about changing it to something such as the following?

    PHP Code:
    if (strcmp (argv[i], "-max") == 0)
    {
        
    1;
        
    maximum data[0];
        while (
    endvalue)
        {
            if (
    data [j] > maximum)
                
    maximum data[j];
            
    ++;
        }
        
    printf("The maximum of the values supplied is; %lf"maximum);
    }

    if (
    strcmp (argv[i], "-min") == 0)
    {
        
    1;
        
    minimum data[0];
        while (
    endvalue)
        {
            if (
    data [j] < minimum)
                
    minimum data [j];
            
    ++;
        }
        
    printf ("The minimum of the values supplied is; %lf"minimum);

    Now, we have 'j' as an inner loop count instead of re-using 'i'.
    For you minimum value detection, you used a simple 'if' and not a loop. Was that correct? Why did you do that?

    Also, we're not assmung ANY hardcoded values for minimum and maximum now. Instead, we're assuming the maximum and minimum will be the first data item, and then iterate through the loop to determine which really is the lowest and the highest.

    Question for you: Can you combine those into a single block of code?

    And yes ... I used PHP tags.
    I think you can put a signature here.

  12. #12
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Angoid's approach is what I was elluding to when I said that you should find
    a way that doesn't need a hard coded value.

    Anyway, back to the more serious matters:

    1. Does your assignment say to redirect stdin, or just read from a file? Use
    fscanf to read from a file, which will have to be opened previously with fopen.
    Read the following:

    http://www.cprogramming.com/tutorial/cfileio.html
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    basically, open the file with fopen, read the file using fscanf and don't use feof
    to control the loop, when fscanf fails (assuming the numbers in the file can be
    successfully be read in), it's at the end of the file.

    2. Arrays do not grow automagically - this is C, not voodoo. Whether you were
    told this, or you assumed it, it is very very wrong. Read up on dynamic memory
    allocation, i.e malloc, calloc, realloc and free.

    3. As a general design rule when using command line arguments, allow a certain
    location in argv to represent the openmode, and allow all remaining values to
    be data.

    4. atoi is buggy as it has very little by way of error handling - are you sure all
    values are integers? maybe strtod would be more suitable.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  13. #13
    Registered User
    Join Date
    Jul 2006
    Posts
    7
    1.My assignment asks me to redirect the input from a file, not actually open the file and then read. From what I've been told by my tutour, it's exactly the same as normal input except that it doesn't require key-strokes to put it in. This is where I'm getting my segmentation error. Unless I've missed something, that should work, but it isnt.

    2, ok, I'll reset the array to a permanant 1000 variable array.

    3. You mean a standard posistion for each CLA? I'll keep that in mind.

    4. Chnaged atoi to atof, seems more practical for the application.

    Alex

  14. #14
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    1.

    Code:
    #include <stdio.h>
    
    int main (void)
    {
    	feof (NULL);
    	return 0;
    }
    Look familiar? It would appear that this isn't allowed - you should be testing
    stdin, not NULL, and as already pointed out, feof is rubbish anyways, so you
    should avoid using it anyways.

    Don't know anything about redirecting stdin, this may even be an OS specific
    task, in which case I'm surprised that you're being asked to do it. Hopefully
    someone else might be able to shed some more light on this. Does your class
    teach you how to do this, or does the information given look more like that from
    the first of the two links I gave in my last post?

    2. Good, that'll work for most situations.

    4. atof is just as bad as atoi as far as I know, but it's up to you.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  15. #15
    Registered User
    Join Date
    Jul 2006
    Posts
    7
    Hi all,
    Well, my assignment is finally near finishing. All previous problems are dealt with, the seg fault is gone as are numerous other warnings. My only problem is that my data isn't being set for further use in the program. This is the code that reads all the data and places it in memory.

    Code:
      for (i=0;i<1000;i++)
                    {
                    printf("");
                    scanf("%lf", &value);
    
                            if (feof(stdin) != 0)
                                    {
                                    return 1;
                                    }
                            else
                                    data[i] = value;
                    }
    I know its not placeing values into the memory properly because when I execute the other commands its giving out no output, even though my process commands it to do so. I've looked through it a lot of times and it isn't working despite my best efforts. It should be noted that the first value the program comes up against is 16.87. This conflicting with my if statement and causing it to stop functioning because its not 0. How do I set it so that it stops functioning if it reaches a NULL?

    Thanks again
    Alex
    Last edited by Chickenhawk; 07-27-2006 at 06:42 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Database assignment is Killing me!
    By Boltrig in forum C Programming
    Replies: 2
    Last Post: 11-29-2007, 02:56 AM
  2. Assignment Help !! (Student information system)
    By ashb in forum C++ Programming
    Replies: 6
    Last Post: 03-12-2005, 04:32 AM
  3. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM
  4. Expression/Declaration Syntax Errors
    By fuh in forum C++ Programming
    Replies: 2
    Last Post: 01-15-2003, 05:49 PM
  5. MFC: Multi-threaded DLL Errors & Related Syntax :: C++
    By kuphryn in forum C++ Programming
    Replies: 3
    Last Post: 02-13-2002, 08:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21