point to a structure member

This is a discussion on point to a structure member within the C Programming forums, part of the General Programming Boards category; hi can someone help me with this: Code: #include <stdio.h> #include <conio.h> #include <string.h> #include "struct.h" //#include "database.txt" /*****function declaration*****/ ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103

    point to a structure member

    hi
    can someone help me with this:

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include "struct.h"
    //#include "database.txt"
    
    /*****function declaration*****/
    void getcmd(char*);
    void getpass(char*);
    void add_std();
    void inf_viewer();
    void show_list(student*);
    
    /*****main block*****/
    int main()
    {
    	/* variables define here */
    	student std[2];
    	char lgn, cmd, std_slct, name, tpass[9], spass[9];
    	int i, j;
    	
    	/**********************************************/
    	/* std 1 */
    
    	strcpy(std[0].name,"Behzad Shabani");
    	strcpy(std[0].std_num,"860343061");
    	std[0].quiz[0] = 18.5;
    	std[0].quiz[1] = 17;
    	std[0].quiz[2] = 19;
    	std[0].exercises[0] = 20;
    	std[0].exercises[1] = 18;
    	std[0].exercises[2] = 20;
    	std[0].project[0] = 20;
    
    	/* std 2 */
    	strcpy(std[1].name,"Sepehr Safi");
    	strcpy(std[1].std_num,"860343121");
    	std[1].quiz[0] = 17.5;
    	std[1].quiz[1] = 15;
    	std[1].quiz[2] = 13;
    	std[1].exercises[0] = 20;
    	std[1].exercises[1] = 19;
    	std[1].exercises[2] = 17;
    	std[1].project[0] = 18;
    
    	/* std 3 */
    	strcpy(std[2].name,"Arsalan Barazesh");
    	strcpy(std[2].std_num,"860343030");
    	std[2].quiz[0] = 17;
    	std[2].quiz[1] = 15.5;
    	std[2].quiz[2] = 14;
    	std[2].exercises[0] = 20;
    	std[2].exercises[1] = 19;
    	std[2].exercises[2] = 18;
    	std[2].project[0] = 17;
    	/**********************************************/
    	
    	/* starting program */
    	clrscr();
    	printf("Welcome To The Student Database Manager!");
    	printf("\nPlease select if you want to login as Student or teacher. (S/T):");
    	getcmd(&lgn);
    	
    	/* checking the command */
    	switch (lgn){
    		case 'T':
    		case 't':
    
    			/* start of teacher function */
    			printf("\n\nYou loged in as Teacher.");
    			
    			/* checking teacher password */
    			printf("\nPlease enter your password:");
    			getpass(tpass);
    			while (strcmp(tpass,"p") != 0){
    				printf("\n\aWrong password!\nPlease try again:");
    				getpass(tpass);
    			}
    			
    			/* getting a command */
    			printf("\n\nSelect one of these action:");
    			printf("\n1. Add a student");
    			printf("\n2. Delete a student");
    			printf("\n3. Edit a student");
    			printf("\nEnter number here:");
    			getcmd(&cmd);
    			
    			/* checking the command */
    			switch (cmd){
    				case '1':
    					printf("\n\nadd a student here!");
    					add_std();
    				break;
    				case '2':
    					printf("\n\ndelete a student!");
    					show_list(std);
    					//del_std();
    				break;
    				case '3':
    					printf("\n\nedit a student");
    					//edt_std();
    				break;
    				default:
    					printf("\n\n\aUnknown command!");
    				break;
    			}
    			
    			//add_inf();
    			/* end of teacher function */
    
    		break;
    		case 'S':
    		case 's':
    
    			/* start of student function */
    			clrscr();
    			printf("You loged in as Student.");
    			printf("\nfind your name from below and enter its number.\n");
    			for (i = 0; i<3; i++){
    				printf("\n%d.", i + 1);
    				puts(std[i].name);
    				printf("\n");
    			}
    			printf("Now enter the number: ");
    			scanf("%d", &j);
    			i = j - 1;
    			printf("\nWelcome ");
    			puts(std[i].name);
    			printf("\nplease enter your password. (default is your student number): ");
    			getpass(spass);
    			inf_viewer();
    			/* end of student function */
    			
    		break;
    		default:
    			printf("\n\aUnknown command!");
    
    		break;
    	}
    
    	return 0; // the program succesfully ended
    }
    
    /*****function definition*****/
    
    /*****getcmd(): getting commands*****/
    // WRITTEN BY: Behzad Shabani
    // CALLING FUNCTION: getcmd(&cmd);
    
    void getcmd(char* cmd){
    
    	/* function variables */
    	char ch_ctrl;
    	int flag = 0;
    
    	/* function process*/
    	while (flag == 0){
    		*cmd = getche();
    		if (*cmd == '\b'){
    			printf("\a ");
    			continue;
    		}
    		else if (*cmd == '\r'){
    			printf("\a");
    			continue;
    		}
    		while (ch_ctrl != '\r'){
    			ch_ctrl = getch();
    			if (ch_ctrl == '\b'){
    				printf("\b");
    				break;
    			}
    			else if (ch_ctrl != '\r'){
    				printf("\a");
    			}
    		}
    		if (ch_ctrl == '\b')
    			continue;
    		flag = 1;
    	}
    
    }
    
    /*****getpass(): getting password*****/
    // WRITTEN BY: Behzad Shabani
    // CALLING FUNCTION: getpass(pass);
    
    void getpass(char* pass){
    
    	/* function variables */
    	char ch;
    	int flag = 0, i = 0;
    
    	/* function process*/
    	while(flag == 0){
    		ch = getch();
    		if (ch == '\r'){
    			if (i != 0){
    				pass[i] = NULL;
    				flag = 1;
    				continue;
    			}
    			printf("\a");
    			continue;
    		}
    		else if (ch == '\b'){
    			printf("\b");
    			i--;
    			continue;
    		}
    		printf("*");
    		pass[i] = ch;
    		i++;
    	}
    
    }
    
    /****** add_std() : adding student into the database by teacher ******/
    //WRITTEN BY : Sepehr Safi
    //CALLING FUNCTION: add_std();
    
    
    void add_std(){
    
    	/* Function Variables */
    	char* name,*std_num;
    	float quiz[3],exercises[5],project[2];
    	int m=0,n=0,b=0,v;
    	
    	/* Function Process */
    	printf("\nPlease Enter The Student's Name :");
    	gets(name);
    	printf("\nStudent's Name Is :");
    	puts(name);
    	printf("\nPlease Enter The Student's Number :");
    	gets(std_num);
    	printf("\nStudent's Number Is :");
    	puts(std_num);
    
    	do{
    		printf("\nEnter The Quiz[%d] point:",m+1);
    		scanf("%f",&quiz[m]);
    		printf("\nThe Quiz[%d] point Is :%f",m,quiz[m]);
    		m++;
    	}while(m<3);
    
    	do{
    		printf("\nEnter The Exercise Series[%d] point:",n);
    		scanf("%f",&exercises[n]);
    		printf("\nThe Exercise Series[%d] point Is :%f",n,exercises[n]);
    		n++;
    	}while(n<5);
    
    	do{
    		printf("\nEnter The Project[%d] point:",b);
    		scanf("%f",&project[b]);
    		printf("\nThe Project[%d] point Is :%f",b,project[b]);
    		b++;
    	}while(b<2);
    
    }
    
    /****** inf_viewer(): showing each student's information ******/
    //WRITTEN BY: Sepehr Safi
    //CALLING FUNCTION: inf_viewer();
    void inf_viewer(){
    
    	/* Function Variables */
    	char* name;
    	float quiz[3],exercises[5],project[2];
    	int l=0,k=0,h=0,i;
    	student* std;
    
    	/* Function Process */
    	printf("\nYou're The Student Number[%d]\n", i+1);
    	while(l<3){
    		printf("\nThe Numbers Of Your Quizes Are:");
    		printf("%f",std[i].quiz[l]);
    		printf("\n");
    		l++;
    	}
    	while(k<5){
    		printf("\nThe Numbers Of Your Exercises Are:");
    		printf("%f",std[i].exercises[k]);
    		printf("\n");
    		k++;
    	}
    	while(h<2){
    		printf("\nThe Numbers Of Your Projects Are:");
    		printf("%f",std[i].project[h]);
    		printf("\n");
    		h++;
    	}
    	getch();
    }
    
    /****** show_list() : show students name 15 by 15 ******/
    //WRITTEN BY : Behzad Shabani
    //CALLING FUNCTION: show_list();
    
    void show_list(student* std){
    
    	/* Function variables */
    	int i = 0, j = 0, flag = 0;
    	char cmd;
    
    	/* Function process */
    	do{
    		while(i < 15 ){
    			printf("\n%d.", i + j + 1);
    			puts((*std + i + j)-> name);
    			i++;
    		}
    		printf("\nDo you want to continue the list? (Y/N):");
    		getcmd(&cmd);
    		switch (cmd){
    			case 'y':
    			case 'Y':
    				printf("\n\ncontinuing");
    				continue;
    			break;
    			case 'n':
    			case 'N':
    				printf("\n\n list ended");
    				flag = 1;
    			break;
    			default:
    				printf("\n\n\aUnknown command!");
    			break;
    		}
    		j += 15;
    		i = 0;
    	}while (flag == 0);
    
    }
    and this is the struct.h file:
    Code:
    /*   struct.h
     
         defining new types, students and teacher, made of structure
    
         written by: Behzad Shabani
         Improved by: Sepehr Safi
    */
    
    
    
    #if !defined(__STRUCT_H)
    
    #define __STRUCT_H
    
    
    typedef struct student{
    
    	char name[45];
    	
    	char std_num [9];
    	
    	float quiz[3];
    	
    	float exercises[5];
    	
    	float project[2];
    
    	};
    
    
    typedef long int teaherpass[9];
    
    
    #endif

  2. #2
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103
    if you see other problem in my code please tell and help me.
    thanks

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Pointers are not storage units.
    http://cpwiki.sf.net/A_pointer_on_pointers
    And never, never, never EVER use gets.
    http://cpwiki.sf.net/Gets
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103
    Quote Originally Posted by Elysia View Post
    Pointers are not storage units.
    http://cpwiki.sf.net/A_pointer_on_pointers
    And never, never, never EVER use gets.
    http://cpwiki.sf.net/Gets
    so what should I do to show a structure member in afunction?

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Code:
    typedef struct student{
    	char name[45];
    	char std_num [9];
    	float quiz[3];
    	float exercises[5];
    	float project[2];
    };
    This is also incorrect. The struct is missing a proper typedef name.

    Quote Originally Posted by behzad_shabani View Post
    so what should I do to show a structure member in afunction?
    You haven't made yourself clear. You need to elaborate on that one and fix the previousmentioned errors first.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by behzad_shabani View Post
    if you see other problem in my code please tell and help me.
    Have you actually tried to compile and run this code? Post specific questions and fix things that we bring up first, before moving onto the next thing.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment to a structure array member
    By sj999 in forum C Programming
    Replies: 4
    Last Post: 05-22-2008, 11:13 PM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Zeroing out member arrays in a Structure
    By manofsteel972 in forum C++ Programming
    Replies: 4
    Last Post: 03-26-2004, 02:50 AM
  5. Replies: 2
    Last Post: 11-28-2003, 10:50 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21