#include<iostream>
using namespace std;
const int DECLARED_SIZE=10;
int main()
{
cout<<"Enter up to ten letters followed by a period"<<endl;
char letter_box[DECLARED_SIZE],next;
int index=0;
cin>>next;
while((next!='.')&&(index<DECLARED_SIZE))
{
letter_box[index]=next;
index++;
cin>>next; //读入字符不必用空白符间隔开
}
int number_used=index;
cout<<"Here they are backwards:"<<endl;
for(index=number_used-1;index>=0;index--)
cout<<letter_box[index];
cout<<endl;
return 0;
}
可以这样输入 abc.
也可以这样输入 a b c .
还可以这样输入
a
b
c
.
对于输入字符来说,cin读取输入时结果都是一样的
#include<iostream>
using namespace std;
const int DECLARED_SIZE=10;
int main()
{
cout<<"Enter up to ten number followed by a period"<<endl;
int int_array[DECLARED_SIZE],next;
int index=0;
cin>>next;
while((next>=0)&&(index<DECLARED_SIZE))
{
int_array[index]=next;
index++;
cin>>next;//读入数字要用空白符间隔开
}
int number_used=index;
cout<<"Here they are backwards:"<<endl;
for(index=number_used-1;index>=0;index--)
cout<<int_array[index];
cout<<endl;
return 0;
}
只能后两种输入方法 1 2 3 -1
或
1
2
3
-1
用cin读取输入的数字,必须用空白符间隔开