如何在 c++ 中创建空间?

发布于 2024-08-31 18:24:33 字数 320 浏览 10 评论 0原文

假设我们有一段代码:

int main()
{
   char a[10];
   for(int i = 0; i < 10; i++)
   {
       cin>>a[i];
       if(a[i] == ' ')
          cout<<"It is a space!!!"<<endl;
   }
   return 0;
}

如何从标准输入中输入空格符号?如果你写空格,程序会忽略! :( 是否有任何符号组合(例如“\s”或类似的东西)表示“空格”,我可以从标准输入中为我的代码使用它?

Say we have a code:

int main()
{
   char a[10];
   for(int i = 0; i < 10; i++)
   {
       cin>>a[i];
       if(a[i] == ' ')
          cout<<"It is a space!!!"<<endl;
   }
   return 0;
}

How to cin a Space symbol from standard input? If you write space, program ignores! :(
Is there any combination of symbols (e.g. '\s' or something like this) that means "Space" that I can use from standard input for my code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

末蓝 2024-09-07 18:24:33

默认情况下,它会跳过所有空白(空格、制表符、换行符等)。您可以更改其行为,或使用稍微不同的机制。要更改其行为,请使用操纵器 noskipws,如下所示:

 cin >> noskipws >> a[i];

但是,由于您似乎想查看单个字符,因此我建议使用 get,例如注意

 cin.get( a, n );

:如果 get 找到换行符 (\n) 或在 n-1 个字符之后,get 将停止从流中检索字符。它提前停止,以便可以将空字符 (\0) 附加到数组中。您可以在此处了解有关 istream 接口的更多信息。

It skips all whitespace (spaces, tabs, new lines, etc.) by default. You can either change its behavior, or use a slightly different mechanism. To change its behavior, use the manipulator noskipws, as follows:

 cin >> noskipws >> a[i];

But, since you seem like you want to look at the individual characters, I'd suggest using get, like this prior to your loop

 cin.get( a, n );

Note: get will stop retrieving chars from the stream if it either finds a newline char (\n) or after n-1 chars. It stops early so that it can append the null character (\0) to the array. You can read more about the istream interface here.

随心而道 2024-09-07 18:24:33
#include <iostream>
#include <string>

int main()
{
   std::string a;
   std::getline(std::cin,a);
   for(std::string::size_type i = 0; i < a.size(); ++i)
   {
       if(a[i] == ' ')
          std::cout<<"It is a space!!!"<<std::endl;
   }
   return 0;
}
#include <iostream>
#include <string>

int main()
{
   std::string a;
   std::getline(std::cin,a);
   for(std::string::size_type i = 0; i < a.size(); ++i)
   {
       if(a[i] == ' ')
          std::cout<<"It is a space!!!"<<std::endl;
   }
   return 0;
}
诗化ㄋ丶相逢 2024-09-07 18:24:33

要输入包含大量空格的整行,您可以使用 getline(cin,string_variable);

例如:

string input;
getline(cin, input);

此格式捕获句子中的所有空格,直到按下 return

To input AN ENTIRE LINE containing lot of spaces you can use getline(cin,string_variable);

eg:

string input;
getline(cin, input);

This format captures all the spaces in the sentence untill return is pressed

喵星人汪星人 2024-09-07 18:24:33

使用 cin.get() 读取下一个字符。

然而,对于这个问题,一次读取一个字符是非常低效的。请改用istream::read()

int main()
{
   char a[10];
   cin.read(a, sizeof(a));
   for(int i = 0; i < 10; i++)
   {
       if(a[i] == ' ')
          cout<<"It is a space!!!"<<<endl;
   }
   return 0;
}

并使用 == 检查相等性,而不是 =

Use cin.get() to read the next character.

However, for this problem, it is very inefficient to read a character at a time. Use the istream::read() instead.

int main()
{
   char a[10];
   cin.read(a, sizeof(a));
   for(int i = 0; i < 10; i++)
   {
       if(a[i] == ' ')
          cout<<"It is a space!!!"<<<endl;
   }
   return 0;
}

And use == to check equality, not =.

猥︴琐丶欲为 2024-09-07 18:24:33

使用cin的>>运算符将删除前导空格并在第一个尾随空格处停止输入。要获取整行输入(包括空格),请尝试 cin.getline()。要一次抓取一个字符,您可以使用 cin.get() 。

Using cin's >> operator will drop leading whitespace and stop input at the first trailing whitespace. To grab an entire line of input, including spaces, try cin.getline(). To grab one character at a time, you can use cin.get().

如痴如狂 2024-09-07 18:24:33

我想我会分享对我有用的答案。上一行以换行符结束,因此大多数答案本身不起作用。这样做是这样的:

string title;
do {
  getline(cin, title);
} while (title.length() < 2);

假设输入总是至少 2 个字符长,这适合我的情况。您还可以尝试将其与字符串 "\n" 进行简单比较。

I thought I'd share the answer that worked for me. The previous line ended in a newline, so most of these answers by themselves didn't work. This did:

string title;
do {
  getline(cin, title);
} while (title.length() < 2);

That was assuming the input is always at least 2 characters long, which worked for my situation. You could also try simply comparing it to the string "\n".

烛影斜 2024-09-07 18:24:33

尝试这四种方法来获取带有空格的输入:)

#include<iostream>
#include<stdio.h>

using namespace std;

void dinput(char *a)
{
    for(int i=0;; i++)
    {
        cin >> noskipws >> a[i];
        if(a[i]=='\n')
        {
            a[i]='\0';
            break;
        }
    }
}


void input(char *a)
{
    //cout<<"\nInput string: ";

    for(int i=0;; i++)
    {
        *(a+i*sizeof(char))=getchar();

        if(*(a+i*sizeof(char))=='\n')
        {
            *(a+i*sizeof(char))='\0';
            break;
        }

    }
}



int main()
{
    char a[20];

    cout<<"\n1st method\n";
    input(a);
    cout<<a;

    cout<<"\n2nd method\n";
    cin.get(a,10);
    cout<<a;

    cout<<"\n3rd method\n";
    cin.sync();
    cin.getline(a,sizeof(a));
    cout<<a;

    cout<<"\n4th method\n";
    dinput(a);
    cout<<a;

    return 0;
}

Try this all four way to take input with space :)

#include<iostream>
#include<stdio.h>

using namespace std;

void dinput(char *a)
{
    for(int i=0;; i++)
    {
        cin >> noskipws >> a[i];
        if(a[i]=='\n')
        {
            a[i]='\0';
            break;
        }
    }
}


void input(char *a)
{
    //cout<<"\nInput string: ";

    for(int i=0;; i++)
    {
        *(a+i*sizeof(char))=getchar();

        if(*(a+i*sizeof(char))=='\n')
        {
            *(a+i*sizeof(char))='\0';
            break;
        }

    }
}



int main()
{
    char a[20];

    cout<<"\n1st method\n";
    input(a);
    cout<<a;

    cout<<"\n2nd method\n";
    cin.get(a,10);
    cout<<a;

    cout<<"\n3rd method\n";
    cin.sync();
    cin.getline(a,sizeof(a));
    cout<<a;

    cout<<"\n4th method\n";
    dinput(a);
    cout<<a;

    return 0;
}
眼波传意 2024-09-07 18:24:33

我有同样的问题,我只是使用了 cin.getline(input,300); 。

noskipwscin.get() 有时不太好用。由于您的数组大小正确,请尝试使用 cin.getline() ,它不关心任何字符并以指定的字符数读取整行。

I have the same problem and I just used cin.getline(input,300);.

noskipws and cin.get() sometimes are not easy to use. Since you have the right size of your array try using cin.getline() which does not care about any character and read the whole line in specified character count.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文