#include <iostream.h>
#include <conio.h>
int main()
{
char a[20], b[] = "string literal"; cout << "Enter a string:";
cin >> a;
cout << "a: " << a << endl
<< "b: " << b << endl
<< "a with spaces between characters is:" << endl; for (int i=0; a[i]!='\0'; i++)
{
cout << a[i] << ' ';
}
cin >> a;
cout << "\na is: " << a << endl;
cout << endl; getch();
return 0;
}运行结果如下:
Enter a string:come here
a: come
b: string literal
a with spaces between characters is:
c o m e
a is: here为什么a在遇到come后的空格是就中断了?这时候用cin的话不需要人为去输入,而是自动去获取余下字符here,这里面有什么道理?
#include <conio.h>
int main()
{
char a[20], b[] = "string literal"; cout << "Enter a string:";
cin >> a;
cout << "a: " << a << endl
<< "b: " << b << endl
<< "a with spaces between characters is:" << endl; for (int i=0; a[i]!='\0'; i++)
{
cout << a[i] << ' ';
}
cin >> a;
cout << "\na is: " << a << endl;
cout << endl; getch();
return 0;
}运行结果如下:
Enter a string:come here
a: come
b: string literal
a with spaces between characters is:
c o m e
a is: here为什么a在遇到come后的空格是就中断了?这时候用cin的话不需要人为去输入,而是自动去获取余下字符here,这里面有什么道理?
char str[100];
cin.getline(str,100,'\n');