这段代码中存在逻辑错误,我无法弄清楚。代码用turbo c ++编写。 [英] There is a logical error in this code which I can not figure out. The code is written in turbo c++.

查看:67
本文介绍了这段代码中存在逻辑错误,我无法弄清楚。代码用turbo c ++编写。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<fstream.h>
#include<process.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>


class address
{	int hno;
	char street[30];
	long int pin;
     public:
	void geta()
	{
		cout<<"Enter the house number ";
		cin>>hno;
		cout<<"Enter the street ";
		gets(street);
		cout<<"Enter the pin ";
		cin>>pin;
	}
	void showa()
	{
		cout<<"\nThe houseno"<<hno;
		cout<<"\nThe street ";
		puts(street);
		cout<<"\nThe pin "<<pin;
		cout<<endl;
	}
};


class voter
{
	char name[30];
	int age;
	long int vid;
	char gender;
	address a;
	int lastvote;
	public:
	void getdata()
	{
		cout<<"Enter name"<<endl;
		gets(name);


		ifstream f2("Voter.dat" , ios::binary|ios::in);
		f2.read((char *) this , sizeof(voter));
		f2.seekg(0,ios::end);
		vid = 1 + f2.tellg()/sizeof(voter) ;

		cout << "Your voter id is" << vid<<endl ;
		f2.close();

		cout<<"Enter age (should be greaterthan or equal to 18)"<<endl;
		cin>>age;
		cout<<"Enter address"<<endl;
		a.geta();
		cout<<"Enter gender (M / F)"<<endl;
		cin>>gender;
		cout<<"Enter the year when last voted (between 1900 and 2014;0 for never)"<<endl;
		cin>>lastvote;
	}

	void putdata()
	{	cout<<"Name:"<<endl;
		puts(name);
		cout<<"Voter ID:"<<endl;
		cout<<vid << endl;
		cout<<"Age:"<<endl;
		cout<<age << endl ;
		cout<<"Address:"<<endl;
		a.showa() ;
		cout<<"Gender:"<<endl;
		cout<<gender << endl ;
		cout<<"Last time voted in the year:"<<endl;
		if (lastvote<1900 || lastvote>2014 || lastvote%5 !=4)
		cout<<"Inavlid input" << endl ;
		else if (lastvote==0)
		cout<<"Never casted a vote"<<endl;
		else
		cout<<lastvote  << endl;
	}
	int ret()
	{
		return vid;
	}
	int retvote()
	{
		return lastvote;
	}
	void modify(void );
}v;

void voter::modify(void )
{

	cout<<"Enter new data:"<<endl;
       cout<<"Enter name "<<endl;
		gets(name);

		cout<<"Enter age (should be greaterthan or equal to 18)"<<endl;
		cin>>age;
		cout<<"Enter address"<<endl;
		a.geta();
		cout<<"Enter gender (M \ F)"<<endl;
		cin>>gender;
		cout<<"Enter the year when last voted (between 1900 and 2014;0 for never)"<<endl;
		cin>>lastvote;	;
}
void main()
{
	clrscr();
	int sw,pos,count,voted,p;
	int s,d,m;
	char test='n';
	count=0;
	voted=0;
	fstream file;
	file.open("Voter.dat",ios::in|ios::app|ios::binary);
	file.close();
	char ch='y';
	cout<<"Enter a choice(1-7)"<<endl;
	do
	{
		cout<<"1.Register"<<endl;
		cout<<"2.View detials of a voter"<<endl;
		cout<<"3.View details of all the  voters"<<endl;
		cout<<"4.Modify data"<<endl;
		cout<<"5.Delete data of a voter"<<endl;
		cout<<"6.Display percentage of people who voted in a particular year"<<endl;
		cout<<"7.Exit"<<endl;
		cin>>sw;
		switch(sw)
		{
			case 1:
				file.open("Voter.dat",ios::in|ios::out|ios::binary);
				v.getdata();
				file.write((char*)&v,sizeof(v));
				file.close();
				break;

			case 2:
				file.open("Voter.dat",ios::in|ios::out|ios::binary);
				cout<<" Voter id : " << endl;
				cin >> s;

				file.seekg(0,ios::beg);
				while(!file.eof())
				{
					file.read((char*)&v,sizeof(v));
					if(v.ret()==s)
					{
						v.putdata();
						test='y';
						break;
					}
				}
				if(test=='n')
				{
					cout<<"Voter ID not found"<<endl;
				}
				file.close();
				break;

			case 3:
				file.open("Voter.dat",ios::in|ios::out|ios::binary);
				file.seekg(0,ios::beg);
				while(!file.eof())
				{
					file.read((char*)&v,sizeof(v));
					if(file.eof())
					break;
					v.putdata();
				}
				file.close();
				break;

			case 4:
				file.open("Voter.dat",ios::in|ios::out|ios::binary);
				cout<<"Enter the voter ID of record to be edited: "<<endl;
				cin>>m;
file.read((char*)&v,sizeof(v));
file.seekg(0,ios::end);
if(file.tellg() >= sizeof(voter) * m)
					{
						pos = (m-1)*sizeof(voter);
file.seekp(pos);
						v.modify();
						file.write((char*)&v,sizeof(v));
						test='y';

					}

				if (test=='n')
				{
					cout<<"Record with voter ID:"<<m<<"not found"<<endl;
				}
				cout<<"Now the file contains"<<endl;
				file.seekg(0,ios::beg);
				while(!file.eof())
				{
					file.read((char*)&v,sizeof(v));
					v.putdata();
				}
				file.close();

				break;

			case 5:
				file.open("Voter.dat",ios::in|ios::out|ios::binary);
				cout<<"Enter the voter ID ofrecord to be deleted"<<endl;
				cin>>d;
				file.seekg(0,ios::beg);
				fstream filetemp;
				filetemp.open("Temp.dat",ios::out || ios::binary);
				while(!file.eof())
				{

					file.read((char*)&v,sizeof(v));
					if(v.ret()==d)
					{
						test='y';
					}
					else
					{
						filetemp.write((char*)&v,sizeof(v));
					}
				}
				if(test=='n')
				cout<<"Record not found"<<endl;
				file.close();
				filetemp.close();
				remove("Voter.dat");
				rename("Temp.dat","Voter.dat");
				file.open("Voter.dat",ios::in|ios::out|ios::binary);
				cout<<"Now the file contains"<<endl;
				file.seekg(0,ios::beg);
				while(!file.eof())
				{
					file.read((char*)&v,sizeof(v));
					v.putdata();
				}
				file.close();
				break;

			case 6:
				file.open("Voter.dat",ios::in|ios::out|ios::binary);
				cout<<"Enter the year to display percentage voters of that	year"<<endl;
				cin>>p;
				file.seekg(0,ios::beg);
				while(!file.eof())
				{
					file.read((char*)&v,sizeof(v));
					if(v.retvote()==p)
					{
						count++;
						voted++;
					}
					else
					count++;
				}
				cout<<"Percentage voters is : "<<(voted/count)*100<<endl;
				file.close();
				break;

			case 7:
				exit(0);
				break;
default :
cout<<"Invalid input"<<endl;
break;
}
		cout<<"Do you want to continue?(Y/N)"<<endl;
		cin>>ch
;}while ( ch=='y' || ch=='Y' );
getch();}





我尝试过:



几周以来我一直在考虑它,但我无法弄清楚我的错误。如果你可以提供帮助,那将是非常好的。代码是使用数据文件处理编写的,是我的12级项目。所以这对我来说非常重要。



What I have tried:

I have been thinking about it for weeks but I am not able to figure out my mistake. It would be really nice if you can help. The code is written using data file handling and is my project for class 12. So it is really important for me.

推荐答案

你只是忘了告诉我们你的代码是怎么回事出错了。

You only forgot to tell us how your code go wrong.
引用:

我已经考虑了几个星期但是我无法弄明白错误。

I have been thinking about it for weeks but I am not able to figure out my mistake.



你知道你的代码应该如何工作,通过使用调试器,你会看到它执行时,当它开始表现不同于预期时,你发现你的位置在哪里bug。



有一个工具可以让你看到你的代码在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期效果时,你就接近一个错误。



使用Turbo C ++中集成的调试器。


You know how your code is supposed to work, by using the debugger, you will see it as it execute, when it start behave otherwise than expected, you found where is your bug.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

Use the debugger integrated in Turbo C++.


这篇关于这段代码中存在逻辑错误,我无法弄清楚。代码用turbo c ++编写。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆