c++ - 如何在 C++ 中创建考试评分程序?

标签 c++ compiler-errors functional-programming

关闭。这个问题需要debugging details .它目前不接受答案。












想改进这个问题?将问题更新为 on-topic对于堆栈溢出。

6年前关闭。




Improve this question




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

# 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();
}

最佳答案

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

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

#include

代替

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

采用
 # include <cstdio>
 # include <iostream>

不要使用非标准标题

消除
 # include <conio.h>

cincoutstd命名空间

更改 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();

关于c++ - 如何在 C++ 中创建考试评分程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36183146/

相关文章:

c - 头文件的排除是否是语法错误的一部分?

oop - FP 和 OO 正交吗?

c++ - 如何在 C++ 上可视化 3D 点

c++ - Qt 在 QVariant 处抛出非法错误

mysql - 如何将非数字值转换为空白以用于 case 语句?

java - 如何递归引用泛型参数?

list - 在 Scala 中将一个列表拆分为两个列表

c++ - 将元素数组转换为 reference_wrappered 元素数组

Android 日志打印 CString

c - 错误:warning comparison between pointer and integer