-
need char help, new to C
Hello,
I'm pretty new to the C language and the TA isn't on my campus right now so i thought i would ask on the forums :)
here is my problem, I have to store a state in 2 separate arrays because my professor wont allow us to use stings for this exercise. The way i have my code set up now is it gets data about a specific event and at the end of the for loop it gets the state. How would i go about putting the inputted state into these two arrays ?
The array names are state1 and state2
If the user place TX for Texas in the input i need state1 to contain T, and state2 to contain X.
Please help if you can :)
Code:
for(i = 0; i < events; i++ )
{
printf("Please enter event code for event number %d:", i + 1);
scanf("%d", &code[i]);
printf("Please enter the month of the event number %d:", i + 1);
scanf("%d", &month[i]);
printf("Please enter the day of the event number %d:", i + 1);
scanf("%d", &day[i]);
printf("Please enter the year of the event number %d:", i + 1);
scanf("%d", &year[i]);
printf("Please enter the earlybird special cost for event number %d:", i + 1);
scanf("%f", &earlycost[i]);
printf("Please enter the regular cost for event number %d:", i + 1);
scanf("%f", ®cost[i]);
printf("Please enter speakers series letter for event number %d:", i + 1);
scanf("%c", &series[i]);
getchar();
printf("Please enter speakers state for event number %d:", i + 1);
scanf("%c%c", &state1[i], &state2[i]);
}
-
That has to be the stupidest assignment constraint of which I've ever heard. Is the code not working as you expect? You need to eat the newline after the last scanf just as you did previously.
-
I know!!! I would normaly just use a string but she says we are to do it this way to get credit. Crazy as heck
What happens when i run the program is this:
Code:
[jjh2808@omega ~]$ gcc Tester.c
[jjh2808@omega ~]$ ./a.out
Data Entry Phase
Please enter the number of events to be inputed to the system
Minimum need is 10 Maximum allowed is 20:10
Thank You!
Please enter event code for event number 1:1345
Please enter the month of the event number 1:11
Please enter the day of the event number 1:23
Please enter the year of the event number 1:2010
Please enter the earlybird special cost for event number 1:32.50
Please enter the regular cost for event number 1:56.70
Please enter speakers series letter for event number 1:M
Please enter speakers state for event number 1:TX
Please enter event code for event number 2:Please enter the month of the event number 2:Please enter the day of the event number 2:Please enter the year of the event number 2:Please enter the earlybird special cost for event number 2:Please enter the regular cost for event number 2:Please enter speakers series letter for event number 2:Please enter speakers state for event number 2:
-
I think i found a way to do it but its kind of an arbitrary way, i just did this at the last line of the for loop
Code:
printf("Please enter speakers state for event number %d:", i + 1);
scanf("%c%c", &state1[i], &state2[i]);
scanf("%c", &state1[i+1]);
And when it starts up again it will just cover up the char in state1[1], ill just have to make the array larger by 1 than all the other arrays. Not sure if this is the correct way though, but it runs
-
Nevermind!!!!! all it does is store state1, state2 doesnt even get stored