C Programming Guru's please help (Simple C Programming)

96redformula

2[H]4U
Joined
Oct 29, 2005
Messages
2,578
Here is my programming that I am having trouble with. I am using arrays to
fill in the data entered by the user and give the statistics of the data entered.
I need help as I am either totally wrong or I am not understanding how to do the syntax right somewhere. Please guide me in the right direction or tell me what I am missing here.

First Error I get when trying to compile it is
Error int main()

Following errors
Cannot compare a pointer to an integer (this is refering to the if statements I have at the bottom)






________________________________________________________________

#include <stdio.h>


int main()
{

int index = 0;
char name_a[index][10];
char gender_a[index][10];
int age_a[index][10];
int f_count = 0;
int m_count = 0;
int i = 0;
int to9 = 0;
int to19 = 0;
int to29 = 0;
int to39 = 0;
int to49 = 0;
int to59 = 0;
int to69 = 0;



while ( i == 0 )
{
printf("Please enter the First name: \n ");
scanf( "%s", &name_a[index] );
printf("Please enter the Gender as (M or F): \n ");
scanf( "%s", &gender_a[index] );
printf("Please enter the Age: \n ");
scanf( "%d" , &age_a[index] );

printf("First Name: %s \n ", name_a[index] );
printf("Gender is : %s \n ", gender_a[index] );
printf("Age is : %d \n " , age_a[index] );

If ((gender_a[index] == 'F' || gender_a[index] == 'f'))
++f_count;

If (gender_a[index] == 'M' || gender_a[index] == 'm')
++m_count;

If ( age_a[index] >=0 && age_a[index] <10 )
++to9;
If ( age_a[index] >=10 && age_a[index] <20 )
++to19;
If ( age_a[index] >=20 && age_a[index] <30 )
++to29;
If ( age_a[index] >=30 && age_a[index] <40 )
++to39;
If ( age_a[index] >=40 && age_a[index] <50 )
++to49;
If ( age_a[index] >=50 && age_a[index] <60 )
++to59;
If ( age_a[index] >=60 && age_a[index] <70 )
++to69;


printf( "%d members that are Female", f_count );
printf( "%d members that are Male", m_count );
printf( "%d members in 0 - 9 group", to9 );
printf( "%d members in 10 - 19 group", to19 );
printf( "%d members in 20 - 29 group", to29 );
printf( "%d members in 30 - 39 group", to39 );
printf( "%d members in 40 - 49 group", to49 );
printf( "%d members in 50 - 59 group", to59 );
printf( "%d members in 60 - 69 group", to69 );



++index;
printf( "\n To Continue with list please press 0:\n To Quit please press 1:");
scanf("%d" , &i );
}


}
 
It looks like you are accessing a 2-Dimensional array like it is a 1-Dimensional array.
 
Your array declarations don't make any sense in the first place. The array bounds given in the declaration must be a constant; you've supplied a non-constant expression. I can't figure out why "index" is involved in your declarations, so I can't directly recommend what to do, but I don't think you mean for those to be two-dimension arrays.

By the way, you'll want to post with
Code:
 tags around your code to preserve formatting. And posting the complete error message, exactly as the compiler provides it, is never a bad idea.

The control expression of your loop doesn't look right, either; won't end in any reasonable time, and you're not indexing the arrays correctly.
 
First, make some defines at the top, like:

Code:
#define MAX_ENTRIES 10
#define STR_LEN 256

Your string declarations should be something like:
Code:
 char name_a[MAX_ENTRIES][STR_LEN];

You should decide if you are using a single character or a string to store the gender. First you use a "&#37;s" scanf formatspecifier, but you try to access it as a character array later on. If you want to allow string entry but only look at the first char, access it as "gender_a[index][0]".

Make sure you check to see if an invalid gender if entered, if you have exceeded MAX_ENTRIES, etc. Error checking like this will get you bonus points or might be required for a full score (I assume this is a school project). Also, it is something you have to do if you ever write code for something "in real life". Example:
Code:
while(i == 0)
becomes
Code:
while(i == 0 && index < MAX_ENTRIES)

Your If statements won't compile because they have the "I" capitalized.


Not errors, just suggestions:

You could easily replace the toXX variables with an array and use something like age_bucket[age_a[index] / 10] to avoid the if..if..if..if..if stuff you have now. Make sure you range-check the age input before using something like that.

The stats print statements have no newlines.

I wouldn't use the variable 'i' for the flag signalling no more entries, instead use a more descriptive name like exit_flag. i is usually used as a loop Index

I modified your program to compile and fixed the errors I listed above and it seems to work fine. Good luck :)
 
I am now getting these errors:
knoppix@1[CPrograms]$ cc increment.c
increment.c: In function 'main':
increment.c:42: error: syntax error before 'f_count'
increment.c:45: error: invalid lvalue in increment
increment.c:45: error: syntax error before 'm_count'
increment.c:48: error: invalid lvalue in increment
increment.c:48: error: syntax error before 'to9'
increment.c:50: error: invalid lvalue in increment
increment.c:50: error: syntax error before 'to19'
increment.c:52: error: invalid lvalue in increment
increment.c:52: error: syntax error before 'to29'
increment.c:54: error: invalid lvalue in increment
increment.c:54: error: syntax error before 'to39'
increment.c:56: error: invalid lvalue in increment
increment.c:56: error: syntax error before 'to49'
increment.c:58: error: invalid lvalue in increment
increment.c:58: error: syntax error before 'to59'
increment.c:60: error: invalid lvalue in increment
increment.c:60: error: syntax error before 'to69'


#include <stdio.h>

#define max_entries 10
#define str_len 10

int main()
{

int index = 0;
char name_a[max_entries][str_len];
char gender_a;
int age_a[max_entries][str_len];
int f_count = 0;
int m_count = 0;
int exit_loop = 0;
int to9 = 0, to19 = 0, to29 = 0, to39 = 0, to49 = 0, to59 = 0, to69 = 0;



while (exit_loop == 0 && index < max_entries)
{
printf("Please enter the First name: \n ");
scanf( "&#37;s", &name_a[max_entries][str_len] );
printf("Please enter the Gender as (M or F): \n ");
scanf( "%c", &gender_a );
printf("Please enter the Age: \n ");
scanf( "%d" , &age_a[max_entries][str_len] );

printf("First Name: %s \n ", name_a[max_entries][str_len] );
printf("Gender : %c \n ", gender_a );
printf("Age is : %d \n " , age_a[max_entries][str_len] );

If ((gender_a == 'F' || gender_a == 'f'))
f_count++;

If (gender_a == 'M' || gender_a == 'm')
++m_count;

If ( age_a[max_entries][str_len] >=0 && age_a[max_entries][str_len] <10 )
++to9;
If ( age_a[max_entries][str_len] >=10 && age_a[max_entries][str_len] <20 )
++to19;
If ( age_a[max_entries][str_len] >=20 && age_a[max_entries][str_len] <30 )
++to29;
If ( age_a[max_entries][str_len] >=30 && age_a[max_entries][str_len] <40 )
++to39;
If ( age_a[max_entries][str_len] >=40 && age_a[max_entries][str_len] <50 )
++to49;
If ( age_a[max_entries][str_len] >=50 && age_a[max_entries][str_len] <60 )
++to59;
If ( age_a[max_entries][str_len] >=60 && age_a[max_entries][str_len] <70 )
++to69;


printf( "%d members that are Female", f_count );
printf( "%d members that are Male", m_count );
printf( "%d members in 0 - 9 group", to9 );
printf( "%d members in 10 - 19 group", to19 );
printf( "%d members in 20 - 29 group", to29 );
printf( "%d members in 30 - 39 group", to39 );
printf( "%d members in 40 - 49 group", to49 );
printf( "%d members in 50 - 59 group", to59 );
printf( "%d members in 60 - 69 group", to69 );



++index;
printf( "\n To Continue with list please press 0:\n To Quit please press 1:");
scanf("%d" , &exit_loop );
}


}
 
Back
Top