When I execute my code, I am able to enter the size of the classroom 3 X 3. I am
ID: 3881647 • Letter: W
Question
When I execute my code, I am able to enter the size of the classroom 3 X 3. I am able to enter the first student's name "Mickey/Mouse". I am able to type in the row (1) and column (2) I want to place this student. However my code keeps saying that the seat is taken when it's not taken. I am not sure how to fix this.
#include <stdio.h>
#include <malloc.h>
#include <string.h>
//Define struct student.
struct student{
char last_name[30];
char first_name[30];
};
struct classroom_seating {
struct student **seating;
};
void student_init_default(struct student *s){
strcpy(s->last_name,"???");
strcpy(s->first_name,"???");
}
void student_init(struct student *s, char *info){
strcpy(s->first_name, strtok(info, "/"));
strcpy(s->last_name, strtok(NULL, "/"));
}
void student_to_string(struct student *s){
printf("%c.%c. ",s->first_name[0],s->last_name[0]);
}
void classroom_seating_init(int rowNum, int columnNum, struct classroom_seating *a){
int i;
int j;
a->seating = (struct student **)malloc(rowNum * sizeof(struct student*));
for(i=0;i<rowNum;i++)
{
a->seating[i] = (struct student*)malloc(columnNum* sizeof(struct student));
}
for(i=0; i<rowNum; i++){
for(j=0; j<columnNum;j++){
struct student obj;
student_init_default(&obj);
a->seating[i][j]=obj;
}
}
}
int assign_student_at(int row, int col, struct classroom_seating *a, struct student *s){
char string[30];
strcpy(string, a->seating[row][col].first_name);
if(strcmp(string, "???")){
strcpy(a->seating[row][col].first_name,s->first_name);
strcpy(a->seating[row][col].last_name,s->last_name);
return 1;
}else{
return 0;
}
}
int check_boundaries(int row, int col, struct classroom_seating *a){
if(row<0 || col<0 || row>sizeof(a->seating) || col>sizeof(a->seating[0])){
return 0;
}else{
return 1;
}
}
void classroom_seating_to_string(struct classroom_seating *a){
int i, j;
printf("Current Seating ");
for(i=0; i< sizeof(a->seating);i++){
for(j=0;j<sizeof(a->seating[0]);j++){
student_to_string(&a->seating[i][j]);
}
printf(" ");
}
}
int main(){
struct student temp_student;
struct classroom_seating seat;
int row, col, rowNum, columnNum;
char student_info[60];
//Ask a user to enter a number of rows for a classroom seating
printf("Enter a number of rows for a classroom seating chart.");
scanf("%d", &rowNum);
//Ask a user to enter a number of columns for a classroom seating chart.
printf("Enter a number of columns for a classroom seating chart.");
scanf("%d", &columnNum);
//classroom_seating
classroom_seating_init(rowNum, columnNum, &seat);
printf("Enter a student's information (first_name/last_name) or enter "Q" to quit.");
scanf("%s",&student_info);
student_init(&temp_student, student_info);
while (strcmp(student_info, "Q") !=0){
printf(" A student information is read. ");
student_to_string(&temp_student);
printf(" ");
//Ask a user to decide where to seat a student by asking
//for row and column of a seat.
printf("Enter a row number where the student wants to sit.");
scanf("%d", &row);
printf("Enter a column number where the student wants to sit.");
scanf("%d", &col);
//Checking if the row number and column number are valid.
//(exist in the classroom that we created).
if(check_boundaries(row, col, &seat) == 0){
printf("A student %s %s is not assigned a seat.", temp_student.first_name, temp_student.last_name);
}else{
//Assigning a seat for a student
if(assign_student_at(row, col, &seat, &temp_student) == 1){
printf(" The seat at row %d and column %d is assigned to the student", row, col);
student_to_string(&temp_student);
classroom_seating_to_string(&seat);
}else{
printf(" The seat at row %d and column %d is taken.", row,col);
}
printf("Please enter a student information or enter "Q" to quit.");
scanf("%s",student_info);
}
return 0;
}
}
Explanation / Answer
int assign_student_at(int row, int col, struct classroom_seating *a, struct student *s){
char string[30];
strcpy(string, a->seating[row][col].first_name);
if(strcmp(string, "???")){
strcpy(a->seating[row][col].first_name,s->first_name);
strcpy(a->seating[row][col].last_name,s->last_name);
return 1;
}else{
return 0;
}
}
I belive you want to assign a seat to the student in case his first name is still "???". If that's the case, you are getting incorrect outcome because of incorrect usage of strcmp(string, "???").
The strcmp(str1, str2) returns 0 in case str1 and str2 are equal. But, the condition if(strcmp(string, "???")) checks if the string and "???" are not equal, then execute the if statements.
Hence, you need to change the "if(strcmp(string, "???")) to if(strcmp(string, "???") == 0)". The correct code snippet is
int assign_student_at(int row, int col, struct classroom_seating *a, struct student *s){
char string[30];
strcpy(string, a->seating[row][col].first_name);
if(strcmp(string, "???") == 0){
strcpy(a->seating[row][col].first_name,s->first_name);
strcpy(a->seating[row][col].last_name,s->last_name);
return 1;
}else{
return 0;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.