I am getting a seg fault when I try to run this code. The compiler is also giving me odd warnings about how an integer constant is too long for "long" type.
Here is the code:
Declarations
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void printstates(){
printf("Enter the number for the corresponding state:\n\
\n(0)Alabama\n(1)Alaska\n(2)Arizona\n(3)Arkansas\n(4)California\n(5)Colorado\
\n(6)Connecticut\n(7)Delaware\n(8)Florida\n(9)Georgia\n(10)Hawaii\n(11)Idaho\
\n(12)Illinois\n(13)Indiana\n(14)Iowa\n(15)Kansas\n(16)Kentucky\n(17)Louisiana\
\n(18)Maine\n(19)Maryland\n(20)Massachusetts\n(21)Michigan\n(22)Minnesota\
\n(23)Mississippi\n(24)Missouri\n(25)Montana\n(26)Nebraska\n(27)Nevada\n(28)New Hampshire\
\n(29)New Jersey\n(30)New Mexico\n(31)New York\n(32)North Carolina\n(33)North Dakota\
\n(34)Ohio\n(35)Oklahoma\n(36)Oregon\n(37)Pennsylvania\n(38)Rhode Island\n(39)South Carolina\
\n(40)South Dakota\n(41)Tennessee\n(42)Texas\n(43)Utah\n(44)Vermont\n(45)Virginia\n(46)Washington\
\n(47)West Virginia\n(48)Wisconsin\n(49)Wyoming\n\n");
}
void writestate(int statenumber, char **state){
switch (statenumber) {
case 0: *state = "Alabama"; break;
case 1: *state = "Alaska"; break;
case 2: *state = "Arizona"; break;
case 3: *state = "Arkansas"; break;
case 4: *state = "California"; break;
case 5: *state = "Colorado"; break;
case 6: *state = "Connecticut"; break;
case 7: *state = "Delaware"; break;
case 8: *state = "Florida"; break;
case 9: *state = "Georgia"; break;
case 10: *state = "Hawaii"; break;
case 11: *state = "Idaho"; break;
case 12: *state = "Illinois"; break;
case 13: *state = "Indiana"; break;
case 14: *state = "Iowa"; break;
case 15: *state = "Kansas"; break;
case 16: *state = "Kentucky"; break;
case 17: *state = "Louisiana"; break;
case 18: *state = "Maine"; break;
case 19: *state = "Maryland"; break;
case 20: *state = "Massachusetts"; break;
case 21: *state = "Michigan"; break;
case 22: *state = "Minnesota"; break;
case 23: *state = "Mississippi"; break;
case 24: *state = "Missouri"; break;
case 25: *state = "Montana"; break;
case 26: *state = "Nebraska"; break;
case 27: *state = "Nevada"; break;
case 28: *state = "New Hampshire"; break;
case 29: *state = "New Jersey"; break;
case 30: *state = "New Mexico"; break;
case 31: *state = "New York"; break;
case 32: *state = "North Carolina"; break;
case 33: *state = "North Dakota"; break;
case 34: *state = "Ohio"; break;
case 35: *state = "Oklahoma"; break;
case 36: *state = "Oregon"; break;
case 37: *state = "Pennsylvania"; break;
case 38: *state = "Rhode Island"; break;
case 39: *state = "South Carolina"; break;
case 40: *state = "South Dakota"; break;
case 41: *state = "Tennessee"; break;
case 42: *state = "Texas"; break;
case 43: *state = "Utah"; break;
case 44: *state = "Vermont"; break;
case 45: *state = "Virginia"; break;
case 46: *state = "Washington"; break;
case 47: *state = "West Virginia"; break;
case 48: *state = "Wisconsin"; break;
case 49: *state = "Wyoming"; break;
case 50: *state = "unknown";
}
}
int main(){
char name[32] = "unknown", rating[25] = "unknown";
char* state = 0;
int lobbyists=0, prevemploys=0, number=0, statenumber, length, i;
double networth=0, tarp=0, contributions=0, ratio1, ratio2;
printf("\n\n(1)Bank name: %s\n(2)State: %s\n(3)Net worth: %.2f\n", name, state, networth);
printf("(4)Rating: %s\n(5)TARP money: %.2f\n(6)Campaign contributions: %.2f\n", rating, tarp, contributions);
printf("(7)Lobbyists in capitol: %d\n(8)Previous Employees in government: %d\n", lobbyists, prevemploys);
printf("\n(9)Display Data\n(10)Clear all Data\n(11)Quit\n\n");
while (number != 11){
printf("Type the number to edit the field / perform task, and press ENTER: ");
scanf("%d",&number);
printf("\n\n");
switch (number) {
case 1:
printf("Enter the name of the bank (max 32 characters): ");
scanf("%s",name);
for((i=0);(name[i]='\0');(i++)){
length = i; }
if(length >31){
printf("Too many characters.");
strcpy(name,"unknown");
}
printf("Bank Name: %s\n\nEnter 9 to display options.\n\n",name);
break;
case 2:
printstates();
scanf("%d",&statenumber);
if(statenumber<0 || statenumber>49){
printf("That is an option.");
statenumber = 50;
}
writestate(statenumber,&state);
printf("State: %s\n\nEnter 9 to display options.\n\n",state);
break;
case 3:
printf("Enter the bank's net worth(- trillion to + trillion): ");
scanf("%lf",&networth);
if (networth < -1000000000000 || networth > 1000000000000){
printf("\nError: That is not in the given range.");
networth=0; }
printf("Net Worth: %f\n\nEnter 9 to display options.\n\n",networth);
break;
case 4:
printf("Enter the bank's rating: ");
scanf("%s",rating);
printf("Bank Rating: %s\n\nEnter 9 to display options.\n\n", rating);
break;
case 5:
printf("Enter the amount of TARP money the bank has received (0 to 1 trillion): ");
scanf("%lf",&tarp);
if (tarp < 0 || tarp > 1000000000000){
printf("\nError: That is not in the given range.");
tarp=0; }
printf("TARP money: %f\n\nEnter 9 to display options.\n\n",tarp);
break;
case 6:
printf("Enter the amount of campaign contributions the bank has made to the current administration (0 to 1 trillion): ");
scanf("%lf",&contributions);
if (contributions < 0 || contributions > 1000000000000){
printf("\nError: That is not in the given range.");
contributions=0; }
printf("Contributions: %f\n\n\Enter 9 to display options.\n\n",contributions);
break;
case 7:
printf("Enter the number of lobbyists the bank has in DC (0 to 100): ");
scanf("%d",&lobbyists);
if (lobbyists < 0 || lobbyists > 100){
printf("Error: That is not in the given range.");
lobbyists=0; }
printf("Lobbyists: %d\n\nEnter 9 to display options.\n\n",lobbyists);
break;
case 8:
printf("Enter the # of previous employees the bank has working for the gov. (0 to 1000): ");
scanf("%d",&prevemploys);
if (prevemploys < 0 || prevemploys > 1000){
printf("Error: That is not in the given range.");
prevemploys=0; }
printf("Gov Employees: %d\n\nEnter 9 to display options.\n\n",prevemploys);
break;
case 9:
printf("\n\n(1)Bank name: %s\n(2)State: %s\n(3)Net worth: %.2f\n", name, state, networth);
printf("(4)Rating: %s\n(5)TARP money: %.2f\n(6)Campaign contributions: %.2f\n", rating, tarp, contributions);
printf("(7)Lobbyists in capitol: %d\n(8)Previous Employees in government: %d\n", lobbyists, prevemploys);
printf("\n(9)Display Data\n(10)Clear all Data\n(11)Quit\n\n");
if(contributions != 0){
ratio1=tarp/contributions;
printf("Ratio of TARP money to campaign contributions: %f",ratio1);
}
if(lobbyists!=0){
ratio2=tarp/lobbyists;
printf("Ratio of TARP money to lobbyists in DC: %f",ratio2);
}
break;
case 10:
strcpy(name,"unknown");
state = 0;
networth = 0;
strcpy(rating, "unknown");
tarp = 0;
contributions = 0;
lobbyists = 0;
prevemploys = 0;
printf("\n\nAll data cleared. Enter 9 for more options.\n\n");
break;
case 11:
printf("\n\n\nProgram terminated. Goodbye World!\n\n");
break;
default:
printf("That is not one of the options. Please choose one of the options.\n\n");
}
}
return 0;
}
And here are the warnings:
hw1.c: In function `main':
hw1.c:126: warning: integer constant is too large for "long" type
hw1.c:126: warning: integer constant is too large for "long" type
hw1.c:142: warning: integer constant is too large for "long" type
hw1.c:151: warning: integer constant is too large for "long" type
And I am getting a segmentation fault. I have no idea why.
Please help : )