getting bubble sort to work with structure - c

im not sure how to bubble sort using structures, id like the function sortMovies to be able to sort the movies alphebeticaly by title but i am getting these errors listed below.
#include <stdio.h>
#include <conio.h>
#define CONST 100
void sortMovies(struct movies main[CONST]);
void changeMovie(struct movies main);
int findMovie(struct movies main, int nOfMovies, struct movies tempMovie);
struct movies newMovie();
typedef struct movies{
char title[30];
char UPC[12];
int qnty;
double price;
}movies;
int main()
{
int nOfMovies = 0, findR, stop = 0, nOfMoviesArr[CONST];
char decider;
while (stop != 1)
{
movies main[CONST];
movies tempMovie;
printf("(A)dd a new movie\n(C)hange a Movie's Information \n(D)elete a Movie \n(L)ist All Movies\n(Q)uit");
scanf(" %c", &decider);
switch (decider)
{
case 'a':
case 'A':
tempMovie = newMovie();
nOfMovies = findMovie(main[CONST], nOfMovies, tempMovie);
nOfMovies++;
break;
case 'c':
case 'C':
printf("enter movie upc:");
scanf("%s", main);
break;
case 'l':
case 'L':
sortMovies(main[CONST]);
break;
case 'q':
case 'Q':
return 0;
break;
default:
printf("An invalid option was selected!");
}
}
}
struct movies newMovie() {
movies new;
printf("enter movie upc:");
scanf("%s", new.UPC);
printf("enter movie title:");
scanf("%s", new.title);
printf("enter movie qauntity:");
scanf("%d", new.qnty);
if (new.qnty <= 0)
{
printf("quanitity must be greater than 0");
printf("enter movie qauntity:");
scanf("%d", new.qnty);
}
printf("enter movie price:");
scanf("%lf", new.price);
if (new.price <= 0)
{
printf("price must be greater than 0");
printf("enter movie price:");
scanf("%lf", new.price);
}
return new;
}
int findMovie(struct movies main, int nOfMovies, struct movies tempMovie)
{
int count;
for (count = 0; count < nOfMovies; count++)
{
if (tempMovie.UPC == main.UPC)
{
return count;
}
else
{
printf("error");
return -1;
}
}
}
void changeMovie(struct movies main)
{
char decider;
printf("would you like to change the value? y or no input");
switch (decider)
{
case 'y':
case 'Y':
printf("enter movie title:");
scanf("%c", main.title);
printf("enter movie qauntity:");
scanf("%d", main.qnty);
if (main.qnty <= 0)
{
printf("quanitity must be greater than 0");
printf("enter movie qauntity:");
scanf("%d", main.qnty);
}
printf("enter movie price:");
scanf("%lf", main.price);
if (main.price <= 0)
{
printf("price must be greater than 0");
printf("enter movie price:");
scanf("%lf", main.price);
}
break;
return 0;
}
}
void sortMovies(struct movies main[CONST])
{
int i, sflag, count = 0;
char temp;
do
{
sflag = 0;
for (i = 1; i < CONST; i++)
{
if (main[i - 1].title > main[i].title)
{
temp = main[i - 1].title;
main[i - 1].title = main[i].title;
main[i].title = temp;
sflag = 1;
}
}
count++;
} while (sflag);
for (i = 0; i < CONST; i++)
{
printf("%c\t%c\t%d\t%lf", main[i].title, main[i].UPC, main[i].qnty, main[i].price);
}
} ```
Severity Code Description Project File Line Suppression State Suppression State
Error C2440 'function': cannot convert from 'movies' to 'movies *' 40
Error (active) E0167 argument of type "movies" is incompatible with parameter of type "struct movies *" 40
Error (active) E0137 expression must be a modifiable lvalue 148
Error C2106 '=': left operand must be l-value 148
Error (active) E0137 expression must be a modifiable lvalue 149
Error C2106 '=': left operand must be l-value 149

Error (active) E0167 argument of type "movies" is incompatible with parameter of type "struct movies *" 40
This is happening because with main[CONST] you are trying to pass a single element of of the main array (the element at index CONST), rather than the whole array when the sortMovies method expects an array. To pass the whole array into the function, use sortMovies(main);.
Error (active) E0137 expression must be a modifiable lvalue 148
Error C2106 '=': left operand must be l-value 148
Error (active) E0137 expression must be a modifiable lvalue 149
Error C2106 '=': left operand must be l-value 149
This is happening because you are trying to copy the contents of one char array to another with the = operator, which is not valid. Instead, use strcpy(main[i - 1].title, main[i].title) and strcpy(main[i].title, temp).
Also, in your sortMovies function you declare temp as a char which you try to use to swap the title strings. What you want here is instead char* temp. If you are not familiar with this syntax, I would recommend reading up on pointers in C. They are a very important construct in the language, and a better understanding of pointers will likely also help you clear up a lot of the issues you are having with this program.

Related

Values are getting stored in different stack even though different variables are used in the code for each stack

I wanted to create 4 stacks, 1 for each subject. Then in each stack store number of days left for each assignment to be submitted. This is the code I have written. The code works with the exception of 1 flaw:
When values are stored in one stack they also show up in the other stacks with value 0 and same number of elements for example :
I store values 5,20,7 in the stack of ds exit that stack and go to the stack of dsgt and store 9,11 and then on printing the values in stack of dsgt the code prints out as " 0 0 0 9 11" here 0 0 0 are the 3 elements of the 1st stack. Now on exiting the stack of dsgt and going back to stack of ds and printing its value the output is "5 7 20 0 0 " , 0 0 here are the elements of the stack of dsgt which I dont want to print with ds and rest of the stacks.
#include <stdio.h>
#include <conio.h>
#define MAXSIZE 20
int st1[MAXSIZE];
int st2[MAXSIZE];
int st3[MAXSIZE];
int st4[MAXSIZE];
int top = -1;
void push(int st[], int item);
int pop(int st[]);
int peek(int st[]);
void display(int st[]);
void sort(int st[]);
int main() {
int subj;
printf("\nEnter the number associated with the Subject assignment that has to be tracked\n");
int choice = 0, item1;
do {
printf("\n 1. Data Structures");
printf("\n 2. DSGT ");
printf("\n 3. CG ");
printf("\n 4. Math");
printf("\n 5. Exit Code");
printf("\n Enter Your Choice");
scanf("%d", & choice);
switch (choice) {
case 1:
ds();
break;
case 2:
dsgt();
break;
case 3:
cg();
break;
case 4:
math();
break;
case 5:
printf("Exited");
break;
default:
printf("\n Wrong Input");
}
} while (choice != 5);
return 0;
}
int dsgt() {
int choice, item2;
do {
printf("Stack Operation \n");
printf("\n 1. Add A New Assignment");
printf("\n 2. Remove the latest Completed Assignment ");
printf("\n 3. View the latest Pending Assignment ");
printf("\n 4. View All the pending Assignments");
printf("\n 5. Exit Code");
printf("\n Enter Your Choice");
scanf("%d", & choice);
switch (choice) {
case 1:
printf("\n Enter the Number of Days Left for the Assignment to be Submitted");
scanf("\n %d", & item2);
push(st2, item2);
sort(st2);
break;
case 2:
item2 = pop(st2);
printf("\n Removed Assignment is: \n %d", item2);
break;
case 3:
item2 = peek(st2);
printf("\n The Latest Assignment to be Submitted is:%d", item2);
break;
case 4:
display(st2);
break;
case 5:
printf("Exited");
break;
default:
printf("\n Wrong Input");
}
} while (choice != 5);
}
int ds() {
int choice, item1;
do {
printf("Stack Operation \n");
printf("\n 1. Add A New Assignment");
printf("\n 2. Remove the latest Completed Assignment ");
printf("\n 3. View the latest Pending Assignment ");
printf("\n 4. View All the pending Assignments");
printf("\n 5. Exit Code");
printf("\n Enter Your Choice");
scanf("%d", & choice);
switch (choice) {
case 1:
printf("\n Enter the Number of Days Left for the Assignment to be Submitted");
scanf("\n %d", & item1);
push(st1, item1);
sort(st1);
break;
case 2:
item1 = pop(st1);
printf("\n Removed Assignment is: \n %d", item1);
break;
case 3:
item1 = peek(st1);
printf("\n The Latest Assignment to be Submitted is:%d", item1);
break;
case 4:
display(st1);
break;
case 5:
printf("Exited");
break;
default:
printf("\n Wrong Input");
}
} while (choice != 5);
}
int cg() {
int choice, item3;
do {
printf("Stack Operation \n");
printf("\n 1. Add A New Assignment");
printf("\n 2. Remove the latest Completed Assignment ");
printf("\n 3. View the latest Pending Assignment ");
printf("\n 4. View All the pending Assignments");
printf("\n 5. Exit Code");
printf("\n Enter Your Choice");
scanf("%d", & choice);
switch (choice) {
case 1:
printf("\n Enter the Number of Days Left for the Assignment to be Submitted");
scanf("\n %d", & item3);
push(st3, item3);
sort(st3);
break;
case 2:
item3 = pop(st3);
printf("\n Removed Assignment is: \n %d", item3);
break;
case 3:
item3 = peek(st3);
printf("\n The Latest Assignment to be Submitted is:%d", item3);
break;
case 4:
display(st3);
break;
case 5:
printf("Exited");
break;
default:
printf("\n Wrong Input");
}
} while (choice != 5);
}
int math() {
int choice, item4;
do {
printf("Stack Operation \n");
printf("\n 1. Add A New Assignment");
printf("\n 2. Remove the latest Completed Assignment ");
printf("\n 3. View the latest Pending Assignment ");
printf("\n 4. View All the pending Assignments");
printf("\n 5. Exit Code");
printf("\n Enter Your Choice");
scanf("%d", & choice);
switch (choice) {
case 1:
printf("\n Enter the Number of Days Left for the Assignment to be Submitted");
scanf("\n %d", & item4);
push(st4, item4);
sort(st4);
break;
case 2:
item4 = pop(st4);
printf("\n Removed Assignment is: \n %d", item4);
break;
case 3:
item4 = peek(st4);
printf("\n The Latest Assignment to be Submitted is:%d", item4);
break;
case 4:
display(st4);
break;
case 5:
printf("Exited");
break;
default:
printf("\n Wrong Input");
}
} while (choice != 5);
}
void push(int st[], int item) {
if (top == MAXSIZE - 1) {
printf("\n You Have a lot of Assignments Due, GET WORKING!!!");
} else {
top = top + 1;
st[top] = item;
}
}
int pop(int st[]) {
int item;
if (top == -1) {
printf("Great Work No Assignment Are Pending");
return 0;
} else {
item = st[top];
top = top - 1;
}
return item;
}
int peek(int st[]) {
int item;
if (top == -1) {
printf("Great Work No Assignment Are Pending");
return 0;
} else {
item = st[top];
return item;
}
}
void display(int st[]) {
if (top == -1) {
printf("Great Work No Assignment Are Pending");
} else {
for (int i = top; i >= 0; i--) {
printf("\n%d\n", st[i]);
}
}
}
void sort(int st[]) {
int tmp, i, j;
for (int i = top; i >= 0; i--) {
for (j = i + 1; j <= top; j++) {
if (st[i] < st[j]) {
tmp = st[j];
st[j] = st[i];
st[i] = tmp;
}
}
}
}
I would recommend that you use a structure for your stacks that contains the array and the top value e.g.
typedef struct{
int stack[MAXSIZE];
int top;
}stack_t;
Then you can declare your stacks and initialize them:
stack_t st1 = {{0}, -1};
stack_t st2 = {{0}, -1};
stack_t st3 = {{0}, -1};
stack_t st4 = {{0}, -1};
or place them in an array
stack_t stacks[4]={
{{0}, -1},
{{0}, -1},
{{0}, -1},
{{0}, -1}, };
You need to declare your functions as follows and update their implementation accordingly:
void push(stack_t* st, int item);
int pop(stack_t* st);
int peek(stack_t st);
void display(stack_t st);
void sort(stack_t* st);
void dsgt(void);
void cg(void);
void math(void);
void ds(void);
The push, pop, sort functions then all use pointers to stack_t e.g.
int pop(stack_t* st) {
int item =0;
if (st->top == -1) {
printf("Great Work No Assignment Are Pending");
} else {
item = st->stack[st->top];
st->top = st->top - 1;
}
return item;
}
display and peek function are okay with stack_t parameter as they don't change it.
If you place all of your stack structures in an array, you then can use a global variable as an index into your stack_t array and reduce the duplication of your choice code.

using the function findMovie for the second time, visual studio throws exception

visual studio throws an exception to the second instance of find movie under case c, it says it has not been initialized yet although in the local windows it shows the struct with info inside?Run-Time Check Failure #3 - The variable 'tempMovie' is being used without being initialized. this only happens the second time i try to use the struct tempMovie
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define CONST 100
typedef struct movies {
char title[30];
char UPC[12];
int qnty;
double price;
}movies;
void sortMovies(struct movies main[], int nOfMovies);
void changeMovie(struct movies main);
int findMovie(struct movies main, int nOfMovies, struct movies tempMovie);
struct movies newMovie();
int main()
{
int nOfMovies = 0, findR, stop = 0, nOfMoviesArr[CONST];
char decider;
while (stop != 1)
{
movies main[CONST];
movies tempMovie;
printf("(A)dd a new movie\n(C)hange a Movie's Information \n(D)elete a Movie \n(L)ist All Movies\n(Q)uit\n\n");
scanf(" %c", &decider);
switch (decider)
{
case 'a':
case 'A':
tempMovie = newMovie();
findR = findMovie(main[nOfMovies], nOfMovies, tempMovie);
if (findR == -1)
{
nOfMovies += findR;
break;
}
else
main[nOfMovies] = tempMovie;
nOfMovies++;
break;
case 'c':
case 'C':
findR = findMovie(main[nOfMovies], nOfMovies, tempMovie);
changeMovie(main[findR]);
break;
case 'd':
case 'D':
break;
case 'l':
case 'L':
sortMovies(main, nOfMovies);
break;
case 'q':
case 'Q':
return 0;
break;
default:
printf("An invalid option was selected!");
}
}
}
struct movies newMovie() {
movies new;
printf("enter movie upc:");
scanf("%s", &new.UPC);
printf("enter movie title:");
scanf("%s", &new.title);
printf("enter movie qauntity:");
scanf("%d", &new.qnty);
if (new.qnty <= 0)
{
printf("quanitity must be greater than 0");
printf("enter movie qauntity:");
scanf("%d", &new.qnty);
}
printf("enter movie price:");
scanf("%lf", &new.price);
if (new.price <= 0)
{
printf("price must be greater than 0");
printf("enter movie price:");
scanf("%lf", &new.price);
}
return new;
}
int findMovie(struct movies main, int nOfMovies, struct movies tempMovie)
{
int count;
for (count = 0; count < nOfMovies; count++)
{
if (tempMovie.UPC != main.UPC)
{
return count;
}
else
{
printf("error");
return -1;
}
}
}
void changeMovie(struct movies main)
{
char decider;
printf("would you like to change the value? y or no input");
switch (decider)
{
case 'y':
case 'Y':
printf("enter movie title:");
scanf("%c", main.title);
printf("enter movie qauntity:");
scanf("%d", main.qnty);
if (main.qnty <= 0)
{
printf("quanitity must be greater than 0");
printf("enter movie qauntity:");
scanf("%d", main.qnty);
}
printf("enter movie price:");
scanf("%lf", main.price);
if (main.price <= 0)
{
printf("price must be greater than 0");
printf("enter movie price:");
scanf("%lf", main.price);
}
break;
default:
printf("An invalid option was selected!");
}
}
void sortMovies(struct movies main[], int nOfMovies)
{
int i, sflag, count = 0;
char* temp;
do
{
sflag = 0;
for (i = 1; i < nOfMovies; i++)
{
if (main[i - 1].title > main[i].title)
{
temp = main[i - 1].title;
strcpy(main[i - 1].title, main[i].title);
strcpy(main[i].title, temp);
sflag = 1;
}
}
count++;
} while (sflag);
for (i = 0; i < nOfMovies; i++)
{
printf("%s\t%s\t%d\t%.2lf\n", main[i].title, main[i].UPC, main[i].qnty, main[i].price);
}
}```
Error may be due to any or many the following reasons:
In changeMovie(), the variable decider is not scanned.
In scanf(), use a spaces omitted before "%c" and "%s" (Use " %c" and " %s")
In changeMovie(), syntax error while scanning (Use &main.qnty instead of main.qnty)
The function changeMovie() is changing the formal parameters and not altering the actual parameters
Also, there are a few logical errors. For instance, in the line if (new.price <= 0) a while should be used instead of an if

*Beginner *C confusing me with Warnings

so I declared a float named price and when I try to compile it I get a waring that it would be a double, can someone tell me why C think that this is a double ?
B3N2.c:37: warning: format specifies type 'float *' but the argument has type 'double' [-Wformat]
scanf("%f", la[i].preis);
int main(int argc, char *argv[]) {
struct Lager {
char artikel[200];
int anzahl;
float preis;
} la[200];
printf("Wilkommen bei Lagerverwaltung 97\n\n\nWas möchten sie tun ?\n");
int exit = 0;
int x,v;
int f = 1;
int i = 0;
char ques;
int end;
do {
printf("\n(1)Artikel hinzufügen\n(2)Artikel entnehmen.\n(3)Eintrag suchen.\n(4)Lager ausgeben.\n(5)Exit\n");
scanf("%x",&x);
switch (x) {
case 1://add
do {
printf("\nGebe den namen des Produkts an: ");
scanf("%s", la[i].artikel);
printf("\nAnzahl der verfügbaren Produkte: ");
scanf("%i", &la[i].anzahl);
printf("\ngib den preis des artikels an: ");
scanf("%f", la[i].preis);
printf("\n\nWeiteres Produkt hinzufügen ? (J/N)");
scanf("%s", &ques);
switch (ques) {
case 'J':
v++;
f++;
break;
case 'N':
end = 1;
v = 0;
break;
default:
printf("Falsche Eingabe\n");
break;
}
} while (end != 1);
if (v >= 2) {
printf("Produkt wurde Erfolgreich hinzugefügt\n\n");
}else {
printf("Produkte wurden Erfolgreich hinzugefügt\n\n");
}
break;
sorry for the dumb question but I tryed to fix it and now i'm totally Overwhelmed
scanf() needs a pointer to the space (that is, the variable) where the scanned value will be stored. But your call hands over the value of la[i].preis which is a float that is cast automatically into a double.
Change your code to give the address of la[i].preis to scanf() by using the & operator like this:
scanf("%f", &la[i].preis);

Array has incomplete element type

I try to give my Void function a struct but when I do this I get a errors.
first:error: subscript of pointer to incomplete type 'struct Lager'.
and a warning:warning: incompatible pointer types passing 'Lager (*)[200]' to parameter of type 'struct Lager *' [-Wincompatible-pointer-types].
and
warning: declaration of 'struct Lager' will not be visible outside of this function [-Wvisibility].
I already asked my fellow students but no-one of them could help me.
void case1(struct Lager *wh, int l){
int end;
int v;
char ques;
do {
printf("\nEnter the product name: ");
scanf("%s", *(wh[l]).artikel);
printf("\nAmount of products: ");
scanf("%i", &*(wh[l]).anzahl);
printf("\n\nAdd another product ? (Y/N)");
// add a space before % to skip leading whitespace
scanf(" %c", &ques);
switch (ques) {
case 'Y':
v++;
l++;
break;
case 'N':
end = 1;
v = 0;
l++;
break;
default:
printf("Wrong entry\n");
end = 1;
break;
}
} while (end != 1);
if (v >= 2) {
printf("Product added successfully\n\n");
}else {
printf("Products have been successfully added\n\n");
}
}
int main() {
typedef int item;
typedef char max;
typedef int GanzvieleBuchstaben;
//unien wär die bessere variante (eigentlich). weil platzsparender
typedef struct managementtool
{
max artikel[200];
item anzahl;
}Lager;
Lager lager[200];
printf("Welcome to Warehouse Management 97\n\n\nWhat do you want to do ?\n");
GanzvieleBuchstaben a,l,f,i,v,x,p,c,я,и,ii;
int exit, end, all;
int b = 201;
char ques, nu1;
char find [100];
do {
printf("\n(1)Add article\n(2)Edit article.\n(3)Search entry.\n(4)Show stock.\n(5)Exit\n");
scanf("%i",&x);
switch (x) {
case 1://add
case1(&lager, l);
*Irrelevant stuff down there*

Having trouble with C pointers

First of all, im a begginer C programmer, studying in a university. I need to do a program that is kinda like a data base. Thus far i have made the function to input new data, it works, but compiler shows a few warning and notes, i tried a lot of things, but nothing helped. Here is the code:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
struct subjects{
char *subjName[100];
char *lectName[100];
char *lectSurname[100];
double credits;
double studentnum;
}subj;
char subjectname[][100];
char lecturername[][100];
char lecturersurname[][100];
int credits[100];
int students[100];
int newentry=0;
void listInput();
void listEdit();
void listDelete();
void listPrint();
int userChoice();
int main() {
int select;
int r=1;
while(r!=0){
select=userChoice();
switch(select){
case 1:
listPrint();
break;
case 2:
listInput();
break;
case 3:
listDelete();
break;
case 4:
listEdit();
break;
case 0:
r=0;
break;
}
}
return 0;
}
int userChoice(){
int choice,input=0,check;
printf("(1). View all the data\n");
printf("(2). Enter new data\n");
printf("(3). Delete data\n");
printf("(4). Edit data\n");
printf("(0). Exit\n");
printf("-----------------------------\n");
while(input!=1){
printf("Enter your choice\n");
scanf("%d", &choice);
if(choice>4 || choice<0){
printf("Invalid input \n");
}
else
input = 1;
}
return choice;
}
void listPrint(){
for(int i=0; i<newentry; i++){
printf("%d, %s, %s, %s, %d, %d\n",i+1, subjectname[i], lecturername[i], lecturersurname[i], credits[i], students[i]);
}
}
void listInput(){
int i;
char firstLetter,term;
printf("Enter the name of the subject \n");
while(scanf("%s", &subj.subjName)!=1)
printf("Error, subject name must be a text ");
printf("Enter the name of the lecturer \n");
int valid=0;
while(valid!=1){
while(scanf("%s", &subj.lectName)!=1)
printf("Error, lecturer's' name must be a text ");
firstLetter=*subj.lectName;
if(firstLetter>65 && firstLetter<90 && isalpha(firstLetter)){
valid=1;
}
else
printf("Error, lecturer's name must start with a capital letter, try again \nEnter the name of the lecturer \n");
}
valid=0;
printf("Enter the surname of the lecturer\n");
while(valid!=1){
while(scanf("%s", &subj.lectSurname)!=1)
printf("Error, lecturer's surname must be a text ");
firstLetter=*subj.lectSurname;
if(firstLetter>65 && firstLetter<90 && isalpha(firstLetter)){
valid=1;
}
else
printf("Lecturer's surname must start with a capital letter, try again \nEnter the surname of the lecturer \n");
}
printf("Enter the amount of credits in course \n");
while(1){
scanf("%lf", &subj.credits);
if(subj.credits<0 || subj.credits != (int)subj.credits)
printf("Error, amount of credits must be a positive integer, try again \n");
if(subj.credits>0 && subj.credits == (int)subj.credits)
break;
}
printf("Enter the number of students \n");
while(1){
scanf("%lf", &subj.studentnum);
if(subj.studentnum<0 || subj.studentnum != (int)subj.studentnum)
printf("Error, number of students must be a positive integer,try again \n");
if(subj.studentnum>0 && subj.studentnum == (int)subj.studentnum)
break;
}
printf("Added a new entry.\n\n");
strncpy(subjectname[newentry], subj.subjName, 99);
strncpy(lecturername[newentry], subj.lectName, 99);
strncpy(lecturersurname[newentry], subj.lectSurname, 99);
credits[newentry]=subj.credits;
students[newentry]=subj.studentnum;
newentry++;
}
void listDelete(){
printf("33333333333");
}
void listEdit(){
printf("4444444");
}
And the warnings:
In function 'listInput':
96 14 [Warning] assignment makes integer from pointer without a cast
108 14 [Warning] assignment makes integer from pointer without a cast
132 33 [Warning] passing argument 2 of 'strncpy' from incompatible pointer type
79 9 [Note] expected 'const char * restrict' but argument is of type 'char **'
133 34 [Warning] passing argument 2 of 'strncpy' from incompatible pointer type
79 9 [Note] expected 'const char * restrict' but argument is of type 'char **'
134 37 [Warning] passing argument 2 of 'strncpy' from incompatible pointer type
79 9 [Note] expected 'const char * restrict' but argument is of type 'char **
At top level:
14 6 [Warning] array 'subjectname' assumed to have one element
15 6 [Warning] array 'lecturername' assumed to have one element
16 6 [Warning] array 'lecturersurname' assumed to have one element
What can i do to fix those warnings? The program works just fine, but i cant pass if i dont fix the warnings.
Here is a better way to do things
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
char *subjName;
char *lectName;
char *lectSurname;
double credits;
double num_students;
}Subject;
typedef struct{
Subject **subjs;
int num_subjs;
}Subjects;
void listInput();
void listEdit();
void listDelete();
void listPrint();
int userChoice();
int main() {
Subjects *subjects = malloc(sizeof(Subjects));
subjects->num_subjs = 0;
subjects->subjs = NULL;
int r=1;
while(r!=0){
int select=userChoice();
switch(select){
case 1:
listPrint(subjects);
break;
case 2:
listInput(&subjects);
break;
case 3:
listDelete();
break;
case 4:
listEdit();
break;
case 0:
r=0;
break;
}
}
return 0;
}
int userChoice(){
int choice,input=0,check;
printf("(1). View all the data\n");
printf("(2). Enter new data\n");
printf("(3). Delete data\n");
printf("(4). Edit data\n");
printf("(0). Exit\n");
printf("-----------------------------\n");
while(input!=1){
printf("Enter your choice\n");
scanf("%d", &choice);
if(choice>4 || choice<0){
printf("Invalid input \n");
}
else
input = 1;
}
return choice;
}
void listPrint(Subjects *subjects){
for(int i=0; i< subjects->num_subjs; i++){
printf("%d, %s, %s, %s, %d, %d\n",i+1, subjects->subjs[i]->subjName, subjects->subjs[i]->lectName, subjects->subjs[i]->lectSurname, subjects->subjs[i]->credits, subjects->subjs[i]->num_students);
}
}
char *getln()
{
char *line = NULL, *tmp = NULL;
size_t size = 0, index = 0;
int ch = 1;
while (ch) {
ch = getc(stdin);
if (ch == '\n')
ch = 0;
if (size <= index) {
size += 1;
tmp = realloc(line, size);
if (!tmp) {
free(line);
line = NULL;
break;
}
line = tmp;
}
line[index++] = ch;
}
return line;
}
int isText(char *str,char *name){
for(int i = 0; i < strlen(str);i++){
if((str[i]<'A' || str[i]>'z') && str[i]!=' '){
printf("Error, %s must be a text \n",name);
return 0;
}
}
return 1;
}
int isNegative(int n){
if(n < 0){
printf("Error, number of students must be a positive integer,try again \n");
return 1;
}
return 0;
}
void listInput(Subjects **p_subjects){
while ( getchar() != '\n' );
Subject *new_subj = malloc(sizeof(Subject));
new_subj->subjName = NULL;
new_subj->lectName = NULL;
new_subj->lectSurname = NULL;
new_subj->credits = 0;
new_subj->num_students = 0;
do{
printf("Enter the name of the subject \n");
new_subj->subjName = getln();
}while(!isText(new_subj->subjName,"Subject name"));
do{
printf("Enter the name of the lecturer \n");
new_subj->lectName = getln();
}while(!isText(new_subj->lectName,"Lecturer's name"));
do{
printf("Enter the surname of the lecturer\n");
new_subj->lectSurname = getln();
//Convert to uppercase
new_subj->lectSurname[0] &= '_';
}while(!isText(new_subj->lectSurname,"Lecturer's surname"));
do{
printf("Enter the number of credits \n");
scanf("%d", &new_subj->credits);
}while(isNegative(new_subj->num_students));
do{
printf("Enter the number of students \n");
scanf("%d", &new_subj->num_students);
}while(isNegative(new_subj->num_students));
(*p_subjects)->subjs = realloc((*p_subjects)->subjs,sizeof(Subject*)*(++(*p_subjects)->num_subjs));
(*p_subjects)->subjs[(*p_subjects)->num_subjs-1] = new_subj;
printf("Added a new entry.\n\n");
}
void listDelete(){
printf("33333333333");
}
void listEdit(){
printf("4444444");
}

Resources