循环在我的程序中没有正确执行! !请有人帮帮我! ! ! [英] Loops are not executing properly in my program! ! Can someone help me out please! ! !

查看:68
本文介绍了循环在我的程序中没有正确执行! !请有人帮帮我! ! !的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的循环继续重复,尽管键入N = 1.



这里是代码..

  void  club :: enterdata()
{
std :: cout<< 输入详细信息;
std :: cin>> mno>> memname>>活动;



这是函数......这是函数中的函数这个函数叫做..

  void  club :: writeinto()
{
club c {};
int N,i = 0 ;
std :: ofstream fout( project.dat,std :: ios ::二进制);
if (!fout)
{
std :: cout<< 错误;
}
while (!fout.eof())
{
std :: cout<< 输入要输入的记录数;
std :: cin>> N;
for (i = 0 ; i< = N; i ++)
{
c.enterdata();
}
fout.write(( char *)& c, sizeof (C) );
}
fout.close();
}

这是输出..



输入要输入的记录数1

输入详细信息12 hka abc

输入详细信息



请帮帮我..



我尝试过:



试图更改括号的位置......

解决方案

为什么你有这个:while(!fout.eof())?



我认为这是你的问题。有的理由这样做。我已经使用C和C ++三十年了,而且在写入时我从未在输出文件上检查过EOF。 EVER。我想不出任何理由,只有理由不这样做。


你应该学会使用调试器,你会在第一次运行中看到你的错误。阅读文档也很有帮助。



你的for循环有一个问题,即在写入文件之前C总是被覆盖。

  for (i =  0 ; i< = N; i ++)
{
c.enterdata();
fout.write(( char *)& c, sizeof (c));
}

提示:为enterdata()中的每个成员创建更详细的I / O,并将数据作为csv文本写入文件中,这样您就可以更好地检查并重新加载它。


My loop keeps on repeating inspite of typing N=1.

here is the code..

void club::enterdata()
{
std::cout<<"Enter details";
std::cin>>mno>>memname>>activity;


this is the function... and this is the funcion in which this function is called..

void club::writeinto()
{
    club c{};
    int N, i = 0;
    std::ofstream fout( "project.dat", std::ios::binary );
    if( ! fout )
    {
        std::cout<<"Error";
    }
    while(!fout.eof())
    {
        std::cout << "Enter the number of records to be entered";
        std::cin >> N;
        for(i=0;i<=N;i++)
        {
            c.enterdata();
        }
        fout.write( (char*)&c, sizeof(c) );
    }
    fout.close();
}

here is the output..

Enter the number of records to be entered 1
Enter details 12 hka abc
Enter details

Pls help me out..

What I have tried:

Tried to change the position of the brackets...

解决方案

Why do you have this : "while(!fout.eof())" ?

I think that is your problem. There is NO reason to do that. I have been working with C and C++ for thirty years and I have never checked for EOF on an output file while writing to it. EVER. I can't think of any reason to, only reasons NOT to.


You should learn to use the debugger, you would have seen your mistake in the first run. Reading the documentation is also helping.

Your for loop has the problem that always the C is overwritten before written to the file.

for(i=0;i<=N;i++)
{
    c.enterdata();
    fout.write( (char*)&c, sizeof(c) );
}  

tip: make more detailed I/O for each member in enterdata() and write the data as csv text in the file, so you can better check and reload it.


这篇关于循环在我的程序中没有正确执行! !请有人帮帮我! ! !的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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