干扰getline功能的东西?(Something interfering with the getline function?)

编程入门 行业动态 更新时间:2024-10-26 00:23:29
干扰getline功能的东西?(Something interfering with the getline function?)

我是代码和这个网站的新手,所以如果我忽略了一些明显的东西,请原谅我。 我一直在尝试用c ++编写一个简短的小游戏,我需要接受用户输入。 我想接受多个单词,所以我的书推荐的cin>>命令就出来了。 我通过研究发现的最好的东西是getline(cin,var); 命令。 我可以轻松地在小规模测试中使用它,但是当我将它实现到我的500系列游戏时,它永远不会起作用。 它将跳过该位代码而不等待用户输入,并将变量设置为空白。 我不会明显包含整个代码,但这里有问题,我的标题。

#include <cstdlib> #include <windows.h> #include <iostream> #include <string> using namespace std; string poemend; int heading; int poem() { system("CLS"); cout<<"This poem is almost done, you just can't seem to find the perfect last word.\n"; cout<<"You can see several of your discarded endings. Most recently, 'pain'.\n\n"; cout<<"How about another go?\n>"; getline(cin,poemend); system("CLS"); cout<<"The Prophet's old parrot spoke much of all things,\n"; cout<<"but when asked about love, squawked only "; cout<<poemend<<" .\n\n"; Sleep(6000); cout<<"You decide it could still use some work"; Sleep(3000); heading = 6; }

再次,如果我把它带到一个新的空白页面,这是完美的,所以我真的不确定是什么阻碍了。 我很乐意回答有关代码的任何问题,并在需要时发布更多有用的内容。 非常感谢您花时间阅读本文!

I'm new to code and this website, so please forgive me if I'm overlooking something obvious. I've been trying to write a short little game in c++, and I need to take in user input. I want to take in more than one word so the cin>> command my book recommends is right out. The best thing I have found through research is the getline(cin,var); command. I can get this to easily work in small scale tests, but when I implement it in to my 500 line game, it never works. It will skip right over that bit of code without waiting for user imput, and set the variable to a blank space. I won't include the whole code obviously, but here is the bit in question, and my headers.

#include <cstdlib> #include <windows.h> #include <iostream> #include <string> using namespace std; string poemend; int heading; int poem() { system("CLS"); cout<<"This poem is almost done, you just can't seem to find the perfect last word.\n"; cout<<"You can see several of your discarded endings. Most recently, 'pain'.\n\n"; cout<<"How about another go?\n>"; getline(cin,poemend); system("CLS"); cout<<"The Prophet's old parrot spoke much of all things,\n"; cout<<"but when asked about love, squawked only "; cout<<poemend<<" .\n\n"; Sleep(6000); cout<<"You decide it could still use some work"; Sleep(3000); heading = 6; }

Again, this works perfectly if I take this in to a new blank page, so I'm really not sure what is getting in the way. I will be happy to answer any questions about the code and post more helpful bits of it if needed. Thank you so much for taking the time to read this!

最满意答案

有时刷新输入缓冲区很神奇。 这可能是您的Senario中的情况,也可能不是,但请尝试此代码。

cin.ignore( cin.rdbuf()->in_avail() ); cin.getline(cin,poemend); cin.clear();

基本上用忽略代码和cin.clear包装你的getline。

Sometimes flushing the input buffer is magic. This may or may not be the case in your senario, but try this code.

cin.ignore( cin.rdbuf()->in_avail() ); cin.getline(cin,poemend); cin.clear();

Essentially wrapping your getline with the ignore code and cin.clear.

更多推荐

本文发布于:2023-08-01 10:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1357921.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:干扰   东西   功能   function   getline

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!