First off, how can I implement a #define in this code? I'm fairly lost when it comes to defining and calling of that #define. Also, if there are any ways to make this code look better it would be greatly appreciated! I'm new to coding so any help would be awsome. This is technically homework but I met all the requirements for the assignment so now this is simply exploratory.
#include <stdio.h>
#include <math.h>
int main() {
int studentID;
int count= 0;
char course1[10]={};//issue with storing user input, used an array
char course2[10]={};
float coursecost1;
float coursecost2;
float credithour = 120.25;
float healthID=35.00;
float totalcost;
// Asks for student ID
printf("Enter the Students Id: \n");
scanf("%d",&studentID);
// Enter CRN/Credit hrs for first course
printf("Enter crn/credit hours for the first course: \n");
scanf("%s", &course1);
// Enter CRN/Credit hrs for Second Course
printf("Enter crn/credit hours for the second course: \n");
scanf("%s", &course2);
// Closing statement
printf("\nThank you!\nPRESS ANY KEY TO CONTINUE...\n\n");
// Calculates Cost of Class
int i = course1[5]-'0'; // Bad version of type casting from char to int.
int k = course2[5]-'0'; // Bad version of type casting from char to int.
coursecost1 = i*credithour;
coursecost2 = k*credithour;
totalcost= healthID+coursecost1+coursecost2;
// Printout
printf("\t\tVALENCE COMMUNITY COLLEGE\n\t\tORLANDO FL 10101\n\t\t");
do{
count++;
printf("*");
}while(count<26);
printf("\n\t\tFee Invoice Prepared for Student V%d\n",studentID);
printf("\t\t1 Credit Hour = %.2f\n",credithour);
printf("\t\tCRN\t CREDIT HOURS\n");
printf("\t\t%c%c%c%c\t %c\t\t $
%.2f\n",course1[0],course1[1],course1[2],course1[3],course1[5],coursecost1);
printf("\t\t%c%c%c%c\t %c\t\t $
%.2f\n",course2[0],course2[1],course2[2],course2[3],course2[5],coursecost2);
printf("\t\t\t Health & id fees $ %.2f\n",healthID);
printf("\t\t");
count=0;
do{ //could define a function to clean up code
count++;
printf("-");
}while (count < 39);
printf("\n\t\t\t Total Payments $ %.2f",totalcost);
return 0;
"define" is basically creating a shortcut for stuff you write often in your code, or stuff you want to change often in order to test your code.
An example:
#define fail printf("You did something wrong")
int main (void)
{
int number;
printf("Insert a number between 1 and 3");
scanf("%d", &number);
if (number>3||number<1)
{
fail;
return -1
}
else
{
printf("Insert a number between 4 and 6");
scanf("%d", &number);
if (number<4||number>6)
{
fail;
return -1;
}
else return 0;
}
}
When this program is read by the compiler, every "fail" gets substituted by "printf("you did something wrong").
This is the main use of define, and if you are beginning now, that's what I'd consider important.
About your code, when you do scanf for strings, don't use "&", or use the "gets" function. I could try to explain why to you, but I'm not the best person for this job. My tip is, keep studying and when you start seeing pointers, you'll understand better why this happens.
Also, when you use arrays without a starting value, you don't need to do the "={ }" thing, just call them without "=".
Now, as you asked, this is how I'd write your code, with the corrections done:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int studentID, intcount = 0; // Count is a function, better not mix things up, that's why I called it "intcount"
char course1[10], course2[10];
float coursecost1, coursecost2, totalcost;
float credithour = 120.25;
float healthID=35.00;
// Asks for student ID
printf("Enter the Students Id: \n");
scanf("%d",&studentID);
// Enter CRN/Credit hrs for first course
printf("Enter crn/credit hours for the first course: \n");
scanf("%s", course1);
// Enter CRN/Credit hrs for Second Course
printf("Enter crn/credit hours for the second course: \n");
scanf("%s", course2);
// Closing statement
// printf("\nThank you!\nPRESS ANY KEY TO CONTINUE...\n\n");
/* Decided not to delete this one, so you can
compare this printf with the new one and the system pause */
printf("\nThank you!\n");
system("pause");
// Calculates Cost of Class
// I'm not sure what you were trying to do in these lines, so I won't touch them
int i = course1[5]-'0';
int k = course2[5]-'0';
coursecost1 = i*credithour;
coursecost2 = k*credithour;
totalcost= healthID+coursecost1+coursecost2;
// Printout
printf("\t\tVALENCE COMMUNITY COLLEGE\n\t\tORLANDO FL 10101\n\t\t");
do
{
intcount++;
printf("*");
}
while(intcount<26);
printf("\n\t\tFee Invoice Prepared for Student V%d\n",studentID);
printf("\t\t1 Credit Hour = %.2f\n",credithour);
printf("\t\tCRN\t CREDIT HOURS\n");
printf("\t\t%c%c%c%c\t %c\t\t $%.2f\n",course1[0],course1[1],course1[2],course1[3],course1[5],coursecost1);
printf("\t\t%c%c%c%c\t %c\t\t $%.2f\n",course2[0],course2[1],course2[2],course2[3],course2[5],coursecost2);
printf("\t\t\t Health & id fees $ %.2f\n",healthID);
printf("\t\t");
intcount=0;
do
{ //could define a function to clean up code
intcount++;
printf("-");
}
while (intcount < 39);
printf("\n\t\t\t Total Payments $ %.2f",totalcost);
}
Basically just try to avoid placing too much information on the same line, even split single printfs into multiple ones if neccessary.
Hope I could be of some use!
Related
#include <stdio.h>
#include <stdlib.h>
struct student
{
char name[30];
char dob[30];
int rollno;
float percent;
int sub[3];
int total;
};
int main(void)
{
int i, n,a,c,*ptr;
char ch;
struct student *st;
printf("Enter number of students data you want to enter:\n");
scanf("%d",&n);
st=(struct student*) malloc(n * sizeof(struct student));
if(st == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
for(i=1;i <= n;i++)
{
printf("Enter name of student %d\n",(i));
scanf("%s",&(st+i)->name);
printf("Enter Roll No of student %d\n",(i));
scanf("%d",&(st+i)->rollno);
printf("Enter Date Of Birth of student %d\n",(i));
scanf("%s",&(st+i)->dob);
printf("Enter marks for 3 subjects of student %d\n",(i));
scanf("%d %d %d",&(st+i)->sub[0],&(st+i)->sub[1],&(st+i)->sub[2]);
(st+i)->total = ((st+i)->sub[0]+(st+i)->sub[1]+(st+i)->sub[2]);
printf("Total Marks of student %d = %d\n\n",(i), (st+i)->total);
}
printf("\n");
printf("\n<1>To display details of students\n");
printf("\n<2>exit\n");
printf("Enter your choice:\n");
scanf("%c",&ch);
switch(ch)
{
case '1':
{
for(i=1; i <= n;i++)
{
printf("\n%d.%s",(st+i)->rollno,(st+i)->name);
}
printf("\n Enter Roll no to display info of student");
scanf("%d",&a);
{
c=a;
a=i;
i=c;
if((st+i)->sub[0]<33||(st+i)->sub[1]<33||(st+i)->sub[2]<33)
{
printf("\nName of student: %s",(st+i)->name);
printf("\nRoll No of student: %d",(st+i)->rollno);
printf("\nDate of Birth : %s",(st+i)->dob);
printf("\nTotal of student: %d",(st+i)->total);
printf("\nStudent Status fail");
return 0;
}
printf("\nName of student: %s",(st+i)->name);
printf("\nRoll No of student: %d",(st+i)->rollno);
printf("\nDate of Birth : %s",(st+i)->dob);
printf("\nTotal of student: %d",(st+i)->total);
(st+i)->percent=((st+i)->total)/3;
printf("\nPercent of Student = %f",(st+i)->percent);
if((st+i)->percent>=33)
{
printf("\nStudent status:- PASS\n");
}
else
printf("\nStudent status:-FAIL\n");
if((st+i)->percent>=90)
printf("Grade= A1\n");
else if (80<=(st+i)->percent)
printf("Grade= A2\n");
else if(70<=(st+i)->percent)
printf("Grade= B1\n");
else if(60<=(st+i)->percent)
printf("Grade= B2\n");
else if(50<=(st+i)->percent)
printf("Grade= C1\n");
else if(40<=(st+i)->percent)
printf("Grade= C2\n");
else if(33<=(st+i)->percent)
printf("Grade= D\n");
else if((st+i)->percent<33)
printf("Grade= F\n");
}
break;
}
case '2':
{
return 0;
}
default:
printf("Invalid! Try again...\n");
}
free(st);
return 0;
}
I want my program to take input of various details of students and display them when I enter the roll no. of student. However,
the switch statement is not executing; the program just exits after taking input.
I checked syntax, but it's correct I don't know what the issue is.
If I could get a hint about the issue it would be great.
The problem has nothing to do with the switch statement, but with writing to and reading from to correct memory locations.
In C, arrays start at index 0. Therefore, the first for-loop tries to access memory outside of what is allocated. The idiomatic solution is to start i at 0 and loop while it is strictly less than the size of the array.
Pointer arithmetic not needed here for accessing the array members, and is better replaced by the index operators ([]). More importantly, please have careful look at the types scanf expects and the actual types of the parameters. The name and dob variables are already of the type char* (or char[30] with pointer decay), so they do not need an additional "address of" operator (&). Viewing the compiler warnings helps in catching these kind of errors.
Here is the code with the suggested improvements:
for(i=0;i < n;i++)
{
printf("Enter name of student %d\n",(i));
scanf("%s",st[i].name);
printf("Enter Roll No of student %d\n",(i));
scanf("%d",&st[i].rollno);
printf("Enter Date Of Birth of student %d\n",(i));
scanf("%s",st[i].dob);
// ...
With this resolved, the code now skips past the switch menu because there already is a newline character (\n) in the input which is read by scanf. To skip over leading whitespace (which includes newline characters), add a space character ( ) before the character conversion specifier:
scanf(" %c",&ch);
Inside the switch statement, apply the same fix as earlier to the for-loop:
for(i=0; i < n;i++)
What follows are more invalid reads, probably caused by c=a; a=i; i=c; and I am not sure what the idea behind this is.
Instead of fixing the entire program, I hope that the advice above will help you on your way to resolving the other issues yourself. It may help to write short isolated code snippets first and fully test these (e.g. by writing to certain memory and then reading from it) and afterwards incorporating these snippets into a larger program.
Also, compiling with debug info (the -g flag) and running the program with Valgrind may help you track down where the bugs come from.
Lastly, by careful of using scanf to read strings. Currently, it may cause buffer overflows if the input string is longer than the size of the array written to. Use, e.g., scanf("%29s",st[i].name); to limit the maximum number of characters scanf will read. There is more to reading input safely and predictably than I can write here, so I encourage you to look around on Stack Overflow or elsewhere yourself.
use getchar() instead of scanf(). This is because scanf leaves the newline you type in the input stream. Try
do
ch = getchar();
while (isspace(ch));
I am a newbie in coding and preparing an assignment in which I am encountering certain difficulties. I just want you guys to help me identify my mistake for one issue I am facing. Before pasting my code I am giving an overview of what had to be done.
The user have to create drones and after creating one drone the program should return back to the main menu, store the details of the 1st drone and if request to enter another drone he/she should be allowed to do so until the number of drones have reached 10. Only 10 drones are allowed and we have to use arrays for storing the values of each drone. I am unable to make the program count 10 and using arrays to store them. In the code I have pasted here, I am trying for 2 but I am unable to do that as well. Please help me...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct drone_t{
char name[30];
float top_s;
float acc;
};
void printing (struct drone_t dron[2]);
int main()
{
int a, i;
char nam;
struct drone_t drone[2];
printf("Welcome to the drone travel time calculator\n");
printf("1. Create Drone\n2. Calculate Time\n3. Exit\n");
scanf("%d", &i);
if (i == 1)
{
for (a=1; a < 3; a++)
{
printf("What is the name of the drone?\n");
scanf("%s", drone[a].name);
printf("What is the top speed of the drone? (kmph)\n");
scanf("%f", &drone[a].top_s);
printf("What is the acceleration of the drone? (mpsps)\n");
scanf("%f", &drone[a].acc);
printing(drone);
}
}
else if (i == 2)
{
printing(drone);
}
}
void printing (struct drone_t dron[2])
{
int a;
for (a=1; a < 3; a++)
{
printf("Name is: %s\n", dron[a].name);
}
//return 0;
}
Anticipated Output is:
Welcome to the drone travel time calculator
1. Create Drone
2. Calculate Time
3. Exit
1
What is the name of the drone?
Jayne
What is the top speed of the drone? (kmph)
12
what is the acceleration of the drone? (mpsps)
12
1. Create Drone
2. Calculate Time
3. Exit
1
What is the name of the drone?
JayneW
What is the top speed of the drone? (kmph)
12
what is the acceleration of the drone? (mpsps)
12
1. Create Drone
2. Calculate Time
3. Exit
2
Select a drone:
1. Jayne
2. JayneW
EDIT:
#coderredoc
Here is the full code.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
struct drone_t{
char name[30];
float top_s;
float acc;
};
void show_menu();
void do_create(int dronesCreated);
#define MAXDRONES 10
int main()
{
int dronesCreated = 0;
int i;
while(1)
{
show_menu();
if (i == 1)
{
if(dronesCreated<=MAXDRONES-1)
{
dronesCreated++;
do_create(dronesCreated);
}
else
{
printf("error: cannot create more drones");
}
}
else
{
break;
}
}
}
void show_menu()
{
int i;
printf("1. Create Drone\n2. Calculate Time\n3. Exit\n");
scanf("%d", &i);
}
void do_create(int dronesCreated)
{
char name[10];
int b, c;
int count = dronesCreated;
struct drone_t drone[10];
for (b=0; b <= count; b++)
{
printf("What is the name of the drone?\n");
scanf("%s", drone[b].name);
printf("What is the top speed of the drone? (kmph)\n");
scanf("%f", &drone[b].top_s);
printf("What is the acceleration of the drone? (mpsps)\n");
scanf("%f", &drone[b].acc);
}
}
As far as I can understand you are stuck in this part.
How to continue getting input?
For that you need to write a while(1){..} with a propr exit condition like break etc.
Rest of the job boils down to how many drones are there? and accessing and storing them.
For storing an array of structure will be sufficient. For count of drone you can use a seperate variable. Accessing them is nothing but array access.
A pseudocode to your guide:
#define MAXDRONES 10
int dronesCreated =0;
while(1){
show _menu();
if( option is DRONE_CREATE){
if(dronesCreated<=MAXDRONES-1){
do_create();//increment NUM_OF_DRONES here.
dronesCreated++;
}
else
print appropriate message.
else if(option is PRINT)
print()
else
break;
}
Look it is simple to understand the design of the program from intuitive viewpoint.
You need a loop because you need to continuouskly ask for option from user. That's why loop.
How to stop at 10 drones?
You keep a variable and check when DRONE_CREATE option is given whether 10 drones are created. If yes then don't call the function else do call the function.
Implementation centric approach:
Now as you are using the global array of structures for the drones, you need to use a count varible like this.
int numberOfDrones = 0; And then use the control as shown above.
How to print and scan the drones?
Simply the way you would input and output them.
scanf("%s",drone[a].name);
print("%s,%d",drone[a].name,drone[a].age);
All at one time:
for(int i=0;i<MAXDRONES;i++){
printf("What is the name of the drone?\n");
scanf("%s", drone[a].name);
printf("What is the top speed of the drone? (kmph)\n");
scanf("%f", &drone[a].top_s);
printf("What is the acceleration of the drone? (mpsps)\n");
scanf("%f", &drone[a].acc);
printing(drone);
}
You have a fundamental error that catches many people new to C-like languages:
Array subscripts in C run from zero, i.e. the first element of array drone would be "drone[0]".
This means that when your code executes the main() for loop for the second time it will be writing the data outside the bounds of the array. This results in undefined behaviour. The quick fix is to change the for loop to: for (a=0; a < 2; a++) in both main() and printing().
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DRONES 10
struct drone_t{
char name[30];
float top_s;
float acc;
};
void printing (struct drone_t dron[]); //no need to mention size while prototyping
int main()
{
unsigned short cnt,choice;
//char nam; //uneccessary variable
struct drone_t drone[DRONES];
printf("Welcome to the drone travel time calculator\n");
do{
printf("1. Create Drone\n2. Calculate Time\n3. Exit\n:\t");
scanf("%d", &choice);
switch(choice)
{
case 1:
for (cnt=0; cnt < DRONES; cnt++)
{
printf("What is the name of the drone?\n");
scanf("%s",drone[cnt].name);
printf("What is the top speed of the drone?(kmph)\n");
scanf("%f", &drone[cnt].top_s);
printf("What is the acceleration of the drone?(mpsps)\n");
scanf("%f", &drone[cnt].acc);
}
printing(drone);//put this statement outside loop
break;
case 2:
printing(drone);
break;
case 3:
exit(EXIT_SUCCESS);
default:
printf("choose correct option :D");
break;
}
}while(choice!=3); //if explicitly using exit case then not needed
}
void printing (struct drone_t dron[10])
{
unsigned short cnt;
for (cnt=0; cnt < DRONES; cnt++)
{
printf("Name is: %s\n", dron[cnt].name);
}
}
Note: Don't use for(cnt=1;;) it may cause problems like an off-by-one error so better use for(cnt=0;;).
Need help adjusting code to allow for undetermined number of students. Tried to modify with help I received last week, but it seems like I am not doing it correctly.
#include <stdio.h>
int main ()
{
/* variable definition: */
char StudentName[100];
float ExamValue, Sum, Avg;
int students,exams;
// Loop through 5 Students
for (students=0; students <5 ; students++)
{
// reset Sum to 0
Sum =0.0;
printf("Enter Student Name \n");
scanf("%s", StudentName);
// Nested Loop for Exams
for (exams=0; exams < 3; exams++)
{
printf ("Enter exam grade: \n");
scanf("%f", &ExamValue);
Sum += ExamValue;
}
Avg = Sum/3.0;
printf( "Average for %s is %f\n",StudentName,Avg);
}
return 0;
}
Not sure how I need to go about changing the code. Any help will be greatly appreciated
Not sure I understand correctly, I understood you want to loop an undertemined amount of students.
I will assume you want to stop if certain keyword is added as a student name, let's say "quit".
Without changing your structure much, it would look like this.
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int main (){
char StudentName[100];
float ExamValue, Sum, Avg;
int students,exams;
bool in = true;
while (in) {
Sum =0.0;
printf("Enter Student Name \n");
scanf("%s", StudentName);
if( strcmp(StudentName, "Quit") == 0 || strcmp(StudentName, "quit") == 0){
in = false;
break;
}
for (exams=0; exams < 3; exams++){
printf ("Enter exam grade: \n");
scanf("%f", &ExamValue);
Sum += ExamValue;
}
Avg = Sum/3.0;
printf( "Average for %s is %f\n",StudentName,Avg);
}
return 0;
}
I am trying to learn C, and I copied a code which calculates VAT> I modified it to recalculate if the user answers yes and exit if the answer in no. To my surprise it behaves in a strange way in that if the answer is yes, it is supposed to go to the beginning as ask the user to enter the item cost. Instead it expects the cost to entered immediately after y is pressed.
The code below;
/* VAT Calculation*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
float price_with_vat(float cost, float vat, int quant);
main()
{
float cost, vat, full_price;
int quant,count=0;
char answer[2];
char reply[2]={"y"};
x:
count=count+1;
printf("count= %d\n",count);
printf("Please enter the item cost:\n\n");
scanf("%f", &cost);
printf("\nPlease enter the quantity of product:\n");
scanf("%d", &quant);
printf("\nquantity=%d\n",quant);
/* exit(0); */
printf("\nPlease enter the VAT percentage:\n");
scanf("%f", &vat);
printf("\ncost=%6.2f quant=%d vat=%6.2f\n",cost, quant, vat);
/* exit(0); */
full_price = price_with_vat(cost, vat, quant);
printf("The total price is %6.2f\n\n",full_price);
printf("\nDo you want to perform another transaction? (y/n)\n");
scanf("%c\n", &answer);
if(answer==reply)
{
system("cls");
main();
}
else
return 0;
}
float price_with_vat(float cost, float vat, int quant)
i replace the part
if(answer==reply)
{
system("cls");
main();
}
else
with
if(answer==reply)
goto x
I know the goto construct is discouraged in C (and also in Fortran). I have a variant which uses a do-while loop. It behaves the same.
Any ideas why this behaviour?
Zilore Mumba
You can't compare strings with == in C, so this is wrong:
if(answer==reply)
You need to use strcmp():
if (strcmp(answer, reply) == 0)
strcmp() requires both arguments to be null-terminated strings. You're never adding a null terminator to answer; you should initialize it as:
char answer[] = { '\0', '\0' };
Or, instead of using strings for reply and answer, you could declare them as single characters:
char reply = 'y';
char answer;
Then you can use == to compare them.
Too many mistakes in your program.
You have used goto.
In the statement scanf("%c\n", &answer);, %c expects a char but you are passing it a char (*)[2].
You declared char reply[2]={"y"};, which is not a valid C syntax.
However if reply is declared char array then if(answer==reply) is completely wrong.
Conclusion:
Go through a good tutorial on C or read a good book to know about basic syntax of C.
As explained in other comments and answers, there are several issues with the code presented, such as:
1) Use of goto is generally unnecessary, rarely used.
2) Calling main() from within main(). Mistake.
3) Execution flow is not well controlled within main body of program,
resulting in the unexpected behavior you described.
4) Comparison techniques are all wrong. Read up on ==, strcmp(), != etc.
Following is an example of similar code that should perform more predictably, illustrating how to execute a simple dialog with a user, and displaying results. I hope this will help you :)
Note: my environment did not require me to #include <conio.h>, so I removed it. You may have to add it back in for your environment.
/* VAT Calculation*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
float price_with_vat(float cost, float vat, int quant);
int main(void)
{
float cost, vat, full_price;
int quant,count=0;
char answer[2]={""};
float running_total=0.0;
count = 0;
//dialog section of code
while(answer[0] != 'n')
{
//get price, quantity and VAT
printf("Please enter the item cost:");
scanf("%f", &cost);
printf("\nPlease enter the quantity:");
scanf("%d", &quant);
printf("\nPlease enter VAT percentage:");
scanf("%f", &vat);
count++;
//results section
printf("\n\nCost: %6.2f\nQuantity: %d\nVAT percent: %6.2f", cost, quant, vat);
full_price = price_with_vat(cost, vat, quant);
running_total += full_price;
printf("The total price is %6.2f\n\n",full_price);
printf("\nRunning total is currently: %6.2f for %d items.\n",running_total, count);
//request if continue
printf("\nDo you want to perform another transaction? (enter \"y\" or \"n\")\n");
scanf("%s", answer);
}
return 0;
}
float price_with_vat(float cost, float vat, int quant)
{
return cost*((1.0)+vat)*((float)(quant));
}
I tried to search this everywhere, but it's kind of difficult to word, it's most likely a simple fix. Basically when I go through my program that is supposed to compute the average rainfall for a year, it comes out with a very large number, however, I thought it may have been just that I was doing the arithmetic wrong or had a syntax error of some sort, but that was not the case, when I checked the value that the function returned it was the proper value.
#include <stdio.h>
#include <string.h>
void getData(float *, float *);
int main()
{
char state[2], city[81];
float rainFall[12], outputAverage, *pAverage;
printf("Name Here\n");
printf("Please enter the state using a two letter abreviation: ");
gets(state);
printf("Please enter the city : ");
gets(city);
pAverage = &outputAverage;
(getData(rainFall, pAverage));
printf("%.2f", outputAverage);
return (0);
}
void getData(float *rainFall, float *pAverage)
{
int i;
float total;
for (i=0; i<12; i++)
{
printf("Please enter the total rainfall in inches for month %d: ", i+1);
scanf("%f", &rainFall[i]);
total += rainFall[i];
}
*pAverage = total / 12;
}
you need to initialize total
float total = 0.0;
Initialize the total to 0
Why you make it complicated? Why not just
return total / 12 ?
and called it like
outputAverage = getData(rainfall)
This is a classic problem in C programming. You are mixing strings and numbers on the input. You are better off reading the input into a string and then, using sscanf to parse it properly.
You have uninitialized variable total which is taking garbage value, thus you see a very large answer.
changed your main.. have a look and let me know if you have understood what changes i have made?
#include <stdio.h>
#include <string.h>
void getData(float *);
int main(int argc, char*argv[])
{
char state[3]={0}, city[81]={0};
float outputAverage;
printf("Name Here\nPlease enter the state using a two letter abreviation: ");
scanf("%s",state);
printf("Please enter the city : ");
scanf("%s",city);
getData(&outputAverage);
printf("The Average Rainfall recorded for the year is %.2f\n", outputAverage);
return 0;
}
void getData(float *pAverage)
{
int i;
float rainFall[12]={0}, total=0;
for (i=0; i<12; i++)
{
printf("Please enter the total rainfall in inches for month %d: ", i+1);
scanf("%f", &rainFall[i]);
total += rainFall[i];
}
*pAverage = total / 12;
}
However instead of using gets you should use fgets but i forgot how to counter the issue of using simultaneous fgets to read input from the standard input stream.
Also initialize the total variable as you are adding in the loop new values to existing value in that variable which would not necessarily add to zero as the premier element. so it could be any garbage value + loop values.
I understand you are practicing pointer concept so you passed the address of the array of floats to your second function but if the rainfall function is not useful in main, Better to restrict the same where it would be useful