注册 登录
编程论坛 VC++/MFC

程序怎么修改

lanyangyangw 发布于 2011-03-23 16:06, 404 次点击
#include"stdio.h"
main()
{
  struct st
  {
      int snum;
      int age;
      char sex;
      float score;
  };
  struct st info;
  printf("Input number:");
  scanf("%d",&info.snum);
  printf("Input age:");
 scanf("%d",&info.age);
  printf("Input sex:");
  scanf("%c\n",&info.sex);
  printf("Input score:");
  scanf("%f",&info.score);
  printf("%d ", info.snum);
  printf("%d ", info.age);
  printf("%c", info.sex);
  printf("%f ", info.score);
}
这个程序里面给预期的结果不一样,应该怎么改?
4 回复
#2
sdupoplar2011-03-24 17:05
  printf("Input number:");
  scanf("%d",&info.snum);
  fflush(stdin);     
  printf("Input age:");
scanf("%d",&info.age);
fflush(stdin);     
  printf("Input sex:");
  scanf("%c",&info.sex);
  fflush(stdin);     
  printf("Input score:");  
  scanf("%f",&info.score);
  printf("%d ", info.snum);
  printf("%d ", info.age);
  printf("%c ", info.sex);
  printf("%f ", info.score);
#3
x5640968982011-03-28 21:15
回复 楼主 lanyangyangw
#include"stdio.h"
main()
{
  struct st
  {
      int snum;
      int age;
      char sex;
      float score;
  };
  struct st info;
  printf("Input number:");
  scanf("%d",&info.snum);
  printf("Input age:");
scanf("%d",&info.age);
  printf("Input sex:");
  scanf("%c\n",&info.sex);
  printf("Input score:");
  scanf("%f",&info.score);
  printf("%d ", info.snum);
  printf("%d ", info.age);
  printf("%c", info.sex);
  printf("%f ", info.score);
  return(0);
}
#4
sxzxy2011-03-30 10:52
经过我的改写和测试,以上的程序若要达到预期的结果,如下所示:

#include "stdio.h"

//定义的数据结构
struct st
{   
    int snum;
    int age;
    char sex;
    float score;
};

int main(int argc,char* argv[])
{
    st info;
    int snum = info.snum;
    int age = info.age;
    char sex = info.sex;
    float score = info.score;

    //做各项参数输入
    printf("Input number:");
    scanf("%d",&snum);
   
    printf("Input age:");
    scanf("%d",&age);
   
    printf("Input sex:");
    scanf("%s",&sex);

    printf("Input score:");
    scanf("%f",&score);
   
    printf("%d",snum);
    printf("%d",age);
    printf("%c",sex);
    printf("%f",score);
}
#5
sxzxy2011-03-30 10:55
上面所录之程序,如有错误,请勿吝赐教!!
1