| |

VerySource

 Forgot password?
 Register
Search
View: 1605|Reply: 15

Structure problem

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-20 17:40:01
| Show all posts |Read mode
I used the structure for the first time and wrote a program, which compiles and links normally. It ’s just that the result is different from the input. Something is added. After searching for a long time, I ca n’t figure out the problem there.
#include "stdio.h"
struct person {
char name [20];
char sex [20];
int age;
};
struct student {
int number;
int score;
    struct person roommate;
};
void main ()
{
int i;
    Struct student myroommate [4];
printf ("---------------- Three classes in the department of family planning ---------------------\n");
printf ("Please enter the student number, grade, name, gender, age of your dormitory member:\n");
printf ("----------- Note that the input parts are separated by ',' -----------\n");
for (i = 0; i <4; i ++)
{
scanf ("% d,% d,% s,% s,% d",
                         &myroommate [i] .number,
&myroommate [i] .score,
            &myroommatei] .roommate.name,
            &myroommatei] .roommate.sex,
&myroommate [i] .roommate.age);
}
printf ("Student number, grade, name, gender, and age of dormitory members are:\n");
for (i = 0; i <4; i ++)
{
printf ("% d,% d,% s,% s,% d\n",
                   myroommate [i] .number, myroommate [i] .score,
myroommate [i] .roommate.name,
                  myroommate [i] .roommate.sex,
                  myroommate [i] .roommate.age);
}
}
The output is a little garbled per line.
Reply

Use magic Report

0

Threads

25

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-1-30 08:45:01
| Show all posts
for (i = 0; i <4; i ++)
{
scanf ("% d,% d,% s,% s,% d",
                         &myroommate [i] .number,
&myroommate [i] .score,
            &myroommatei] .roommate.name,
            &myroommatei] .roommate.sex,
&myroommate [i] .roommate.age);
}
=====================
printf ("----------- Note that each part of the input is separated by a space -----------\n");
scanf ("% d% d% s% s% d",
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 Korea, Republic of

Post time: 2020-1-30 12:27:01
| Show all posts
scanf ("% d,% d,% s,% s,% d",
&myroommate [i] .number,
&myroommate [i] .score,
&myroommatei] .roommate.name,
&myroommatei] .roommate.sex,
&myroommate [i] .roommate.age);
}
===========================>
scanf ("% d,% d,% s,% s,% d",
&myroommate [i] .number,
&myroommate [i] .score,
myroommatei] .roommate.name, // No '&'
myroommatei] .roommate.sex, // No '&'
&myroommate [i] .roommate.age);
}
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 Invalid IP Address

Post time: 2020-1-30 15:45:02
| Show all posts
Upstairs solution% s is delimited by whitespace
scanf% s
             Matches a sequence of non-white-space characters;
             the next pointer must be a pointer to charac-
              ter array that is long enough to hold the input sequence and the terminating null character
              (’\0’), which is added automatically. The input string stops at white space or at the maximum
              field width, whichever occurs first.
Reply

Use magic Report

0

Threads

78

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-1-30 16:45:01
| Show all posts
scanf ("% d,% d,% s,% s,% d",
&myroommate [i] .number,
&myroommate [i] .score,
myroommate [i] .roommate.name, // no '&'
myroommate [i] .roommate.sex, // Without '&', what about the half bracket of i?
&myroommate [i] .roommate.age);
}
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 Invalid IP Address

Post time: 2020-1-30 22:54:01
| Show all posts
The problem is that the last 2 variables have not been assigned by scanf. Your input format is
1,1, aa, bb, 1
% d,% d,% s,% s,% d format aa, bb, 1 all assigned to the third, the last two are not assigned the initial value
Reply

Use magic Report

0

Threads

18

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-2-1 11:45:01
| Show all posts
myroommate [i] .roommate.name, // no '&'
myroommate [i] .roommate.sex, // No '&'
  I can't debug it too, sadly!
Reply

Use magic Report

0

Threads

25

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-2-1 21:54:01
| Show all posts
myroommate [i] .roommate.name, // no '&'
myroommate [i] .roommate.sex, // No '&'
=====================
It doesn't matter if there is&.

printf ("----------- Note that each part of the input is separated by a space -----------\n");
scanf ("% d% d% s% s% d",
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-2-3 20:00:02
| Show all posts
% s ends with '\n' and spaces. Your ',' cannot be used to separate% s. . . The one upstairs told you very clearly, use printf ("----------- Note that each part of the input is separated by a space -----------\n");
scanf ("% d% d% s% s% d",
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-2-4 16:15:01
| Show all posts
yctoyaThank you very much. Now the debugging is successful. As for the two left brackets, when I posted the source code for easy browsing, I took a lot of care to deal with it. I also thank other participants and removed two The case of '&' is also correct, but I don't know why?yctoyacan parse it
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