Thread: need char help, new to C

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    14

    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", &regcost[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]);
    
                            }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    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.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    14
    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:

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    14
    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

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    14
    Nevermind!!!!! all it does is store state1, state2 doesnt even get stored

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM

Tags for this Thread