标题:C,function不能用
只看楼主
chaicai333
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2014-1-20
结帖率:50%
 问题点数:0 回复次数:5 
C,function不能用
小弟刚学C 请多多指教
这是我的作业
不知道为什么我的第三个function不可以用。。。就是要印出我输入过的资料不能出来

程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct node
{
    char *student_name;
    char *gender;
    char *course;
    int *student_number;
    struct node *next;
    struct node *prev;
}*newnode, *firstPtr,*temp,student;

void insert_new(char *name, char *gender, char *course, int *number){


    newnode = (struct node*)malloc(sizeof (struct node));
    newnode->student_name = name;
    newnode->gender = gender;
    newnode->course = course;
    newnode->student_number = number;
    newnode->next = NULL;

    if (firstPtr == NULL)
    {
        firstPtr = newnode;
    }
    else
    {

        temp = firstPtr;
        while (temp->next != NULL)
        {
            temp = temp->next;
        }
        temp->next = newnode;
        firstPtr = temp;
    }
    printf("Insert success\n");
    printf("Press any key to go back main menu\n");
    getch();
}

void insert_last(){

}

void print_begin(){
    if (firstPtr == NULL)
        printf("List is empty\n");
    else
    {
        struct node *tempdisplay;
        tempdisplay = firstPtr;

        while (tempdisplay != NULL)
        {
            printf("%s\n", tempdisplay->student_name);
            printf("%s\n", tempdisplay->gender);
            printf("%s\n", tempdisplay->course);
            printf("%d\n", tempdisplay->student_number);
            tempdisplay = tempdisplay->next;
        }
    }
    printf("\n\nPress any key to go back main menu\n");
    getch();
}

void print_last(){

}

void remove_student(){

}

main()
{
    firstPtr=NULL;
    char name;
    char gender;
    char course;
    int number;
    int main_choice;
    do{
        printf("\n");
        printf("\n");
        printf("\n");
        printf("               1.Add a new student to the beginning\n");
        printf("               2.Add a new student to the end\n");
        printf("               3.Print out the entire list from the beginning\n");
        printf("               4.Print out the entire list from the end\n");
        printf("               5.Remove a student from the list\n");
        printf("               6.Quit the program\n");
        printf("\n\nEnter the number of the options\n");
        scanf("%d", &main_choice);

        switch (main_choice){

        case 1: {
                    
                    printf("Enter the name of the student\n");
                    scanf("%s", &name);
                    printf("Enter the gender of the student\n");
                    scanf("%s", &gender);
                    printf("Enter the course of the student\n");
                    scanf("%s", &course);
                    printf("Enter the number of the student\n");
                    scanf("%d", &number);
                    insert_new(name,gender,course,number);
        }break;
        case 2: insert_last(); break;
        case 3: print_begin(); break;
        case 4: print_last(); break;
        case 5: remove_student(); break;
        case 6:{
                   printf("\n\n\nThe program is closing...\n...\n");
                   return 0;
                   break;
        }
        }
        system("cls");
    } while (main_choice != 6);
}


[ 本帖最后由 chaicai333 于 2014-4-13 03:25 编辑 ]
搜索更多相关主题的帖子: 资料 course function 
2014-04-12 17:12
xiaozi2013
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:四川成都
等 级:蒙面侠
威 望:6
帖 子:830
专家分:4331
注 册:2013-12-13
得分:0 
字好小!!


--整天敲那些破代码能找着媳妇儿吗。。。---
2014-04-12 17:37
chaicai333
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2014-1-20
得分:0 
回复 2楼 xiaozi2013
换粗给你了~
2014-04-13 03:25
fopen
Rank: 2
来 自:广东
等 级:论坛游民
帖 子:21
专家分:26
注 册:2014-2-11
得分:0 
main()前面不用加一些东西吗?

我只会一点C和HTML,叫我情何以堪!我要学php和java!
2014-04-13 08:11
ying8501
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:6
帖 子:1092
专家分:1446
注 册:2008-11-24
得分:0 
太扣扣了,0分贴呀。
char name;   //换成字符数组才能放串,下同
    char gender;
    char course;
...
  printf("Enter the name of the student\n");
                    scanf("%s", &name);
                    printf("Enter the gender of the student\n");
                    scanf("%s", &gender);
                    printf("Enter the course of the student\n");
                    scanf("%s", &course);
2014-04-13 08:18
chaicai333
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2014-1-20
得分:0 
回复 5楼 ying8501
不好意思 那个我漏了改
请帮我看一下这段应该怎样写?
我觉得这样assign value应该不对吧?
程序代码:
void insert_new(char name[], char xgender[], char xcourse[], int number){
    newnode = NULL;
    newnode = (struct node*)malloc(sizeof (struct node));
    newnode->student_name[40] = name;
    newnode->gender[40] = xgender;
    newnode->course[40] = xcourse;
    newnode->student_number = number;
    newnode->next = NULL;


还有为什么我fgets(name,100,stdin); 这个不会走的?
我expect output会printf("Enter the name of the student\n";
然后fgets(name,100,stdin); 给user输入
可是他直接跳到printf("Enter the gender of the student\n";这边
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct node
{
    char student_name[40];
    char gender[40];
    char course[40];
    int student_number;
    struct node *next;
    struct node *prev;
}*newnode, *firstPtr,*temp,student;

void insert_last(){

}

void print_begin(){

    struct node *tempdisplay;
    tempdisplay = firstPtr;

    if (firstPtr == NULL)
        printf("List is empty\n");
    else
    {
        while (tempdisplay!= NULL)
        {
            printf("%s", tempdisplay->student_name);
            printf("%s", tempdisplay->gender);
            printf("%s", tempdisplay->course);
            printf("%d\n", tempdisplay->student_number);
            tempdisplay = tempdisplay->next;
        }
    }
    printf("\n\nPress any key to go back main menu\n");
    getch();
}

void print_last(){

}

void remove_student(){

}

void insert_new(char name[], char xgender[], char xcourse[], int number){
    newnode = NULL;
    newnode = (struct node*)malloc(sizeof (struct node));
    strcpy(newnode->student_name,name);
    strcpy(newnode->gender, xgender);
    strcpy(newnode->course, xcourse);
    newnode->student_number = number;
    newnode->next = NULL;

    if (firstPtr == NULL)
    {
        firstPtr = newnode;
    }
    else
    {
        while (firstPtr->next != NULL)
        {
            firstPtr = firstPtr->next;
        }
        firstPtr->next = newnode;
    }

    printf("Insert success\n");
    printf("Press any key to go back main menu\n");
    getch();
}

main()
{
    firstPtr=NULL;
    char name[40];
    char gender[40];
    char course[40];
    int number;
    int main_choice;

    do{
        printf("\n");
        printf("\n");
        printf("\n");
        printf("               1.Add a new student to the beginning\n");
        printf("               2.Add a new student to the end\n");
        printf("               3.Print out the entire list from the beginning\n");
        printf("               4.Print out the entire list from the end\n");
        printf("               5.Remove a student from the list\n");
        printf("               6.Quit the program\n");
        printf("\n\nEnter the number of the options\n");
        scanf("%d", &main_choice);

        switch (main_choice){

        case 1: {
                    printf("Enter the name of the student\n");
                    fgets(name,100,stdin);
                    printf("Enter the gender of the student\n");
                    fgets(gender,100,stdin);
                    printf("Enter the course of the student\n");
                    fgets(course,100,stdin);
                    printf("Enter the number of the student\n");
                    scanf("%d", &number);

                    insert_new(name,gender,course,number);
        }break;
        case 2: insert_last(); break;
        case 3: print_begin(); break;
        case 4: print_last(); break;
        case 5: remove_student(); break;
        case 6:{
                   printf("\n\n\nThe program is closing...\n...\n");
                   return 0;
                   break;
        }
        }
        system("cls");
    } while (main_choice != 6);
}


[ 本帖最后由 chaicai333 于 2014-4-13 18:22 编辑 ]
2014-04-13 17:24



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-430006-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.326481 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved