A<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2FConsole%2FConsole_Read_Validation</id>
		<title>C/Console/Console Read Validation - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2FConsole%2FConsole_Read_Validation"/>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C/Console/Console_Read_Validation&amp;action=history"/>
		<updated>2026-04-09T10:41:38Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://cppe.ru/index.php?title=C/Console/Console_Read_Validation&amp;diff=196&amp;oldid=prev</id>
		<title> в 14:20, 25 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C/Console/Console_Read_Validation&amp;diff=196&amp;oldid=prev"/>
				<updated>2010-05-25T14:20:56Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 14:20, 25 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://cppe.ru/index.php?title=C/Console/Console_Read_Validation&amp;diff=197&amp;oldid=prev</id>
		<title>Admin: 1 версия:&amp;#32;Импорт контента...</title>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C/Console/Console_Read_Validation&amp;diff=197&amp;oldid=prev"/>
				<updated>2010-05-25T10:22:25Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия: Импорт контента...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Characters in the format control string==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
void main()&lt;br /&gt;
{&lt;br /&gt;
   int i = 10;&lt;br /&gt;
   int j = 10;&lt;br /&gt;
   int k = 10;&lt;br /&gt;
   float fp1 = 10.0;&lt;br /&gt;
   k = scanf(&amp;quot;fp1 = %f i = %d %d&amp;quot;, &amp;amp;fp1, &amp;amp;i , &amp;amp;j);&lt;br /&gt;
   printf(&amp;quot;\nCount of values read = %d&amp;quot;, k);&lt;br /&gt;
   printf(&amp;quot;\nfp1 = %f\ti = %d\tj = %d\n&amp;quot;, fp1, i, j);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Exercising formatted input==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
void main()&lt;br /&gt;
{&lt;br /&gt;
   int i = 10;&lt;br /&gt;
   int j = 10;&lt;br /&gt;
   int k = 10;&lt;br /&gt;
   float fp1 = 10.0;&lt;br /&gt;
   char word1[20] = &amp;quot; &amp;quot;;&lt;br /&gt;
   char word2[20] = &amp;quot; &amp;quot;;&lt;br /&gt;
   k = scanf(&amp;quot;%f %d %d %[abcdefghijklmnopqrstuvwxyz] %*1d %s&amp;quot;,&lt;br /&gt;
     &amp;amp;fp1, &amp;amp;i , &amp;amp;j, word1, word2);&lt;br /&gt;
   printf(&amp;quot;\nCount of values read = %d\n&amp;quot;, k);&lt;br /&gt;
   printf(&amp;quot;\nfp1 = %f   i = %d   j = %d&amp;quot;, fp1, i, j);&lt;br /&gt;
   printf(&amp;quot;\nword1 = %s   word2 = %s\n&amp;quot;, word1, word2);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Narrow down the input by setting scanf==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
  char str[80];&lt;br /&gt;
  printf(&amp;quot;Enter letters, anything else to stop\n&amp;quot;);&lt;br /&gt;
  scanf(&amp;quot;%[a-zA-Z]&amp;quot;, str);&lt;br /&gt;
  printf(str);&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Narrow the input: letter and space==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
  char str[80];&lt;br /&gt;
  printf(&amp;quot;Enter letters and spaces\n&amp;quot;);&lt;br /&gt;
  scanf(&amp;quot;%[a-zA-Z ]&amp;quot;, str);&lt;br /&gt;
  printf(str);&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Read a certain length of string==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
  int i;&lt;br /&gt;
  char str[80], str2[80];&lt;br /&gt;
  scanf(&amp;quot;%20s&amp;quot;, str);&lt;br /&gt;
  printf(&amp;quot;%s&amp;quot;,  str );&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Reading monetary amounts separated by commas and spaces==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
  Ensure that the input format string specifies that &amp;quot;$&amp;quot;,&lt;br /&gt;
  spaces, and commas are ignored.&lt;br /&gt;
*/&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
void main()&lt;br /&gt;
{&lt;br /&gt;
  double amounts[4] = {0.0};&lt;br /&gt;
  double total = 0.0;&lt;br /&gt;
  int i = 0;&lt;br /&gt;
   printf(&amp;quot;Enter the four amounts:\n&amp;quot;);&lt;br /&gt;
   for(i = 0 ; i&amp;lt;4 ; i++)&lt;br /&gt;
   {&lt;br /&gt;
     scanf(&amp;quot;%*[ ,$]%lf&amp;quot;, &amp;amp;amounts[i]);&lt;br /&gt;
     total += amounts[i];&lt;br /&gt;
   }&lt;br /&gt;
     &lt;br /&gt;
   printf(&amp;quot;The total of the input is: $%.2lf\n&amp;quot;, total);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Reading types of strings from the keyboard==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
Beginning C, Third Edition&lt;br /&gt;
 By Ivor Horton&lt;br /&gt;
 ISBN: 1-59059-253-0&lt;br /&gt;
 Published: Apr 2004&lt;br /&gt;
 Publisher: apress&lt;br /&gt;
*/&lt;br /&gt;
/*&lt;br /&gt;
type 1: a sequence of lowercase letters followed by a digit. e.g. number1&lt;br /&gt;
type 2: two words that both begin with a capital letter and have a hyphen between them. e.g. Seven-Up&lt;br /&gt;
type 3: a decimal value. e.g. 7.35&lt;br /&gt;
type 4: a sequence of upper or lower case letters and spaces. e.g. Oliver Hardy&lt;br /&gt;
type 5: a sequence of any characters except spaces and digits that does not&lt;br /&gt;
        start with a letter. e.g. floating-point&lt;br /&gt;
The simple approach is to use the fact that each string after the first starts with&lt;br /&gt;
a character that does not appear in the previous string. Thus you can specify the characters for &lt;br /&gt;
each string as anything that is not the first character in the string that follows.&lt;br /&gt;
This approach would allow illegal characters in the string to be read.&lt;br /&gt;
e.g. the first string could be read as any lowercase letter or digit.&lt;br /&gt;
To positively ensure the strings are in the form specified is much more work as the&lt;br /&gt;
code here shows.&lt;br /&gt;
If you wanted to allow erroneous data to be re-entered, it would be easiest if you&lt;br /&gt;
require each input to be on a separate line.&lt;br /&gt;
*/&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
void main()&lt;br /&gt;
{&lt;br /&gt;
   char word1[50];&lt;br /&gt;
   char word2[50];&lt;br /&gt;
   char word3[50];&lt;br /&gt;
   char word4[50];&lt;br /&gt;
   char word5[50];&lt;br /&gt;
   char temp[50];&lt;br /&gt;
   int k = 0;&lt;br /&gt;
   printf(&amp;quot;Enter the five strings:\n&amp;quot;);&lt;br /&gt;
   /* Read first string, first the lowercase letters, then the digit */&lt;br /&gt;
   k = scanf(&amp;quot; %[abcdefghijklmnopqrstuvwxyz]%1[0123456789]&amp;quot;, word1, temp);&lt;br /&gt;
   if(k&amp;lt;2)&lt;br /&gt;
   {&lt;br /&gt;
     printf(&amp;quot;Incorrect input for first string - program terminated.\n&amp;quot;);&lt;br /&gt;
     exit(1);&lt;br /&gt;
   }&lt;br /&gt;
   strcat(word1,temp);&lt;br /&gt;
   /* Read second string, first the uppercase letter, then the following&lt;br /&gt;
      letters, then the hyphen, then the uppercase letter, finally the&lt;br /&gt;
      following letters.                                                &lt;br /&gt;
   */&lt;br /&gt;
   k = scanf(&amp;quot; %1[ABCDEFGHIJKLMNOPQRSTUVWXYZ]&amp;quot;, word2);&lt;br /&gt;
   if(k&amp;lt;1)&lt;br /&gt;
   {&lt;br /&gt;
     printf(&amp;quot;Incorrect input for second string - program terminated.\n&amp;quot;);&lt;br /&gt;
     exit(1);&lt;br /&gt;
   }&lt;br /&gt;
   scanf(&amp;quot;%[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]&amp;quot;, temp);&lt;br /&gt;
   strcat(word2,temp);&lt;br /&gt;
   k = scanf(&amp;quot;%1[-]&amp;quot;, temp);&lt;br /&gt;
   if(k&amp;lt;1)&lt;br /&gt;
   {&lt;br /&gt;
     printf(&amp;quot;Incorrect input for second string - program terminated.\n&amp;quot;);&lt;br /&gt;
     exit(1);&lt;br /&gt;
   }&lt;br /&gt;
   k = scanf(&amp;quot; %1[ABCDEFGHIJKLMNOPQRSTUVWXYZ]&amp;quot;, temp);&lt;br /&gt;
   if(k&amp;lt;1)&lt;br /&gt;
   {&lt;br /&gt;
     printf(&amp;quot;Incorrect input for second string - program terminated.\n&amp;quot;);&lt;br /&gt;
     exit(1);&lt;br /&gt;
   }&lt;br /&gt;
   strcat(word2,temp);&lt;br /&gt;
   scanf(&amp;quot;%[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]&amp;quot;, temp);&lt;br /&gt;
   strcat(word2,temp);&lt;br /&gt;
   /* Read the third string. I cheat a bit because I read this assuming it is a well-formed&lt;br /&gt;
      decimal string. To verify that is really is, you could read it as a decimal&lt;br /&gt;
      value and then convert it to a string.&lt;br /&gt;
   */&lt;br /&gt;
   k = scanf(&amp;quot; %[0123456789.-]&amp;quot;, word3);&lt;br /&gt;
   if(k&amp;lt;1)&lt;br /&gt;
   {&lt;br /&gt;
     printf(&amp;quot;Incorrect input for third string - program terminated.\n&amp;quot;);&lt;br /&gt;
     exit(1);&lt;br /&gt;
   }&lt;br /&gt;
   /* Read the fourth string. */&lt;br /&gt;
   scanf(&amp;quot;%[ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]&amp;quot;, word4);&lt;br /&gt;
   if(k&amp;lt;1)&lt;br /&gt;
   {&lt;br /&gt;
     printf(&amp;quot;Incorrect input for fourth string - program terminated.\n&amp;quot;);&lt;br /&gt;
     exit(1);&lt;br /&gt;
   }&lt;br /&gt;
   /* Read the fifth string. You must exclude newline, otherwise it will be&lt;br /&gt;
      rad as part of the string.&lt;br /&gt;
   */&lt;br /&gt;
   scanf(&amp;quot; %[^ 0123456789\n]&amp;quot;, word5);&lt;br /&gt;
   printf(&amp;quot;The input strings read are: \n%s  %s  %s  %s  %s\n&amp;quot;,&lt;br /&gt;
            word1, word2, word3, word4, word5);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Read only a range of char==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
  int i;&lt;br /&gt;
  char str[80], str2[80];&lt;br /&gt;
  scanf(&amp;quot;%d%[abcdefg]%s&amp;quot;, &amp;amp;i, str, str2);&lt;br /&gt;
  printf(&amp;quot;%d %s %s&amp;quot;, i, str, str2);&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
  &lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>