我在cplusplus.com,tutorialspoint.com,codecall.net帖子以及Stack Overflow上的不同帖子上阅读了文章。
我也通过电子邮件发送给我的教授,在我解释说材料不在书中并且不属于指定的章节之后,他的回答是“我很清楚这些材料不在章节中。我希望学生能够做研究。尝试使用谷歌搜索指针和使用malloc创建动态数组。我得到1,600,000次点击。“
这是我的作业:
使用STL堆栈来反转读入字符串的输入字符行。您的堆栈应包含用户字符串的字符。使用getline()进行输入 - 它需要成为C ++工具库存的一部分。
关于getline的说明:假设我正在执行以下操作 -
This program reverses a string using the STL stack Enter your string of less than 80 characters followed by an ENTER a string input Enter another? 1 = continue. Anything else to stop 1 Enter your string of less than 80 characters followed by an ENTER a second string
只需使用以下循环即可使用此代码:
int go = 1; cout << "This program reverses a string using the STL stack" << endl; while(go == 1){ cout << "Enter your string of less than 80 characters followed by an ENTER" << endl; char* s = (char *)malloc(80); cin.getline(s,81,'\n'); cout << s << endl; cout << "Enter another? 1 = continue. Anything else to stop" << endl; cin >> go; }
试一试,看看会发生什么!还要注意,我从来没有摆脱我用malloc分配的内存 - 你的代码必须摆脱它。此外,malloc在循环外部以获得更高效的代码。
你必须在cin&gt;&gt;之后使用getchar()(cstdio库的一部分)。去;
原因是当您输入1时,您也使用Enter键。当你再次点击getline时,它仍然在缓冲区中!所以你会读到'\ n'
另请注意,当您从STL堆栈中获取某些内容时,必须使用.top()查看它,然后使用.pop()将其删除。
示例:
This program reverses a string using the STL stack Enter your string of less than 80 characters followed by an Enter m&m cheeto mayo oyam oteehc m&m Enter another? 1 = continue. Anything else to stop 1 Enter your string of less than 80 characters followed by an Enter celery lettuce apple elppa ecuttel yrelec Enter another? 1 = continue. Anything else to stop 0 Press any key to continue . . .
以下是我所做的事情: 在循环内外使用 malloc()。 在循环内外使用 free()和 delete()。 尝试创建一个临时的新指针,然后将我的旧指针重新分配给临时指针。
我一直在使用repl.it来完成这项任务,那么为什么这不起作用呢?
在我不断运行程序后,它看起来内存不清楚。它似乎只是将字符添加到我在上一次迭代中输入的内容。
我希望我从这篇文章中涵盖了足够的标准(http://meta.programmers.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems)。如果我不这样做,我道歉。
任何帮助将不胜感激。如果你想向我提供网站的链接,或者可能有其他主题来研究/谷歌帮助我全神贯注。
我的源代码:
#include <iostream>
#include <stdio.h>
#include <stack>
#include <string>
#pragma warning(disable: 4996)
using namespace std;
int main() {
int go = 1;
cout << "This program reverses a string using the STL stack" << endl;
char* s = (char *)malloc(80);
char *temp;
stack <char> reverse;
while(go == 1){
cout << "Enter your string of less than 80 characters followed by an ENTER" << endl;
cin.getline(s,81,'\n');
for (int i = 0; i < 81; i++){
reverse.push(s[i]);
}
for (int i = 0; i < 81; i++){
cout << reverse.top();
reverse.pop();
}
char *temp = (char*)realloc(s, 81*sizeof(char));
if ( temp != NULL )
{
s = temp;
}
else
{
free(s);
}
cout << "\nEnter another? 1 = continue. Anything else to stop" << endl;
cin >>go;
getchar();
cout << endl;
}
system("pause");
return 0;
}
这是我的输出:
This program reverses a string using the STL stack
Enter your string of less than 80 characters followed by an ENTER
Joey
yeoJ
Enter another? 1 = continue. Anything else to stop
1
Enter your string of less than 80 characters followed by an ENTER
Dog
goD
Enter another? 1 = continue. Anything else to stop
1
Enter your string of less than 80 characters followed by an ENTER
JoeyDog
goDyeoJ
Enter another? 1 = continue. Anything else to stop
1
Enter your string of less than 80 characters followed by an ENTER
lkjlkdjf;ldjaflkdjaflkdjlfajdlkfja;sdlkjf
fjklds;ajfkldjafljdklfajdklfajdl;fjdkljkl
Enter another? 1 = continue. Anything else to stop
1
Enter your string of less than 80 characters followed by an ENTER
joey
fjklds;ajfkldjafljdklfajdklfajdl;fjdyeoj
Enter another? 1 = continue. Anything else to stop
答案 0 :(得分:0)
您无需清除记忆。更好的方法是检查字符串的长度。主循环的更正版本(为了清晰起见,我删除了一些不太重要的元素,如cout):
cin.getline(s,80,'\n');
int len = strlen(s);
for (int i = 0; i < len; i++)
reverse.push(s[i]);
for (int i = 0; i < len; i++){
cout << reverse.top();
reverse.pop();
}
所以我用strlen
计算字符串的长度,并在循环中使用结果。请注意,我完全删除了你temp
的东西,因为现在没有意义。