查看: 836|回复: 9
|
请大家来帮忙解决C++程序里的9个errors
[复制链接]
|
|
以下的程序码有9个errors,
可否请大家告诉我怎么解决?
有劳各位了。。
- #include <iostream.h>
- #include <fstream.h>
- void main()
- {
- fstream hasil;
- int BilUndian;
- int Jumlah=0;
- char Nama [20];
- char NamaCalon [20];
- double PeratusUndian;
- hasil.open (" hasil.dat ", ios::out);
-
- cout<< " Masukkan nama calon : "<<NamaCalon;
- cin>>Nama;
- cout<< " Masukkan bil undian : "<<BilUndian;
- cin>>BilUndian;
- for (int i; i<=15; i++)
- {
- Jumlah=Jumlah + BilUndian;
- hasil<<NamaCalon<< " "<<BilUndian<<endl;
- }
- hasil.close();
- paparCalon (Jumlah);
- Pemenang ();
- }
- void paparCalon (int jum)
- {
- int BilUndian;
- int Jumlah=0;
- double PeratusUndian;
- fstream hasil;
- hasil.open ( " hasil.dat ", ios::in);
- if (hasil.open.fail())
- {
- cerr<<" Ralat semasa membaca fail "<<"hasil.dat";
- exit (-1);
- }
- hasil<<Nama<<BilUndian<<endl;
- cout<<" Calon "<<" "<<" Undian_Diterima "<<" "<<" % daripada Undian diterima "<<endl;
- while (!hasil.eof())
- {
- PeratusUndian = BilUndian/Jumlah*0.01;
- cout<<"Nama"<<" "<<"BilUndian"<<" "<<"PeratusUndian"<<endl;
- hasil>>Nama>>BilUndian;
- }
- hasil.close ();
- }
- void Pemenang ()
- {
- char Nama_Pemenang [20];
- char Nama [20];
- int BilUndian;
- int Undi;
- float PeratusUndian;
- fstream hasil;
- hasil.open (" hasil.dat ", ios::in);
- if (hasil.open.fail())
- {
- cerr<<" Ralat semasa membaca fail "<< "hasil.dat";
- exit (-1);
- }
- hasil>>Nama>>BilUndian;
- Undi=BilUndian;
- strcpy (Nama_Pemenang, Nama);
-
- while (!hasil.eof())
- {
- hasil>>Nama>>BilUndian;
- if (Undi<BilUndian)
- {
- Undi=BilUndian;
- strcpy (Nama_Pemenang, Nama);
- }
- }
- cout<<" Pemenang Pilihanraya adalah : "<< Nama_Pemenang;
- hasil.close();
- }
复制代码 |
|
|
|
|
|
|
|
发表于 25-2-2006 10:21 PM
|
显示全部楼层
function 是無法辯認 另個function 的variable的.
所以void main()和void pemenang()裏的Nama 不能在void Pemenang ()被識別。
除非利用function的argument形式讀取
或是利用C++的強力工具 class
另外 hasil.open.fail()不知道可不可以用。
試試 !hasil.is_open()
或是 hasil.fail() |
|
|
|
|
|
|
|
发表于 25-2-2006 11:04 PM
|
显示全部楼层
hasil.open ( " hasil.dat ", ios::in);
可能有问题..应该 hasil.open ( "hasil.dat", ios::in);
是否也应该将 Error Msg 也贴出来呢? |
|
|
|
|
|
|
|
楼主 |
发表于 25-2-2006 11:58 PM
|
显示全部楼层
原帖由 jasonmun 于 25-2-2006 11:04 PM 发表
hasil.open ( " hasil.dat ", ios::in);
可能有问题..应该 hasil.open ( "hasil.dat", ios::in);
是否也应该将 Error Msg 也贴出来呢?
- --------------------Configuration: Pemenang - Win32 Debug--------------------
- Compiling...
- Pemenang.cpp
- c:\documents and settings\fan_2\desktop\c++\pemenang.cpp(26) : error C2065: 'paparCalon' : undeclared identifier
- c:\documents and settings\fan_2\desktop\c++\pemenang.cpp(27) : error C2065: 'Pemenang' : undeclared identifier
- c:\documents and settings\fan_2\desktop\c++\pemenang.cpp(31) : error C2373: 'paparCalon' : redefinition; different type modifiers
- c:\documents and settings\fan_2\desktop\c++\pemenang.cpp(38) : error C2228: left of '.fail' must have class/struct/union type
- c:\documents and settings\fan_2\desktop\c++\pemenang.cpp(41) : error C2065: 'exit' : undeclared identifier
- c:\documents and settings\fan_2\desktop\c++\pemenang.cpp(44) : error C2065: 'Nama' : undeclared identifier
- c:\documents and settings\fan_2\desktop\c++\pemenang.cpp(59) : error C2373: 'Pemenang' : redefinition; different type modifiers
- c:\documents and settings\fan_2\desktop\c++\pemenang.cpp(68) : error C2228: left of '.fail' must have class/struct/union type
- c:\documents and settings\fan_2\desktop\c++\pemenang.cpp(76) : error C2065: 'strcpy' : undeclared identifier
- Error executing cl.exe.
- Pemenang.obj - 9 error(s), 0 warning(s)
复制代码 |
|
|
|
|
|
|
|
发表于 26-2-2006 12:33 AM
|
显示全部楼层
#include <iostream>
#include <fstream>
using namespace std;
void paparCalon (int); //any function written after main() must be declare first
void Pemenang ();
char Nama [20]; //make this as global variable
int main()
{
fstream hasil;
int BilUndian;
int Jumlah=0;
char NamaCalon [20];
double PeratusUndian;
hasil.open (" hasil.dat ", ios:ut);
cout<< " Masukkan nama calon : "<<NamaCalon;
cin>>Nama;
cout<< " Masukkan bil undian : "<<BilUndian;
cin>>BilUndian;
for (int i; i<=15; i++)
{
Jumlah=Jumlah + BilUndian;
hasil<<NamaCalon<< " "<<BilUndian<<endl;
}
hasil.close();
paparCalon (Jumlah);
Pemenang ();
}
void paparCalon (int jum)
{
int BilUndian;
int Jumlah=0;
double PeratusUndian;
fstream hasil;
hasil.open ( " hasil.dat ", ios::in);
if (hasil.fail())
{
cerr<<" Ralat semasa membaca fail "<<"hasil.dat";
exit (-1);
}
hasil<<Nama<<BilUndian<<endl;
cout<<" Calon "<<" "<<" Undian_Diterima "<<" "<<" % daripada Undian diterima "<<endl;
while (!hasil.eof())
{
PeratusUndian = BilUndian/Jumlah*0.01;
cout<<"Nama"<<" "<<"BilUndian"<<" "<<"eratusUndian"<<endl;
hasil>>Nama>>BilUndian;
}
hasil.close ();
}
void Pemenang ()
{
char Nama_Pemenang [20];
char Nama [20];
int BilUndian;
int Undi;
float PeratusUndian;
fstream hasil;
hasil.open (" hasil.dat ", ios::in);
if (hasil.fail())
{
cerr<<" Ralat semasa membaca fail "<< "hasil.dat";
exit (-1);
}
hasil>>Nama>>BilUndian;
Undi=BilUndian;
strcpy (Nama_Pemenang, Nama);
while (!hasil.eof())
{
hasil>>Nama>>BilUndian;
if (Undi<BilUndian)
{
Undi=BilUndian;
strcpy (Nama_Pemenang, Nama);
}
}
cout<<" Pemenang Pilihanraya adalah : "<< Nama_Pemenang;
hasil.close();
}
ps:我是用dev-cpp IDE来compile的,没问题。。。
[ 本帖最后由 titanhrk 于 26-2-2006 12:36 AM 编辑 ] |
|
|
|
|
|
|
|
楼主 |
发表于 26-2-2006 12:59 AM
|
显示全部楼层
原帖由 titanhrk 于 26-2-2006 12:33 AM 发表
#include <iostream>
#include <fstream>
using namespace std;
void paparCalon (int); //any function written after main() must be declare first
void Pemenang ();
char ...
可是大大
我copy paste了你的编码
compile时却。。。
- --------------------Configuration: Pemenang - Win32 Debug--------------------
- Compiling...
- Pemenang.cpp
- C:\Documents and Settings\Fan_2\Desktop\Pemenang.cpp(18) : warning C4101: 'PeratusUndian' : unreferenced local variable
- C:\Documents and Settings\Fan_2\Desktop\Pemenang.cpp(72) : warning C4101: 'PeratusUndian' : unreferenced local variable
- C:\Documents and Settings\Fan_2\Desktop\Pemenang.cpp(23) : warning C4700: local variable 'BilUndian' used without having been initialized
- C:\Documents and Settings\Fan_2\Desktop\Pemenang.cpp(52) : warning C4700: local variable 'BilUndian' used without having been initialized
- Pemenang.obj - 0 error(s), 4 warning(s)
复制代码 |
|
|
|
|
|
|
|
发表于 26-2-2006 08:33 PM
|
显示全部楼层
不好意思,其实我也没真正去看你的code,只是抄过去我用的IDE再修改compile time error罢了。。。你用的是什么compiler? |
|
|
|
|
|
|
|
楼主 |
发表于 26-2-2006 08:58 PM
|
显示全部楼层
原帖由 titanhrk 于 26-2-2006 08:33 PM 发表
不好意思,其实我也没真正去看你的code,只是抄过去我用的IDE再修改compile time error罢了。。。你用的是什么compiler?
visual C++ |
|
|
|
|
|
|
|
发表于 27-2-2006 04:20 PM
|
显示全部楼层
原帖由 烦 于 26-2-2006 08:58 PM 发表
visual C++
OK 料 .. 没 WARNING 了。。
#include <iostream>
#include <fstream>
using namespace std;
void paparCalon (int); //any function written after main() must be declare first
void Pemenang ();
char Nama [20]; //make this as global variable
int main()
{
fstream hasil;
int BilUndian=0;
int Jumlah=0;
char NamaCalon [20];
double PeratusUndian=0.0;
hasil.open ("hasil.dat", ios:ut);
cout<< " Masukkan nama calon : "<<NamaCalon;
cin>>Nama;
cout<< " Masukkan bil undian : "<<BilUndian;
cin>>BilUndian;
for (int i; i<=15; i++)
{
Jumlah=Jumlah + BilUndian;
hasil<<NamaCalon<< " "<<BilUndian<<endl;
}
hasil.close();
paparCalon (Jumlah);
Pemenang ();
return 0;
}
void paparCalon (int jum)
{
int BilUndian=0;
int Jumlah=0;
double PeratusUndian;
fstream hasil;
hasil.open ( " hasil.dat ", ios::in);
if (hasil.fail())
{
cerr<<" Ralat semasa membaca fail "<<"hasil.dat";
exit (-1);
}
hasil<<Nama<<BilUndian<<endl;
cout<<" Calon "<<" "<<" Undian_Diterima "<<" "<<" % daripada Undian diterima "<<endl;
while (!hasil.eof())
{
PeratusUndian = BilUndian/Jumlah*0.01;
cout<<"Nama"<<" "<<"BilUndian"<<" "<<"eratusUndian"<<endl;
hasil>>Nama>>BilUndian;
}
hasil.close ();
}
void Pemenang ()
{
char Nama_Pemenang [20];
char Nama [20];
int BilUndian;
int Undi;
float PeratusUndian=0;
fstream hasil;
hasil.open (" hasil.dat ", ios::in);
if (hasil.fail())
{
cerr<<" Ralat semasa membaca fail "<< "hasil.dat";
exit (-1);
}
hasil>>Nama>>BilUndian;
Undi=BilUndian;
strcpy (Nama_Pemenang, Nama);
while (!hasil.eof())
{
hasil>>Nama>>BilUndian;
if (Undi<BilUndian)
{
Undi=BilUndian;
strcpy (Nama_Pemenang, Nama);
}
}
cout<<" Pemenang Pilihanraya adalah : "<< Nama_Pemenang;
hasil.close();
} |
|
|
|
|
|
|
|
发表于 27-2-2006 04:23 PM
|
显示全部楼层
COMPILE 是 OK 了。。
不过你的 PROGRAM 很有问题。。 |
|
|
|
|
|
|
| |
本周最热论坛帖子
|