ورود

View Full Version : چند سوال از آرایه ای از نوع کاراکتری



fshb_ 1370
یک شنبه 18 بهمن 1388, 19:37 عصر
سلام
اگر یه آرایه از نوع کاراکتری رو به وجود بیارم و موقع وارد کردن داده، چند تا کلمه که با space از هم جدا میشن رو وارد کنم، توی آرایه فقط اولین کلمه قبل از space رو توی آرایه قرار میده؟
اگه جواب درسته، پس چرا تو خط 27 کلمه there رو چاپ میکنه؟(ورودی hello there است) خط 26 چی کار میکنه؟


// Fig. 7.12: fig07_12.cpp
// Treating character arrays as strings.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
char string1[ 20 ]; // reserves 20 characters
char string2[] = "string literal"; // reserves 15 characters
// read string from user into array string1
cout << "Enter the string \"hello there\": ";
cin >> string1; // reads "hello" [space terminates input]
// output strings
cout << "string1 is: " << string1 << "\nstring2 is: " << string2;
cout << "\nstring1 with spaces between characters is:\n";
// output characters until null character is reached
for ( int i = 0; string1[ i ] != '\0'; i++ )
cout << string1[ i ] << ' ';
cin >> string1; // reads "there"
cout << "\nstring1 is: " << string1 << endl;
return 0; // indicates successful termination
} // end main

fshb_ 1370
دوشنبه 19 بهمن 1388, 20:50 عصر
منظورم از خط 26 و 27 این دو خط:

cin >> string1; // reads "there"
cout << "\nstring1 is: " << string1 << endl;