Professional Documents
Culture Documents
STRUCTURED PROGRAMMING
Validations
Input validation
Having seen the material on loops and functions, it is
nowpossible to makeprograms with a logical processof
repetition according to very precise conditions.
int value;
cout << "Enter a numerical value: ";
cin >> value; // transfers the entered value
The buffer will be cleared up to the line feed character (\n, Enter) if the value can
be completely placed in the variable. The variable value now contains 12.
The input buffer
int readInt();
Function calls without parameters
Then we can use the function as follows:
int main()
{
int val1 = readInt();
int val2 = readInt();
int val3 = readInt();
cout << val1 << val2 << val3 << endl;
return 0;
}
Other types of input
◻So far, we’ve mostly only used cin for numerical input
◻We might also want the user to input other types of
data, such as strings
◻However, this comes with its own set of concerns
https://www.youtube.com/watch?v=yMwA4u7eXEY
https://www.youtube.com/watch?v= o1jE0JSiE
Character strings
◻ When requesting a value by input, we have gotten in
the habit of using cin to accomplish the task, but in the
caseof character strings, cin isnot adapted for the
task.
◻ In a string, there are often spaces and tabulations
(tabs). Thecin object considers these characters as
field separators, or field delimiters (characters
prompting the termination of reading from a
variable).
Character strings
For the following code, if we input Hello World…
#include <iostream>
using namespace std;
int main()
{
char str[20];
cout << "Enter a message: ";
cin >> str;
cout << str << endl;
return 0;
}
Character strings
int main()
{
char str[20];
cout << "Enter a message: ";
cin >> str;
cout << str << endl;
cout << "Enter another message: ";
cin >> str;
cout << str << endl;
return 0;
}
Character strings
int main()
{
char str[20];
cout << "Enter a message: ";
cin.getline(str, 20);
cout << str << endl;
cout << "Enter another message: ";
cin.getline(str, 20);
cout << str << endl;
return 0;
}
Character strings
int main()
{
char str[20];
cout << "Enter a message: ";
cin.getline(str, 20);
cout << str << endl;
return 0;
}
Character strings
On the face of it, the only problem that has occurred is
that only one part of the string is copied into the array.
But in the background, something else has happened!
Character strings
#include <iostream>
using namespace std;
int main()
{
char str[20];
cout << "Enter a message: ";
cin.getline(str, 20);
cout << str << endl;
cout << "Enter another message: ";
cin.getline(str, 20);
cout << str << endl;
return 0;
}
Character strings
int main()
{
char str[20];
cout << "Enter a message (max 20 characters): " <<
endl;
cin.getline(str, 20);
cout << str << endl;
int val;
cout << "Enter a number: ";
cin >> val;
cout << val << endl;
return 0;
Character strings
int main()
{
int val;
cout << "Enter a number: ";
cin >> val;
cout << val << endl;
char str[20];
cout << "Enter a message (max 20 characters): " <<
endl;
cin.getline(str, 20);
cout << str << endl;
return 0;
Character strings
The behaviour is not correct. The program does not give us
the time to enter a message to fill the string with.
The reason is that, when we use the cin object to input
numerical values, after the input, there remainsin the
buffer the character ‘\n’ (which is in fact a line return).
Character strings
Upon subsequently calling cin.getline(), this remaining
character isautomatically interpreted by the function, and
applies a line return without giving the user the opportunity
to input anything.
int main()
{
int val;
cout << "Enter a number: ";
cin >> val;
cout << val << endl;
cin.ignore(512, '\n');
char str[20];
cout << "Enter a message (max 20 characters): " << endl;
cin.getline(str, 20);
cout << str << endl;
return 0;
}
Conclusion