| |

VerySource

 Forgot password?
 Register
Search
View: 839|Reply: 6

The corresponding item in the linked list read abnormally. . . depressed. . .

[Copy link]

7

Threads

15

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-1-6 16:30:01
| Show all posts |Read mode
Except for the name (NAME) can be read in normally, all others are random values, and there are problems with SEX
struct stu_data // student data, record student number, name, gender, age separately
{
long num;
char name [NAMELEN];
char sex;
int age;
struct stu_data * next;
};
struct stu_data * Crt (void) // Create a linked list
{
struct stu_data * head, * bk, * fwd;
head = fwd = bk = (struct stu_data *) malloc (LEN);
                        // head doesn't store anything, it starts from head-> next
fwd = head-> next = (struct stu_data *) malloc (LEN);
printf ("Now input the number, name, sex and age in order please\n");
printf ("You can input 0 as a number to cease\n");
while (1) // If the student ID is 0, then BREAK
{
printf ("Num =>");
scanf ("% ld", fwd-> num);
if (fwd-> num == 0)
{
bk-> next = NULL;
free (fwd);
break;
}
printf ("Name =>");
scanf ("% s", fwd-> name);
printf ("sex =>");
scanf ("% c% c", fwd-> sex);
printf ("age =>");
scanf ("% d", fwd-> age);
bk = fwd;
fwd = fwd-> next = (struct stu_data *) malloc (LEN);
}
return (head);
}
Reply

Use magic Report

0

Threads

25

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-1-6 19:36:01
| Show all posts
ding
The anomaly is
scanf ("% ld", fwd-> num) ;?
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-7 08:36:01
| Show all posts
#include <malloc.h>
#include <iostream>
using namespace std;
struct stu_data // student data, record student number, name, gender, age separately
{
long num;
char * name;
char sex;
int age;
struct stu_data * next;
};
struct stu_data * Crt (void) // Create a linked list
{
struct stu_data * head, * bk, * fwd;
head = fwd = bk = (struct stu_data *) malloc (sizeof (stu_data));
// head doesn't store anything, it starts from head-> next
fwd = head-> next = (struct stu_data *) malloc (sizeof (stu_data));
cout << "Now input the number, name, sex and age in order please\n";
cout << "You can input 0 as a number to cease\n";
while (1) // If the student ID is 0, then BREAK
{
cout << "Num =>";
cin >> fwd-> num;
if (fwd-> num == 0)
{
bk-> next = NULL;
free (fwd);
break;
}
cout << "Name =>";
cin >> fwd-> name;
cout << "sex =>";
cin >> fwd-> sex;
cout << "age =>";
cin >> fwd-> age;
bk = fwd;
fwd = fwd-> next = (struct stu_data *) malloc (sizeof (stu_data));
}
return (head);
};
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-7 08:51:01
| Show all posts
Please see if this works, I don't quite understand, because I just learned. If there are any mistakes, please advise, thank you!
Reply

Use magic Report

7

Threads

15

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

 Author| Post time: 2020-1-10 16:27:02
| Show all posts
Thank you angel ha although I don't understand. . . (I just started C, I will learn C ++ next semester, but thank you ~~~) I still have sunshine, and I know what's wrong. It is indeed SCANF,&less, and hematemesis. . . . --!
Reply

Use magic Report

7

Threads

15

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

 Author| Post time: 2020-1-10 18:09:01
| Show all posts
Thank you angel, although I don't understand. . . (Just started with C, I will learn C ++ next semester ... but thank you ~) I still have sunshine, it is indeed planted on SCANF, FWD-> there is less&in front, vomiting blood. . . --!
Reply

Use magic Report

7

Threads

15

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 United States

 Author| Post time: 2020-1-10 18:36:01
| Show all posts
Although I do n’t understand, thank you ~ (just started C, learn C ++ next semester ... but thank you ~) Back to the sunshine, I did die on SCANF,&less, vomiting blood. . . --!
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list