Find specifi ID info using structure - c

I could not find specific student_Id info from a rage of given info.suppose, I am taking input from 1 to 100 all student info. now I want to find only 50th number student_ID all info.i could not do it. how it possible.here show my code. what's wrong with my code and how fixed it.thanks
# include <string.h>
# include <stdio.h>
# include <stdlib.h>
# include <conio.h>
struct student
{
char student_id[100];
char name[10];
int m[50],credit[100],sub[100];
int total,sumCredit;
float GP[100];
char result[5];
char grade[100][10];
double sumCreditxGP;
}*p,*s;
float gradesToGP(float marks);
char *markToG(float gp);
void lines(void);
void main()
{
int i,j,l,n,sub,k;
// clrscr();
printf("Enter the no. of students : ");
scanf("%d",&n);
p=(struct student*) malloc(n*sizeof(struct student));
//printf("%d",p);
//exit(0);
s=p;
for(i=0; i<n; i++)
{
printf("Enter a student id : ");
scanf("%s",&p->student_id);
printf("Enter a name : ");
scanf("%s",&p->name);
printf("Enter the no. of subject : ");
scanf("%d",&p->sub[i]);
p-> total=0;
p-> sumCredit=0;
p-> sumCreditxGP=0;
l=0;
for(j=0; j<p->sub[i]; j++)
{
one:
printf("Enter Marks of %d Subject\t%d : ",j+1,p->sub[i]);
scanf("%d",&p->m[j]);
printf("Enter Credit Hour: ");
scanf("%d",&p->credit[j]);
p->GP[j] = gradesToGP((float)p->m[j]);
strcpy(p->grade[j],markToG(p->m[j]));
if((p->m[j])>100)
{
printf("---------------Wrong Value Entered-----------\n");
goto one;
}
p->total+=p->m[j];
p->sumCredit+=p->credit[j];
p->sumCreditxGP+=p->credit[j]*p->GP[j];
if(p->m[j]<40)
l=1;
}
if(l==0)
strcpy(p->result,"PASS");
else
strcpy(p->result,"FAIL");
p++;
}
char search_id[10];
printf("Enter id to find specific student ");
scanf("%s",search_id);
//PROBLEM START HERE
for(i=0; i<n; i++)
{
if(p->student_id==search_id){
printf("found");
printf("%s",s->student_id);
}else{
printf("Not found");
}
s++;
}
getch();
}
float gradesToGP(float marks)
{
if (marks>=80 && marks<=100)
{
return(float)4.00;
}
else if (marks>=75 && marks<=79)
{
return(float)3.67;
}
else if (marks>=70 && marks<=74)
{
return(float)3.33;
}
else if (marks>=65 && marks<=69)
{
return(float)3.00;
}
else if (marks>=60 && marks<=64)
{
return(float)2.67;
}
else if (marks>=55 && marks<=59)
{
return(float)2.33;
}
else
{
return(float)5.00;
}
}
char *markToG(float marks)
{
if (marks>=80 && marks<=100)
{
return "A";
}
else if (marks>=75 && marks<=79)
{
return "A-";
}
else if (marks>=70 && marks<=74)
{
return "B+";
}
else if (marks>=65 && marks<=69)
{
return "B";
}
else if (marks>=60 && marks<=64)
{
return "B-";
}
else if (marks>=55 && marks<=59)
{
return "C+";
}
else
{
return "null";
}
}
void lines(void)
{ printf("**********************************************************************");
}
Please tell me how can I fixed it.thanks in advanced.

if(p->student_id==search_id){
printf("found");
Now, that's not how you compare strings in C. Use the strcmp() function for string comparison. You may read about strcmp() here.

The issue is your equality check: if(p->student_id==search_id)
Because both student_id and search_id are char arrays, the types will decay to pointers (char *) and this will never work as you expect. Instead, you need to use strcmp (or better, strncmp).
if(strncmp(p->student_id, search_id, 10) == 0) { /* equality */ }

Related

How do i send a string to another function using c?

i am trying to send a string that is "genrecode" back to my main function from the my other function known as char Vote. At first, i used the 'int' data type for the variable "genreCode" since its just integers but i wanted to see if i can send it as a string. How do i do that?
#include <stdio.h>
#include<string.h>
void View_Genre();
char Vote();
void View_Result(int, int, int);
void Exit(int, int , int);
int main ()
{
int option;
char genreCode[2];
int rock=1;
int pop=1;
int country=1;
int Trock=0;
int Tcountry=0;
int Tpop=0;
printf("WELCOME TO MUSIC GENRE VOTING SYSTEM\n");
printf("View Genre\n");
printf("Vote\n");
printf("View Result\n");
printf("Exit\n");
do
{
printf("\nInput option : ");
scanf("%d", &option);
switch(option)
{
case 1 : View_Genre();
break;
case 2 : genreCode=Vote();
if(strcmp(genreCode,"11")==0)
{
Trock=Trock+rock;
}
else if(strcmp(genreCode,"22")==0)
{
Tpop=Tpop+pop;
}
else if(strcmp(genreCode,"33")==0)
{
Tcountry=Tcountry+Tpop;
}
break;
case 3 : View_Result(Trock, Tpop, Tcountry);
break;
case 4 :Exit(Trock, Tpop, Tcountry);
}
}while(option==1 || option==2 || option==3);
printf("\nThank you.");
return 0;
}
void View_Genre()
{
printf("GENRE CODE\n");
printf("ROCK 11 \n");
printf("Pop 22 \n");
printf("Country 33 \n\n");
}
char Vote()
{
char genreCode[2];
printf("Enter genre code : ");
scanf("%d", &genreCode);
if(strcmp(genreCode,"11")==0)
{
printf("Your favourite genre is Rock");
}
else if(strcmp(genreCode,"22")==0)
{
printf("Your favourite genre is Pop");
}
else if(strcmp(genreCode,"33")==0)
{
printf("Your favourite genre is Country");
}
return genreCode;
}
void View_Result(int Nrock, int Npop, int Ncountry)
{
printf("GENRE VOTES\n");
printf(" Rock %d \n", Nrock);
printf(" Pop %d \n", Npop);
printf(" Country %d \n", Ncountry);
}
void Exit(int Nrock,int Npop,int Ncountry)
{
char popular[10];
if(Nrock!=0 || Npop!=0 || Ncountry!=0)
{
if(Nrock>Npop)
{
if(Nrock>Ncountry)
{
strcpy(popular,"Rock");
}
else
{
strcpy(popular,"Country");
}
}
else if(Npop>Ncountry)
{
strcpy(popular,"Pop");
}
else
{
strcpy(popular,"Country");
}
printf("The most popular music genre is : %s\n", popular);
}
else
{
printf("\nYou have not vote any genre\n");
}
}
The rest are all correct. I just need to find out how do i send back a string to my main function.
You could do this by allocating space in the heap for your genreCode variable.
//...
#include <stdlib.h>
//...
int main ()
{
// ...
//allocate space for 3 chars in the heap
//the third char stores the string terminator '\0'
char* genreCode = malloc(sizeof(char)*3);
// ...
do
{
//...
switch(option)
{
//...
case 2 : Vote(genreCode);
//...
break;
//...
}
}while(option==1 || option==2 || option==3);
//...
free(genreCode); //Free the allocated memory
return 0;
}
//...
void Vote(char* genreCode)
{
printf("Enter genre code : ");
scanf("%s", genreCode);
//...
}
//...
Reference:
malloc() - Tutorialspoint

Access a field of a structure that is within the union of it

This program must handle data that represents the products used in the factory. It is desired that the product represents the following characteristics: Code, description unit of measure, and price; In turn, if the product is imported you want to know the origin of the product, while if it is purchased in the square you want to know the name of the provider's phone number.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <stdbool.h>
#include <winsock.h>
void clrscr();
typedef enum {IMPORTED, LOCAL} type;
typedef struct
{
int code;
char description [20];
char MeasureUnit [5];
float price;
type discriminant;
union
{
char origin [20];
char destination [20];
int telephone;
} impoExpo;
} Product;
//this procedure fails
void loadProduct (Product *p)
{
printf("\nEnter the code:");
scanf("%d",&Product.code); //<-error: expected expression before 'Product'
fflush(stdin);
printf("\nEnter the description:");
scanf("%s",Product.description);
printf("Indicate the unit of measure:");
scanf("%s",Product.MeasureUnit);
printf("Enter the price:");
scanf("%f",&Product.price);
int i;
printf("\nInsert 1 if imported");
scanf("%d", &i);
if(i == 1)
{
p->discriminant = IMPORTED;
}
else
{
p->discriminant = LOCAL;
}
if(p->discriminant == IMPORTED)
{
printf("\nEnter source: ");
gets(p->impoExpo.origin);
}
else
{
printf("\nEnter the phone");
scanf("%d", &p->impoExpo.telephone);
}
}
//it is also
void showProduct (Product p)
{
printf("\nCode: %d", p.code); //<----- error: request for member 'code' in something not a structure or union
printf("\nDescription");
printf("%s", p.description);
printf("\nMeasurement unit:");
printf("%s", p.MeasureUnit);
printf("\nPrice:% .2f", p.price);
printf("\nType:");
if (p.discriminant == IMPORTED)
{
printf("Imported:");
printf("\nOrigin: %s", p.impoExpo.origin);
printf("%s", p.impoExpo.origin);
}
else
{
printf("Local:");
printf("\nTelephone: %d", p.impoExpo.telephone);
}
}
//this one also
bool areequal (Product p1, Product p2)
{
bool equal = false;
if ((p1.code == p2.code) && (p1.description == p2.description))
{
if ((p1.MeasureUnit == p2.MeasureUnit) && (p1.price == p2.price))
{
if (p1.discriminant == p2.discriminant)
{
if (p1.discriminant == IMPORTED)
{
if (p1.impoExpo.origin == p2.impoExpo.origin)
{
equal = true;
}
}
else
{
if (p1.impoExpo.telephone == p2.impoExpo.telephone)
{
equal = true;
}
}
}
}
}
return equal;
}
//this función ok
void copy (Product * const destination, const Product * const origin)
{
destination->code = origin->code;
(*destination->description) = (*origin->description);
(*destination->MeasureUnit) = (*origin->MeasureUnit);
destination->price = origin->price;
destination->discriminant = origin->discriminant;
if(destination->discriminant == IMPORTED)
(*destination->impoExpo.origin) = (*origin->impoExpo.origin);
else
destination->impoExpo.telephone = origin->impoExpo.telephone;
}
//and the latter also
int main ()
{
int option;
do
{
clrscr();
printf("Welcome to the program\n");
printf("Enter an option\n");
printf("1. Load a product\n");
printf("2. Show product\n");
printf("3. Check if two products are the same\n");
printf("0. Exit");
printf("Enter the option, and press ENTER");
scanf("%d",&option);
switch (option)
{
case 1:
loadProduct(&p);
getch();
break;
case 2:
showProduct(p);
getch();
break;
case 3:
printf("Enter the name of the product 1");
scanf("%d",&p1);
printf("Enter the name of the product 2");
scanf("%d",&p2);
printf("% d",areequal (p1, p2));
getch();
break;
}
} while (option != 0);
getch();
return 0;
}
void clrscr()
{
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord = {0, 0};
DWORD count;
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hStdOut, &csbi);
FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
SetConsoleCursorPosition(hStdOut, coord);
}
Product is a pointer, so you have to access its members using the arrow notation:
scanf("%d",&p->code); //<-error: expected expression before 'Product'

control may reach end of non-void function [-Wreturn-type]|

Ok, I have a code in C. Its noughts and crosses.
But i don't know about my 'sprawdzenie'(check).
IDE send me control may reach end of non-void function [-Wreturn-type].
I have no idea whats wrong but I know my code isn't beauty.
#include <stdio.h>
#include <stdlib.h>
char plansza[3][3];
void wczytaj()
{
printf(" %c |%c | %c\n",plansza[0][0],plansza[0][1],plansza[0][2]);
printf("-------\n");
printf(" %c |%c | %c\n",plansza[1][0],plansza[1][1],plansza[1][2]);
printf("-------\n");
printf(" %c |%c | %c\n",plansza[2][0],plansza[2][1],plansza[2][2]);
}
char sprawdzenie()
{
if((plansza[0][0]&&plansza[0][1]&&plansza[0][2] =='x' )||
(plansza[0][2]&&plansza[1][1]&&plansza[2][0]=='x') ||
(plansza[0][2]&&plansza[1][2]&&plansza[2][2]=='x') ||
(plansza[2][0]&&plansza[2][1]&&plansza[2][2]=='x') ||
(plansza[1][0]&&plansza[1][1]&&plansza[1][2]=='x') ||
(plansza[0][1]&&plansza[1][1]&&plansza[2][1]=='x') ||
(plansza[0][0]&&plansza[1][1]&&plansza[2][2]=='x') ||
(plansza[0][0]&&plansza[1][0]&&plansza[2][0]=='x'))
{
return 1;
}else if((plansza[0][0]&&plansza[0][1]&&plansza[0][2]=='o') ||
(plansza[0][0]&&plansza[1][0]&&plansza[2][0]=='o') ||
(plansza[0][0]&&plansza[1][1]&&plansza[2][2]=='o') ||
(plansza[0][1]&&plansza[1][1]&&plansza[2][1]=='o') ||
(plansza[1][0]&&plansza[1][1]&&plansza[1][2]=='o') ||
(plansza[2][0]&&plansza[2][1]&&plansza[2][2]=='o') ||
(plansza[0][2]&&plansza[1][2]&&plansza[2][2]=='o') ||
(plansza[0][2]&&plansza[1][1]&&plansza[2][0]=='o'))
{
return 2;
}
}
void gracz_x()
{
unsigned int i=3;
unsigned int j=3;
wczytaj();
printf("Podaj wiersz i kolumnę: ");
scanf("%u %u",&i,&j);
if(plansza[i][j]!='o')
plansza[i][j]='x';
else{
printf("Podaj poprawne współrzędne: ");
scanf("%u %u",&i,&j);
plansza[i][j]='x';
}
}
void gracz_o()
{
unsigned int i=3;
unsigned int j=3;
wczytaj();
printf("Podaj wiersz i kolumnę: ");
scanf("%u %u",&i,&j);
if(plansza[i][j]!='x')
plansza[i][j]='o';
else{
printf("Podaj poprawne współrzędne: ");
scanf("%u %u",&i,&j);
plansza[i][j]='o';
}
}
int main()
{
char wynik=0;
int kolejka = 0;
while(wynik==0)
{
if(kolejka==0)
{
gracz_o();
}else{
gracz_x();
}
kolejka=kolejka+1;
wynik=sprawdzenie();
if(kolejka==9){
printf("remis");
break;
}
}
wczytaj();
if(wynik==1)
{
printf("wygryw x\n");
}else if(wynik==2)
{
printf("wygryw o\n");
}
return 0;
}
It means that your function sprawdzenie is supposed to return char but it might not. You have an if and an else if. If neither of these conditions are met, the function will get to the end and not return anything. This isn't allowed because the function is expected to return char.
Add a default return value at the end or add an else block to prevent your function from ending without returning anything.

using two-dimensional array for store several strings

I wrote a function in C that search if a substring is in a string and it's OK, but when I use it in a array of strings I face an error. Please see this code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int substring(char*, char*);
main()
{
char student[100][7];
int counter = 0;
int finish =1;
char s1[4];
while(finish){
printf("please Enter Student Number: \n");
scanf("%s", student[counter]);
counter++;
printf("Do you want to exit? 1/0");
scanf("%d", &finish);
}
printf("Now, You can search in Student numbers\n");
printf("Enter a number to search: ");
scanf("%s", s1);
for(int i = 0; i < counter; i++){
printf("%d : %s\n", i, student[i]);
if(substring(student[i],s1) == 1)
printf("%s", student[i]);
}
getch();
}
int substring(char *s1,char *s2)
{
int f=0;
for(; *s1 !='\0';)
{
if(*s2=='\0')
break;
for(;*s2 !='\0';)
{
if(*s1==*s2)
{
f=1;
s1 ++;
s2 ++;
}
else
{
f=0;
s1++;
break;
}
}
}
if(f==0)
return 0;
else
return 1;
getch();
}
This is how you can test for substring using simple state machine:
int substring(const char *s1, const char *s2)
{
enum
{
search, start_match
} state = search;
const char *m;
while(*s1 != '\0')
{
switch(state)
{
case search:
if(*s2 == '\0')
return 0;
else if(*s2 == *s1)
{
state = start_match;
m = s2 + 1;
}
break;
case start_match:
if(*m == '\0')
return 1;
else if(*m != *s1)
state = search;
else
++m;
}
++s1;
}
return 0;
}
Also you may use the standard strstr function.

Stack of strings

Hi i have program here that accept int as value. i wanted to translate it to accept strings in array then. i have read about using struct but i couldnt get into it. i hope someone can help me getting into that without using struct i dont know where to start i want to keep this lines of code.
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
int top = 0;
int *stack = NULL;
int size = 0;
main()
{
int opt, num;
char cont[] = { 'y' };
clrscr();
/* <start Declaring Stack Size { */
printf("Stacking Program");
printf("\n\nData Size: ");
scanf("%d", &size);
printf("\n");
/* } end> */
/* <start Allocates size of stack { */
if(size > 0)
{
stack = malloc(size * sizeof(int));
if(stack == NULL)
{
printf("ERROR: malloc() failed\n");
exit(2);
}
}
else
{
printf("ERROR: size should be positive integer\n");
exit(1);
}
/* } end> */
while((cont[0] == 'y') || (cont[0] == 'Y'))
{
clrscr();
/* <start Main Menu { */
printf("Stacking Program");
printf("\n\nData Size: %d\n\n", size);
printf("MAIN MENU\n1. Pop\n2. Push\n3. Pick\n4. View\nChoose: ");
scanf("%d", &opt);
printf("\n");
switch(opt) {
case 1:
pop();
break;
case 2:
if(top==size)
{
printf("You can't push more data");
}
else
{
printf("Enter data for Stack[%d]: ", top+1);
scanf("%d", &num);
push(num);
}
break;
case 3:
pick();
break;
case 4:
view();
break;
default:
printf("Your choice is not on the list.");
break;
}
/* } end> */
printf("\n\nDo you want continue\(Y\/N\)?");
scanf("%s", &cont[0]);
}
free(stack);
}
pop()
{
int a;
loading();
if(top <= 0)
{
printf("Stack empty.");
return 0;
}
else
{
top--;
a=stack[top];
printf("\(Stack[%d] = %d\) removed.", top+1, a);
}
}
push(int a)
{
stack[top]=a;
top++;
loading();
}
pick()
{
loading();
if(top <= 0)
{
printf("Nothing to display.");
return 0;
}
else
{
printf("\(Stack[%d] = %d\) is the last data.", top, stack[top-1]);
}
}
view()
{
int i;
loading();
if(top <= 0)
{
printf("Nothing to display.");
return 0;
}
else
{
for(i=0;i<top;i++)
{
printf("Stack[%d] = %d\n", i+1, stack[i]);
}
}
}
loading()
{
float i, x;
float load;
int loadarea[] = { 5000, 10000, 15000, 20000, 25000, 30000 };
int percentLoad;
x=0;
load=0;
percentLoad = loadarea[random(5)];
gotoxy(26,11);
printf("[");
for(i=0;i<25;i++)
{
x = i+27;
gotoxy(x, 11);
printf("=");
delay(percentLoad);
gotoxy(51,11);
printf("]");
gotoxy(53,11);
load=(i/25)*104.5;
if(load>100)
load = 100.00;
printf("%.2f\%",load);
}
delay(60000);
for(i=0;i<60;i++) {
printf("\b \b");
}
printf("\n");
}
Easiest way is to convert your stack to store char* instead of int.
char **stack;
stack = malloc( size * sizeof(char*) );
Now, your push operation will accept a char* from some buffer that is storing the string that was just input, duplicate it with strdup, and store that new pointer in the stack.
typedef enum {
STACK_MEM_ERROR = -1,
STACK_FULL = 0,
STACK_OK = 1
} StackStatus;
StackStatus push(const char *str)
{
char *newstr;
if( top >= size ) return STACK_FULL;
newstr = strdup(str);
if( newstr == NULL ) return STACK_MEM_ERROR;
stack[top++] = newstr;
return STACK_OK;
}
When you pop a string, you just get a pointer.
char *pop()
{
if( top == 0 ) return NULL;
return stack[--top];
}
You are responsible for freeing that memory when you are finished with the pointer (by calling free).
char * val;
while( NULL != (val = pop()) )
{
printf( "I popped: %s\n", val );
free(val);
}

Resources