1

This is a program for a class project. It is suppose to be able to create and edit structures. The CreatRec function works fine. For the ModifyRec I am trying to send it the array by pointers in order to avoid having to "copy" the data. However, I am having trouble getting it to actually change the array. ATM The line at the bottom (gr[change].lastname= *info;) is not working at all. I really have no clue what I am doing wrong here.

 #include "stdafx.h"
#include <string.h>
#include <stdlib.h>

struct student{
    int recordname;
    char lastname[10];
    char firstname[10];
    float math;
    float english;
    float science;
};

//prototypes
int menu();
struct student  CreatRec(int);
void ModifyRec(struct student*);


void main()
{
    int option, j;//option will be for users menu choice, j makes for loop work for creatrec
    struct student grades[10];
    j = 0;
    option=Menu();
    if (option == 1)
        for (j = 0; j<10; j++)
            (grades[j + 0]) = CreatRec(j);
    else if (option==2)
        ModifyRec(grades);//dont need & is smart bc array


    printf("%s",grades[0].lastname);//This line is checking to see if ModifyRec actaully worked

    //free(grades);2
    while (1);
}


int Menu()
{
    int choi;
    printf("Please choose one of the following options.\n 1) Create  New Student Records.\n 2) Modify an Existing Student Record\n");
    printf(" 3) Print a New Sutdent Record.\n 4) Quit\n");
    scanf("%d", &choi);
    return choi;
}
struct student  CreatRec(int i)
{
    struct student qr;
    //qr = (struct student*)malloc(sizeof(struct student)*6);

    printf("RecordNum %i\n", i);
    printf("Please enter last name-->");
    scanf("%s", &qr.lastname);
    printf("Please enter first name-->");
    scanf("%s", &qr.firstname);
    printf("Please math grade-->");
    scanf("%f", &qr.math);
    printf("Please english grade-->");
    scanf("%f", &qr.english);
    printf("Please science grade-->");
    scanf("%f", &qr.science);
    return qr;
}

void ModifyRec(struct student gr[])
{
    int change;
    char feild[10], info[10];

    printf("Which record would you like to change?\n");
    scanf("%d", &change);
    rewind(stdin);
    printf("Which feild would you like to edit?\n");
    scanf("%s", &feild);
    rewind(stdin);
    printf("Enter info\n");
    scanf("%s", &info);

    if (!strcmp("lastname", feild))
        gr[change].lastname= *info;//NOT WORKING

}
3
  • Why the C++ tag? A typo? Commented Apr 1, 2014 at 20:01
  • the site wrote that tag for me Commented Apr 1, 2014 at 20:02
  • it wasnted allowing be to enter info on the next time when I didnt have the rewind Commented Apr 1, 2014 at 20:03

2 Answers 2

1

First of all I do not see a great sense in expression grades[j + 0] of statement

    for (j = 0; j<10; j++)
        (grades[j + 0]) = CreatRec(j);

These statements

printf("Please enter last name-->");
scanf("%s", &qr.lastname);
printf("Please enter first name-->");
scanf("%s", &qr.firstname);

have to be substituted for

printf("Please enter last name-->");
scanf("%s", qr.lastname);
printf("Please enter first name-->");
scanf("%s", qr.firstname);

And this statement

if (!strcmp("lastname", feild))
    gr[change].lastname= *info;//

has to be substituted for

if (!strcmp("lastname", feild))
    strcpy( gr[change].lastname, info );
Sign up to request clarification or add additional context in comments.

5 Comments

Ok so your second substitutions works wonderful, thank you so much! However, the first one, that doesnt work one it gets down to scanning in the ints. As for the first bit, I have an array of 10 structures, so I have to fill in all 10 with the creatrec
@user3486669 You have to enter ENTER key after each data that the input would be correct.
Im not quite sure I understand what you mean
@user3486669 When you enterd for example last name press Enter key
oh yes, I know that, and it works thats way with just the chars, but not the floats
0

gr[change].lastname is a char array, not a pointer. You can't reassign it. In this case, you probably ought to do scanf("%s", gr[change].lastname); and skip char info[10] altogether.

2 Comments

ok but later on I how to allow them to edit other fields as well, (firstname) etc. Are you saying I should have scanf("%s", FEILD) for each one?
you could. another possibility is to set up a char *destination to point to a specific field and then scanf("%s", destination).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.