#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讀取輸入的數字,必須用空白符間隔開