简体   繁体   English

为什么程序的输出中有空格?

[英]Why is there a whitespace in my program's output?

When burning a DVD it is essential that the laser beam burning pits onto the surface is constantly fed with data, otherwise the DVD fails. 刻录DVD时,至关重要的是要不断向表面的激光束烧录坑馈送数据,否则DVD会失败。 Most leading DVD burn applications make use of a circular buffer to stream data from the hard disk onto the DVD. 大多数领先的DVD刻录应用程序都使用循环缓冲区将数据从硬盘流式传输到DVD上。 The first part, the ' writing process' fills up a circular buffer with data, then the ' burning process' begins to read from the buffer as the laser beam burns pits onto the surface of the DVD. 第一部分,“写入过程”用数据填充圆形缓冲区,然后当激光束将凹坑烧到DVD表面上时,开始从缓冲区读取“刻录过程”。 If the buffer starts to become empty, the application should continue filling up the emptied space in the buffer with new data from the disk. 如果缓冲区开始变空,则应用程序应继续使用磁盘中的新数据填充缓冲区中的空白空间。 Implement this scenario using Circular Queue. 使用循环队列实施此方案。

For the above question I wrote the code as follows 对于上述问题,我将代码编写如下

#include<iostream>
#include<string.h>
using namespace std;
#define max 5
struct queue
{
    char a[max];
    int f,r;
}q;
void initialize()
{
    q.f=q.r=-1;
}
int enqueue(char c)
{
    if(((q.f==0)&&(q.r==max-1)) || (q.r+1==q.f))
        return 1;
    else{
    if(q.r==-1)
    {
        q.r=0;
        q.f=0;
    }
    else if(q.r==max-1)
        q.r=0;
    else
        q.r++;
    q.a[q.r]=c;
    }return 0;
}
char dequeue()
{
    if(q.f==-1)
    {
        cout<<"Empty queue";
        return '\0';
    }
    else
    {
        char c = q.a[q.f];
        if(q.r==q.f)
            q.r=q.f=-1;
        else if(q.f==max-1)
            q.f=0;
        else
            q.f++;
        return c;
    }
}
void display()
{
    int i;
    for(i=0;i<max-1;i++)
        cout<<q.a[i]<<"\t";
    cout<<"\nfront: "<<q.f<<"\trear: "<<q.r<<endl;
}
int main()
{
    string str,str1;
    cout<<"Enter a String to write data in DVD\n";
    getline(cin,str,'#');
    int i,f,choice;

    for(i=0;str[i]!='\0';i++)
    {
        f=enqueue(str[i]);
        if(f==1)
        {
            do{
            cout<<"Buffer is:\n";
            display();
            cout<<"Enter 1 to read and 2 to exit\n";
            cin>>choice;
            if(choice==1)
            {
                str1=str1+dequeue();
                cout<<"output: "<<str1<<endl;
            }
            f=enqueue(str[i]);
            i++;
            }while(choice!=2);
        }
        if(choice==2)
            break;
        f=0;
    }
}

I don't know why I am getting a whitespcae when the code runs 我不知道为什么在代码运行时出现白点

在此处输入图片说明

Can anyone point out where I am making mistake? 谁能指出我在哪里犯错?

You forgot to call initialize , so qf and qr are not -1 . 您忘记了调用initialize ,因此qfqr都不为-1 In your case, they are 0 , so the system thinks that there is already something in a[0] and prints this. 在您的情况下,它们是0 ,因此系统认为a[0]已经存在某些内容并将其打印出来。 It's not printable, so you only see the \\t after it. 它不可打印,因此您只能在它后面看到\\t For this reason, initialization should be done in a constructor which you can't forget to call. 因此,初始化应该在您不能忘记调用的构造函数中完成。

Starting with C++11, you can initialize the f and r directly with 从C ++ 11开始,您可以直接使用来初始化fr

struct queue
{
    char a[max];
    int f=-1, r=-1;
} q;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM