Skip to content

Commit

Permalink
chapter16 cin
Browse files Browse the repository at this point in the history
  • Loading branch information
hzchenxiaobin committed May 28, 2023
1 parent baee8ba commit 6987373
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 0 deletions.
10 changes: 10 additions & 0 deletions chapter17/1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <iostream>

using namespace std;

int main()
{
cout << "Hello world!" << endl;

return 0;
}
21 changes: 21 additions & 0 deletions chapter17/2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>

using namespace std;

int main()
{
char ch;
cin.get(ch);

int ct = 0;
while(ch != '\n')
{
cout << ch;
ct++;
cin.get(ch);
}

cout << ct << endl;

return 0;
}
Binary file added chapter17/a.out
Binary file not shown.
Binary file added chapter17/get_fun
Binary file not shown.
21 changes: 21 additions & 0 deletions chapter17/get_fun.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>

using namespace std;

const int Limit = 255;

int main()
{
char input[Limit];

cout << "Enter a string for getline() processing: " << endl;
cin.getline(input, Limit, '#');
cout << "Here is your input: " << endl;
cout << input << endl;
cout << "---------------------------" << endl;

char ch;
cin.get(ch);
cout << ch << endl;
return 0;
}
49 changes: 49 additions & 0 deletions chapter17/peeker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <iostream>

using namespace std;

int main()
{
char ch;

while (cin.get(ch))
{
if (ch != '#')
cout << ch;
else
{
cin.putback(ch);
break;
}
}

cout << endl;

if (!cin.eof())
{
cin.get(ch);
cout << ch << " is next input character." << endl
}
else
{
cout << "End of input reached." << endl;
exit(0);
}

while (cin.peek() != '#')
{
cin.get(ch);
cout << ch;
}

if (!cin.eof())
{
cin.get(ch);
cout << endl
<< ch << " is next input character." << endl;
}
else
cout << "End of file reached." << endl;

return 0;
}
1 change: 1 addition & 0 deletions chapter17/text
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world!
Binary file added chapter17/truncate
Binary file not shown.
29 changes: 29 additions & 0 deletions chapter17/truncate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <iostream>

using namespace std;

const int SLEN = 10;

int main()
{
char name[SLEN];
char title[SLEN];

cout << "Enter your name: ";
cin.get(name, SLEN);

if(cin.peek() != '\n')
cout << "Sorry, we don't have enough room for " << name << endl;

while(cin.get() != '\n')
continue;

cout << "Dear " << name <<", enter your title: ";
cin.get(title, SLEN);
if(cin.peek() != '\n')
cout << "We were forced to truncate your title." << endl;
cout << "Name: " << name << endl;
cout << "Title: " << title << endl;

return 0;
}

0 comments on commit 6987373

Please sign in to comment.