Thread: Code works almost perfectly, except two of my strings are swapped and can't fix

  1. #1
    Registered User
    Join Date
    Mar 2013

    Unhappy Code works almost perfectly, except two of my strings are swapped and can't fix

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define StringSIZE 81
    char tempString[StringSIZE];
    typedef struct 
        char *name;
        char IDnum[10];
        int age;
        int salary;
        float SETE;
    int main()
        int i;
        int NumofT;
        TeacherRecord *teacher;               
        scanf("%d",&NumofT);                //Scans the first number from input, number of teachers
        for(i = 0; i < NumofT; i++)            //Common for loop, will loop until all teachers have been processed
            teacher = (TeacherRecord *) malloc (sizeof(TeacherRecord));        //Gets size of structure
            scanf(" %s", tempString);                                            //scans first string on new line
            teacher->name = (char *) malloc(strlen(tempString)+1);            //allocates enough size in tempString for *name
            strcpy(teacher->name,tempString);                                //copies *name into tempString
            scanf("%s %d %d %f", teacher->IDnum,                            //Scans the rest of the data
                   &teacher->age, &teacher->salary, &teacher->SETE);        //Stores the values to TeacherRecord Structure
            printf("\n\n %s, %s %s, %s %d, %s %d, %s  %6.2f\n\n",            //Arranged printf statement so that output matches Praktomat expectation:
                   teacher->name,                                             //Ex. Sweany, id = 123456789, is 53, earns $8096, and has a SETE of   3.66
                   " ID = ", teacher->IDnum, 
                   " is ", teacher->age,                                 
                   " earns $", teacher->salary, 
                   " and has a SETE of ",teacher->SETE);
        return 0; 
    The program is fairly simple, and I know I'm just a few tweaks away from making it work perfectly. Only problem is that my names and ID's switch places when I submit the data. The rest of the variables print out perfectly. In the attachment you can see where the data is entered and everything comes out fine except for the placement of the ID number and the name. I don't see how that's possible while looking at my code, as the printf starts with the name and is then followed by the ID number. I don't see how they could have switched values at any time through my program, so this has really put me in quite the pickle.

    Code works almost perfectly, except two of my strings are swapped and can't fix-strings-swapped-png

  2. #2
    Registered User
    Join Date
    Dec 2011
    You enter the ID into teacher->name, hence...

  3. #3
    Registered User
    Join Date
    Mar 2013
    That moment when you wasted hours trying to find some way to fix this, and the answer was really that simple -_-
    Thank you Caligulaminus

  4. #4
    Registered User
    Join Date
    Mar 2013
    Only one problem left and my code will be perfect. No matter what number I set i = to my for loop always comes out 1 increment short. If I have 4 people to process, it gives me 3. If I have 3, it gives me 2. I've tried increasing n+1, increasing, decreasing i, setting i=0. I'm surprised increasing n+1 didn't help considering just reading data that started with a 4 instead of a 3 made a difference.

    Pic is still a lot like the attachment, just with ID and names in right place. Still coming short by 1.

    EDIT: Fixed the problem by adding ("%d+1", n);
    Program works perfectly. Am expecting a 100
    Last edited by Campocalypse; 03-26-2013 at 03:35 PM.

  5. #5
    Join Date
    Oct 2007
    Inside my computer
    Is there a reason you posted this in the C++ section?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Illegal Instruction at perfectly fine Code
    By m00ni in forum C Programming
    Replies: 24
    Last Post: 02-14-2011, 02:56 AM
  2. How come my code works? Seriously. :)
    By assiduus in forum C Programming
    Replies: 15
    Last Post: 02-10-2011, 12:54 PM
  3. Replies: 15
    Last Post: 09-23-2010, 02:19 PM
  4. Replies: 4
    Last Post: 08-18-2009, 03:32 PM
  5. How Do u See if a code works
    By Nos in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2005, 01:34 PM

Tags for this Thread