Question

In Language = C Concepts for this are (malloc/realloc, structs, and stdin/fgets/getc/scanf) Description Your program will...

In Language = C

Concepts for this are (malloc/realloc, structs, and stdin/fgets/getc/scanf)

Description

Your program will ask the user to type in the following information:

First Name:

Last Name:

ID#:

Email:

The program will terminate when a single . is entered as a First Name.

This information will be stored in the following structure type:

struct student {

int recordCount;

char *firstName;

char *lastName;

char *id;

char *email;

}

recordCount is a number assigned at the beginning of the program starting at 1 and increasing by 1 for each student record entered. No name (first or last) will be more than 50 characters long but you must only store the exact number of characters needed. The id will be 9 characters in length (plus end of string). You must check to see if this is the case and that each character is a decimal digit (0 to 9). If it is not, the program will tell the user this and ask for the ID# again until it is entered correctly. There is no error checking on the email input.

Any number of students can be input to the program.

After all student records have been input, the program will print out all of the records sorted by ID# (smallest to largest). The output will look like the following:

091234567,Emma,Peel,[email protected]

092345678,John,Steed,[email protected]

Sample Input and Output

$ ./enterStudents

First Name: Emma

Last Name: Peel

ID#: 091234567

Email: [email protected]

First Name: John

Last Name: Steed

ID#: 092345678

Email: [email protected]

First Name:.

091234567,Emma,Peel,[email protected]

092345678,John,Steed,[email protected]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

// declaring structure to store student information
typedef struct student{
int recordCount;
char *firstName;
char *lastName;
char *id;
char *email;
struct student *ptr;
} student;

// function which checks if a given string is numeric or not
int containsNumber(const char *str)
{
while (*str) {
if (isdigit(*str++) == 0) return 0;
}
return 1;
}

/*
* This function is used to insert a student in the list.
* student will be added in sorted order on basis of id number(small to large)
*/

student insert(student head, int recordCount, char firstName, char lastName, char id, char email) {
student temp, prev, *next;

// create a temporary student
temp = (student*)malloc(sizeof(student));

// populate the student with information
temp->recordCount = recordCount;
temp->firstName = firstName;
temp->lastName = lastName;
temp->id = id;
temp->email = email;

temp->ptr = NULL;

// adding the student node to proper location in linked list
if(!head){
head=temp;
} else{
prev = NULL;
next = head;

// keep traversing until id is less than this node
while(next && (strcmp(next->id, id)<=0)){
prev = next;
next = next->ptr;
}

// fixing it to the correct position
if(!next){
prev->ptr = temp;
} else{
if(prev) {
temp->ptr = prev->ptr;
prev-> ptr = temp;
} else {
temp->ptr = head;
head = temp;
}
}
}
return head;
}

/*
* function used to cleanup the linked list after use
*/
void free_list(student *head) {
student *prev = head;
student *cur = head;
while(cur) {
prev = cur;
cur = prev->ptr;
free(prev->email);
free(prev->firstName);
free(prev->lastName);
free(prev->id);
free(prev);
}
}

/*
* Driver function to test
*/
int main(){

int recordCount = 0;

student head, p;
head = NULL; // head of the list where we will store student information

// temporary variable to read the line
char c[500];

// run forever, untill break
while(1) {
printf("Enter First Name:");
scanf("%s", c);

// checking if user wants to break
if(strcmp(c, ".") == 0) {
break;
}

// creating memory for firstname
char firstname = (char*)malloc((strlen(c) + 1) sizeof(char));
strcpy(firstname, c);

printf("Enter Last Name:");
scanf("%s", c);
// creating memory for lastname
char lastname = (char*)malloc((strlen(c) + 1) sizeof(char));
strcpy(lastname, c);

char *id;
while(1) {
// keep asking untill valid
printf("Enter ID:");
scanf("%s", c);
if((!containsNumber(c)) || (strlen(c) != 9)) {
printf("Error: ID should be numeric and 9 digis long only ");
continue;
}

// creating memory for id
id = (char*)malloc((strlen(c) + 1) * sizeof(char));
strcpy(id, c);
break;
}

printf("Enter Email:");
scanf("%s", c);
// creating memory for email
char email = (char*)malloc((strlen(c) + 1) sizeof(char));
strcpy(email, c);

// adding new student node to the list
head = insert(head, ++recordCount, firstname, lastname, id, email);

printf(" ");
}

// printing the records
printf(" Printing The records in sorted order:: ");
p = head;

// keep looping untill more nodes are there
while(p) {
printf("%s,", p->id);
printf("%s,", p->firstName);
printf("%s,", p->lastName);
printf("%s ", p->email);

p = p->ptr;
}

// free the allocated space after use
free_list(head);

return 0;
}

Sample Output:

Add a comment
Know the answer?
Add Answer to:
In Language = C Concepts for this are (malloc/realloc, structs, and stdin/fgets/getc/scanf) Description Your program will...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #pragma warning(disable : 4996) // to avoid scanf...

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #pragma warning(disable : 4996) // to avoid scanf error, Visual Studio only #define MAX_DIM 3 #define STUDENTS 5            // total number of students int scores[STUDENTS]; int *pScores = NULL; C language Implement the function that creates and displays the user name of the user. Ask the user for their first and last name. The username is the first letter of first name, followed by the last name. For instance, if the user's...

  • In Java, How would I create a program that does this from user input? studentld: consists...

    In Java, How would I create a program that does this from user input? studentld: consists of the first two characters of the student's first name, student's birth year, and the last two characters of the last name. For instance, if the student's full name is John mith and birthyear is 1988, then the studentid will be jo1988th. s email: consists of the first name, dot, last name, and @pa2.com. For instance, if the student's full name is John Smith,...

  • The program reads an unknown number of words – strings that all 20 characters or less...

    The program reads an unknown number of words – strings that all 20 characters or less in length. It simply counts the number of words read. The end of input is signaled when the user enters control-d (end-of-file). Your program prints the number of words that the user entered. ****** How do you I make it stop when control-d is entered. My code: #include <stdio.h> void main(void) { char sentence[100]; int i = 0; int count = 1; printf("Enter a...

  • I need a C++ program like this please! and please follow all instructions! Thanks so much!...

    I need a C++ program like this please! and please follow all instructions! Thanks so much! A menu-driven program with 3 choices: Names Rearranger, Number Validator, or Exit Menu Option 1: Name Rearranger: 1. Input ONE STRING from the user containing first name, middle name, and last name. ie, "John Allen Smith". USE THE GETLINE function. (Not cin) 2. Loop through the string and validate for a-z or A-Z or . If the name has any other characters, then ask...

  • IN C language Write a C program that prompts the user to enter a line of...

    IN C language Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr andcreadLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate...

  • Program in IDLE: At a university, each student is assigned a system login name, which the...

    Program in IDLE: At a university, each student is assigned a system login name, which the student uses to log into the campus computer system. As part of your internship with the university's Information Technology department, you have been asked to write the code that generates system login names for students. You will use the following algorithm to generate a login name: 1) Get the first 3 characters of the student's first name. 2) Get the first 2 characters of...

  • Please help modify my C program to be able to answer these questions, it seems the...

    Please help modify my C program to be able to answer these questions, it seems the spacing and some functions arn't working as planeed. Please do NOT copy and paste other work as the answer, I need my source code to be modified. Source code: #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { char title[50]; char col1[50]; char col2[50]; int point[50]; char names[50][50]; printf("Enter a title for the data:\n"); fgets (title, 50, stdin); printf("You entered: %s\n", title);...

  • Write a program in Java according to the following specifications: The program reads a text file...

    Write a program in Java according to the following specifications: The program reads a text file with student records (first name, last name and grade on each line). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "print" - prints the student records (first name, last name, grade). "sortfirst" - sorts the student records by first name. "sortlast" - sorts the student records by last name. "sortgrade" - sorts the...

  • lab4C.c file contains: #include <stdio.h> ..... #define SIZE 10 #define SIZE2 40 int main(int argc, char *argv...

    lab4C.c file contains: #include <stdio.h> ..... #define SIZE 10 #define SIZE2 40 int main(int argc, char *argv[]) { char input[SIZE2]; char name[SIZE]; .... char resu[SIZE2], resu2[SIZE2], resu3[SIZE2]; printf("Enter name, age and wage (exit to quit): "); fgets(input, 40, stdin); while (...) { /* use fgets to read again */ printf("Enter name, age and wage (exit to quit): "); fgets(input, 40, stdin); } return 0; }3.1 Specification Develop an ANSI-C program that reads user information from the standard inputs, and outputs...

  • c++ The program will be recieved from the user as input name of an input file...

    c++ The program will be recieved from the user as input name of an input file and and output.file. then read in a list of name, id# and score from input file (inputFile.txt) and initilized the array of struct. find the student with the higher score and output the students info in the output file obtain the sum of all score and output the resurl in output file. prompt the user for a name to search for, when it find...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT