C++在Xcode中暂停
C++ in Xcode pausing
这是我的第一篇SO文章。我对编程很陌生,使用C++,我想我可以尝试制作一个程序,允许用户提交一段文本(最多500个字符),允许他们输入一个4个字母的单词,程序会返回它在文本中拾取该单词的次数。我使用的是X代码,它不断生成一个绿色断点,并在"for"循环函数处暂停程序。我的代码如下所示:
#include <iostream>
#include <string>
#include <math.h>
#define SPACE ' '(char)
using namespace std;
//Submit text (maximum 500 characters) and store in variable
string text;
string textQuery(string msgText) {
do {
cout << msgText << endl;
getline(cin, text); } while (text.size() > 500);
return text;
}
//Query word to search for and store as variable
string word;
string wordQuery(string msgWord) {
cout << msgWord << endl;
cin >> word;
return word;
}
//Using loop, run through the text to identify the word
int counter = 0;
bool debugCheck = false;
int searchWord() {
for (int i = 0; i < text.size(); i++) {
char ch_1 = text.at(i);
char ch_2 = text.at(i + 1);
char ch_3 = text.at(i + 2);
char ch_4 = text.at(i + 3);
cout << i;
if(ch_1 == word.at(0) &&
ch_2 == word.at(1) &&
ch_3 == word.at(2) &&
ch_4 == word.at(3) )
{
counter++;
debugCheck = true;
}
}
return counter;
}
//cout the result
int main() {
string textUserSubmit = textQuery("Please submit text (max 500 characters): ");
string wordUserSubmit = wordQuery("Please select a word to search for: ");
int counterResponse = searchWord();
cout << debugCheck << endl;
cout << "The number of times is: " << counterResponse << endl;
return 0;
}
我在for循环中得到错误。关于如何使我的程序适用于不同的单词、多种长度的单词,以及如何在文本中突出显示单词,任何其他建议都会很有帮助。如果有人能帮我解决问题,我将不胜感激。谢谢
我在for循环中得到错误。
你应该描述你得到的错误。我碰巧可以访问Xcode,这样我就可以运行你的代码,看看会发生什么,但你应该尽量避免那些你想要帮助的人。
在这种情况下,您应该描述调试器如何在以下行停止程序:
char ch_4 = text.at(i + 3);
包括消息:"线程1:信号SIGABRT",控制台输出显示
libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string
您的问题是:在将字符串text
用作索引之前,for
循环会检查以确保i
在其正确的范围内,但随后您也会将i+1
、i+2
和i+3
用作索引,而不检查这些值是否也有效。
修复该检查,程序看起来运行良好(输入正确)。
一些杂七杂八的评论。
- 使用更一致的缩进。它使程序更易于阅读和遵循。以下是我将如何缩进它(使用clang格式工具)
#define SPACE ' '(char)
看起来是个坏主意,即使您没有使用它using namespace std;
通常不受欢迎,但只要你不把它放在标题中,它通常不会引起太多麻烦。不过我仍然可以,因为你可能不理解由此产生的错误消息,所以你可能无论如何都想避免它。如果你真的不喜欢到处写std::
,那么就使用更有限的应用程序,比如using std::string;
和using std::cout;
- 应该避免全局变量,在这里只需将
textUserSubmit
和wordUserSubmit
传递给searchWord()
即可 - 实际上不需要确保CCD_ 15的长度小于或等于500个字符。您使用的是
std::string
,因此它可以容纳更长的输入 - 您永远不会检查
word
的长度,即使您的代码要求它至少有4个字符长。幸运的是,您正在使用at()
对其进行索引,这样您就不会得到未定义的行为,但您仍然应该进行检查。我会删除textQuery
中的复选框,并在wordQuery
中添加一个
相关文章:
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- ld:bind_at_load和-bitcode_bundle(Xcode设置ENABLE_bitcode=YES)不能
- 在没有Xcode的情况下在Mac捆绑包中嵌入框架
- Xcode 语义问题引用或以前定义的代码
- 使用 Xcode 将双精度存储在数组C++中
- Xcode 9.4.1 中的 Apple Mach-O 链接器 (id) 错误
- 如何暂停插孔音频客户端
- 各种版本的 Mac OSX 向后兼容性如何?(Xcode C++
- MacOS 上的 Xcode 11 项目不在一个函数中使用 sin 和 cos:未定义的符号"___sincosf_stret"
- 在 vscode 中运行时无法暂停或设置断点
- C++ 如何暂停控制台输出,以便用户可以在继续之前阅读信息
- macOS 是内置在 clang 编译器中还是内置于 xcode ide 中?
- 如何将循环暂停几秒钟?
- Qt - 从XCode上传到MacOS AppStore
- 在 xcode 10 上找不到 stdlibc++ 标头错误
- cmake add_custom_command + Xcode:多输出 = 多命令调用
- 在 xcode 上使用 stdlib.h
- 我可以在没有Xcode的情况下在Mac上使用带有C++的虚幻引擎吗?
- 为什么不支持 Xcode 1.5?
- C++在Xcode中暂停