当前位置:编程学习 > C/C++ >>

C++程序的小问题

部分功能无法实现

是不是局部变量定义问题 试了很久还是不行

下面是写的程序

 

 

#include "stdio.h"
#include "conio.h"

 

void main()
      {

    int a;

    do
    {

    printf  ("1: Introduction\n");
    printf  ("2: Student transcripts Editor\n");

    printf("Enter your choice: ");

    scanf("%d",&a);
    }while(a<1||a>2);

    if(a==1)
    {
     printf("                      Introduction      \n");

 

     printf("I just graduated from university students.I am a newcomer to c++\n");
     printf("I am learning to improve their level of c++. am very diligent. \n");
     printf("I learn good character is sometimes more outward calm.\n");
     printf("Sometimes wrote some articles,the English water,proficiency\n ");
     printf("to computer,typing soon, to shift to a good sense of service,\n");
     printf("work conscientiously stringent, harmony and colleagues,with\n");
     printf("a capacity to adapt. to properly deal with contingencies.I\n");
     printf("hope that the company can give me a chanceI learn good \n");
     printf("character is sometimes more outward calm.Sometimes wrote\n");
     printf("some articles, the English water, proficiency to computer,\n");
     printf("typing soon, to shift to a good sense of service, work \n");
     printf("conscientiously  stringent, harmony and colleagues,with a \n");
     printf("capacity to adapt. to properly deal with contingencies. I hope\n");
     printf("that the company can give me a chance. \n");

 


    }

    else if(a==2)
    {
    struct student
    {
    char name[20];
    int math ;
    int physics;
    int language;
    }students[10];

     int i,j;
     int sum[10]={0,0,0,0,0,0,0,0,0,0};
     char ch;

    printf("Enter student data: \n");
    for(i=0;i<10;i++)
    {

    printf("continue(y/n)?");

    scanf("%s",ch);
    ch=getchar();
    if(ch==n)
    break;

    printf("\n");

    printf("name: ");
    scanf("%s",students[i].name);
    printf("math: ");
    scanf("%d",&students[i].math);
    printf("physics: ");
    scanf("%d",&students[i].physics);
    printf("languaage: ");
    scanf("%d",&students[i].language);
    }
    for(j=0;j<i;j++)
    {

    sum[j]=sum[j]+students[j].math;
    sum[j]=sum[j]+students[j].physics;
    sum[j]=sum[j]+students[j].language;
    }
    printf("\n");
    for(j=0;j<i;j++)
    {

    printf("%s",students[j].name);
    printf("%d\n",sum[j]);
    }
    return;


    }

    getch();
}

追问:那个我写掉了- -  是有 'n'的- -
答案:#include "stdio.h"
#include "conio.h"

 

void main()
      {

    int a;

    do
    {

    printf  ("1: Introduction\n");
    printf  ("2: Student transcripts Editor\n");

    printf("Enter your choice: ");

    scanf("%d",&a);
    }while(a<1||a>2);

    if(a==1)
    {
     printf("                      Introduction      \n");

 

     printf("I just graduated from university students.I am a newcomer to c++\n");
     printf("I am learning to improve their level of c++. am very diligent. \n");
     printf("I learn good character is sometimes more outward calm.\n");
     printf("Sometimes wrote some articles,the English water,proficiency\n ");
     printf("to computer,typing soon, to shift to a good sense of service,\n");
     printf("work conscientiously stringent, harmony and colleagues,with\n");
     printf("a capacity to adapt. to properly deal with contingencies.I\n");
     printf("hope that the company can give me a chanceI learn good \n");
     printf("character is sometimes more outward calm.Sometimes wrote\n");
     printf("some articles, the English water, proficiency to computer,\n");
     printf("typing soon, to shift to a good sense of service, work \n");
     printf("conscientiously  stringent, harmony and colleagues,with a \n");
     printf("capacity to adapt. to properly deal with contingencies. I hope\n");
     printf("that the company can give me a chance. \n");

 


    }

    else if(a==2)
    {
    struct student
    {
    char name[20];
    int math ;
    int physics;
    int language;
    }students[10];

     int i,j;
     int sum[10]={0,0,0,0,0,0,0,0,0,0};
     char ch;

    printf("Enter student data: \n");
    for(i=0;i<10;i++)
    {

    printf("continue(y/n)?");

    scanf("%s",ch);
    ch=getchar();
    if(ch=='n')
    break;

    printf("\n");

    printf("name: ");
    scanf("%s",students[i].name);
    printf("math: ");
    scanf("%d",&students[i].math);
    printf("physics: ");
    scanf("%d",&students[i].physics);
    printf("languaage: ");
    scanf("%d",&students[i].language);
    }
    for(j=0;j<i;j++)
    {

    sum[j]=sum[j]+students[j].math;
    sum[j]=sum[j]+students[j].physics;
    sum[j]=sum[j]+students[j].language;
    }
    printf("\n");
    for(j=0;j<i;j++)
    {

    printf("%s",students[j].name);
    printf("%d\n",sum[j]);
    }
    return;


    }

    getch();
}
你的程序基本上都是对的,唯一错的点就是  if(ch==n)
    break;这点的n没有 定义,这里应该是个字符串吧,要在n的两边加上'n';

错了两处,

 1. scanf("%c",&ch);//ch 是char
    ch=getchar();
   2. if(ch=='n')

改下就可以了

上一个:C++ 字符串类的设计
下一个:比较好的c++学习网站

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,