如果用户超过特定字符数则换行
changing line if user exceeds a specific number of characters
我正在做一个作业,其中我正在创建一个在主内存上工作的文本编辑器。我不允许使用文件处理,也不允许我使用字符串 class 字符串库或 cstring 库。
现在我必须实现的是一行只包含 60 个字符,如果用户输入超过 60 个字符,它应该自动转移到下一行,我还必须在用户输入时显示行号
我的代码在这里
#include <iostream>
using namespace std;
int main()
{
char***files=new char**[50];
char**fileNames=new char*[50];
int fileCount=0;
while (true)
{
int selector=0;
cout<<"MacMAds Notepad"<<endl<<endl;
cout<<"Press 1. To Create a new file"<<endl;
cout<<"Press 2. To View an existing file by giving file name"<<endl;
cout<<"Press 3. To edit an existing file by giving its name"<<endl;
cout<<"Press 4. To copy an existing file to a new file"<<endl;
cout<<"Press 5. To delete an existing file by giving its name"<<endl;
cout<<"Press 6. To view listof all files with the names"<<endl;
cout<<"Press7. To Exit"
cin>>selector;
if (selector==7)
break;
if (selector==1)
{
cout<<"Please enter the name of file: ";
cin>>fileNames[fileCount];
int nLines=0;
cout<<"Please enter the number of lines for "<<fileNames[fileCount]<<": ";
cin>>nLines;
files[fileCount]=new char*[nLines];
for (int i=0;i<nLines;i++)
{
files[fileCount][i]=new char[61];
cin.getline(files[fileCount][i],60)
}
}
}
return 0;
}
now what i have to achieve is that a single line contains only 60 characters and if the user exceeds 60 characters in input it should automatically shift to the next line
您不能使用来自 std::cin
和 std::getline()
的标准 c++ 缓冲输入来执行此操作,因为所有输入都需要使用 ENTER 触发关键。
您需要拦截任何直接扫描键盘输入事件的按键。这是 C++ 标准库未涵盖的 OS 特定功能。
虽然有像 ncurses
这样的第 3 方库,但它们允许您以几乎与平台无关的方式执行此操作。
您不能通过标准 c++ 库限制用户输入的字符数。
我正在做一个作业,其中我正在创建一个在主内存上工作的文本编辑器。我不允许使用文件处理,也不允许我使用字符串 class 字符串库或 cstring 库。 现在我必须实现的是一行只包含 60 个字符,如果用户输入超过 60 个字符,它应该自动转移到下一行,我还必须在用户输入时显示行号 我的代码在这里
#include <iostream>
using namespace std;
int main()
{
char***files=new char**[50];
char**fileNames=new char*[50];
int fileCount=0;
while (true)
{
int selector=0;
cout<<"MacMAds Notepad"<<endl<<endl;
cout<<"Press 1. To Create a new file"<<endl;
cout<<"Press 2. To View an existing file by giving file name"<<endl;
cout<<"Press 3. To edit an existing file by giving its name"<<endl;
cout<<"Press 4. To copy an existing file to a new file"<<endl;
cout<<"Press 5. To delete an existing file by giving its name"<<endl;
cout<<"Press 6. To view listof all files with the names"<<endl;
cout<<"Press7. To Exit"
cin>>selector;
if (selector==7)
break;
if (selector==1)
{
cout<<"Please enter the name of file: ";
cin>>fileNames[fileCount];
int nLines=0;
cout<<"Please enter the number of lines for "<<fileNames[fileCount]<<": ";
cin>>nLines;
files[fileCount]=new char*[nLines];
for (int i=0;i<nLines;i++)
{
files[fileCount][i]=new char[61];
cin.getline(files[fileCount][i],60)
}
}
}
return 0;
}
now what i have to achieve is that a single line contains only 60 characters and if the user exceeds 60 characters in input it should automatically shift to the next line
您不能使用来自 std::cin
和 std::getline()
的标准 c++ 缓冲输入来执行此操作,因为所有输入都需要使用 ENTER 触发关键。
您需要拦截任何直接扫描键盘输入事件的按键。这是 C++ 标准库未涵盖的 OS 特定功能。
虽然有像 ncurses
这样的第 3 方库,但它们允许您以几乎与平台无关的方式执行此操作。
您不能通过标准 c++ 库限制用户输入的字符数。