【问题标题】:How to create exam grading program in c++? [closed]如何在 C++ 中创建考试评分程序? [关闭]
【发布时间】:2016-03-23 16:06:50
【问题描述】:

我尝试帮助朋友完成他的考试以使用 C++ 创建这个考试评分程序,但所有尝试都未能编译该程序。你能帮助我吗? 每次尝试总是得到“致命错误”和“没有这样的文件或目录 编译终止。” 到目前为止,我们尝试使用在线编译器对其进行编译。

# include <stdio.h>
# include <iostream.h>
# include <conio.h>

main()
{
   char nama[20],*Grade;
   float nk,nt,nu,nmk,nmt,nmu,na;
   cout<<"Program Hitung Nilai Akhir Siswa"<<endl<<endl;
   cout<<"   Masukkan Nama Siswa : ";gets(nama);
   cout<<"   Nilai Keaktifan     : ";cin>>nk;
   cout<<"   Nilai Tugas         : ";cin>>nt;
   cout<<"   Nilai Ujian         : ";cin>>nu;
   nmk=nk*0.2;
   nmt=nt*0.3;
   nmu=nu*0.5;
   na=nmk+nmt+nmu;
   if(na>=80)a
   {
      Grade="A";
   }
   else if(na>=90)
   {
      Grade="B";
   }
   else if(na>=80)
   {
      Grade="C";
   }
   else if(na>=70)
   {
      Grade="D";
   }
   else
   {
      Grade="E";
   }
   cout<<endl;
   cout<<"     Siswa Yang Bernama "<<nama<<endl;
   cout<<"     Dengan nilai presentase yang dihasilkan"<<endl;
   cout<<"     Nilai Murni Keaktifan x 20%    : "<<nmk<<endl;
   cout<<"     Nilai Murni Tugas     x 30%    : "<<nmt<<endl;
   cout<<"     Nilai Murni Ujian     x 50%    : "<<nmu<<endl;
   cout<<"     Memperoleh Nilai Akhir Sebesar : "<<na<<endl;
   cout<<"     Grade yang di dapat            : "<<Grade<<endl;
   getch();
}

【问题讨论】:

  • 在线编译器无法识别 20 多年前在 c++ 编译器中使用的非标准头文件。摆脱# include &lt;iostream.h&gt;# include &lt;conio.h&gt;。使用#include &lt;iostream&gt; 而不是# include &lt;iostream.h&gt;
  • 你需要添加using namespace stdstd:: infront或所有std函数
  • 同样char *GradeGrade = "A" 也不起作用
  • 还有char * Grade -> char Grade 然后Grade='A' 将起作用

标签: c++ compiler-errors functional-programming


【解决方案1】:

大多数在线编译器都使用最新的 C++ 标准。它们很可能不支持旧式 C++ 程序。

你可以改变的事情开始......

#include

代替

 # include <stdio.h>
 # include <iostream.h>

使用

 # include <cstdio>
 # include <iostream>

不要使用非标准标题

删除

 # include <conio.h>

cincout 位于 std 命名空间中

cin 的所有用法更改为std::cin,并将cout 的所有用法更改为std::cout。你也可以使用

using namespace std;

避免使用std::cinstd::cout。但是,不要在任何地方都使用这种机制,以免输入额外的 std::

不要使用gets

使用gets 是已知的安全漏洞来源。不要使用它。 将其用法替换为fgets

代替

   cout<<"   Masukkan Nama Siswa : ";gets(nama);

你可以使用

   cout<<"   Masukkan Nama Siswa : ";
   fgets(nama, sizeof(nama), stdin);

但是,这也不好,因为您混合使用 stdincin 来获取用户输入。要么坚持使用来自stdio.h 的函数,要么使用cin 来获取用户输入。您可以使用:

   cout<<"   Masukkan Nama Siswa : ";
   cin.get(nama, sizeof(nama));

使用std::string而不是char*来保存字符串

改变

   char nama[20],*Grade;

   char nama[20];
   std::string Grade;

不要使用非标准函数

删除线

   getch();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多