从输入中读取单词序列。使用"Quit"终止,按输入顺序打印,不要打印两次单词。
Read a sequence of words from input. Use "Quit" to terminate, print in order they were entered and don't print a word twice.
如果有人能提供比我更好的代码,那就太好了,尽管工作也很糟糕。另外,我的代码有问题!!当我打印我的新数组arr
时,在输出中,当有一个重复的单词时,数组的位置被跳过,并为它输出一个空白空间。
bat,
cat,
mat,
//here the word bat is not printed again, but instead i get an empty space
rat,
quit,
也请只使用指针,数组或字符串,因为我还没有研究向量,列表,映射等。谢谢你。
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
int main()
{
string word[100];
string arr[100];
int i=0,j,k,m,n;
while(1)
{
cout<<"enter word: n";
cin>>word[i];
if(word[i]=="quit")
break;
i++;
}
for(j=i,m=0;j>=0,m<=i;m++)
{
for(k=j-1;k>=0;k--)
{
if(word[j] == word[k])
goto start;
}
arr[m]=word[j];
start:
j--;
}
for(n=m;n>0;n--)
cout<<arr[n]<<"n";
getch();
}
修正了一点,我不想花时间去理解你的for(j=i,m=0;j>=0,m<=i;m++)
循环,所以完全重写了它。
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
int main()
{
string word[100];
string arr[100];
int wordCount = 0;
while(1)
{
cout<<"enter word: n";
cin>>word[wordCount];
if(word[wordCount] == "quit")
{
break;
}
++wordCount;
}
int arrCount = 0;
for(int i = 0; i < wordCount; ++i)
{
bool found = false;
for (int j = 0; j < arrCount; ++j)
{
if (arr[j] == word[i])
{
found = true;
break;
}
}
if (!found)
{
arr[arrCount] = word;
++arrCount;
}
}
for(int i = 0; i < arrCount; ++i)
{
cout<<arr[n]<<"n";
}
getch();
}
和永远不要使用goto,如果你的老师看到了,他会很生气的
相关文章:
- 打印时有二叉树问题.用户输入不打印任何内容
- 为什么有时我输入一个整数,程序将第一个输入的数字打印成十进制数?
- 我要求用户输入大小和数组,但是当我打印矢量时,它仅显示'0'作为输出
- 如何打印不带空格的输入整数?
- 为什么无论输入如何,所有 if-else 语句都会打印?
- 使用用户输入打印 6X10 矩阵的代码 - C++
- 如何使用打印到 PDF 打印机在 MFC 和 CView 中以编程方式打印而不提示输入文件名?
- 如何打印输入值
- 带有用户输入的C 文本数组,然后打印输入
- 读取和打印输入和 C 字符串C++
- 如何打印输入而不将其存储在c++中
- SFML—创建游标并打印输入
- c++的新功能,scanf不能正确读取/打印输入
- 如何打印输入的第一个数字?
- 如何自动打印输入的c++函数参数值
- C++:为什么 getline() 不打印输入字符串的最后一个单词?
- 打印输入的最小数字(不包括任何负数)
- 创建一个控制台应用程序,打开TCP/IP连接并用C++、C#或Java打印输入的字符串
- 从文件打印输入数据
- 按倒序打印输入字符串