arrays

This is a discussion on arrays within the C Programming forums, part of the General Programming Boards category; I keep getting syntax errors. Code: #include <stdio.h> void main() { int number[10]; /* array of 10 numbers */ int ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    42

    arrays

    I keep getting syntax errors.
    Code:
    #include <stdio.h>
    
    void main()
    {
    	int number[10]; /* array of 10 numbers */
    	int count = 0;  /* count through the amount of times entered */
    	long sum = 0L;  /* The total amount of the numbers entered */
    	float average = 0.0f; /* average of the total / count */
    	int i = 0;			/* count through the array */
    	char newnum;		/* check if user wants to continue */
    
    
    	do				/* do  loop to go through the user input */
    	{
    	printf("\n Enter the numbers you wish to average:\n");	/* enter the numbers display to the user */
    	scanf("%d", &number[i]);								/* read input */
    	printf("Would you like to add more numbers? y or n");	/* check if user want to enter another number */
    	scanf("%c", &newnum);				/* read input */
    
    	if("%newnum == 'Y' || newnum == 'y')
    	{
    		printf("\n Enter the numbers you wish to average:\n");	/* enter new numbers if continue is true */
    		scanf("%d", &number[i]);	/* read new numbers into array */
    	}
    	else
    	
    
    	for(i = 0; i < count; i++)			/* else display the value of the numbers entered into the array */
    	{
    		printf("%d", i+1);			/* display the number of the array plus one to diplay sequenceto user */
    		scanf("The value you entered was %d", &number[i]); /* read numbers input into the array */
    		sum += number[i];	/* sum total of numbers inthe array */
    	}
    	average = (float)sum / count;		/* average of sum of numbers divided by count of numbers */
    	
    	for(i = 0; i < count; i++)	
    	{
    	printf("\nThe Number %d was %d", i + 1, number[i]);	/* display the number of the line and the corresponding value in the array */
    	printf("\n Average of the numbers entered is : %f\n", average); /* display the average of all numbers entered in the array */
    	printf("Would you like to continue? Y/N\n"); /*check if the user would like to enter a new set of numbers into the array */
    	scanf(" %c", &newnum); /* read input */
    	}
    	}while("%newnum == 'Y' || newnum == 'y'); /* if the user wants to continue, continue */
    
    	
    
    }

    CODE TAGS added by Hammer
    Last edited by john_murphy69; 02-11-2003 at 01:49 PM.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Please have a read of this, then edit your post accordingly.

    Thanks
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    Is that better?

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    John still no, please add the code tags in your code.

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Now, let's talk about your code

    >>void main()
    Never use void main(). You should use instead int main() and than return an value at the end of the program.

    >>if("%newnum == 'Y' || newnum == 'y')
    Try looking at the block in bold, and tell me by yourself what's the error

    >>while("%newnum == 'Y' || newnum == 'y');
    Again the same error? Remove the bold part, you dont need ", neither %

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    I don't understand

  7. #7
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    john_murphy69:
    Look here:
    Code:
    if("%newnum == 'Y' || newnum == 'y')
    You're trying to see if the char is 'y' or 'Y'.
    But the syntax isn't like that, the right syntax is:
    Code:
    if (newnum == 'Y' || newnum == 'y')
    You understand the difference?

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    OK that got rid of the syntax errors.
    Thanks, Now I have to fix my code because it is still not doing exactly what I want it too. Thanks again and for your quick response.

  9. #9
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    No problem
    Next time, just don't forget to add the code tags, your code will be more readable and more people will want to help you.

    Enjoy coding.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM

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