Theory question about fields

Hi, I'm reading "Programming Principles and Practice Using C++" by B.Stroustrup and I'm at chapter 23 and trying to answer all the review questions. I'm kind of a little stuck on this 1 question so here it is

12. What is the difference between an empty field and a nonexistent field? Give Two examples.


So far I understand (if correctly) from previous chapters that field is kind of a formatting and space what your output takes in lets say console (because I'm only writing some console applications as exercises now). But nothing is mentioned about empty and nonexistent fields.

Here is the quote from the book
Using scientific and fixed formats, a programmer can control exactly how much space a value takes up on output. That's clearly useful for printing tables, etc. The equivalent mechanism for integer values is called fields. You can specify exactly how many character positions an integer value or string value will occupy using the "set field width" manipulator setw(). . .


So can anyone help me with these "empty fields and nonexistent fields"? What exactly are they?

EDIT : the chapter I'm reading is called "Text manipulation" and Bjarne is introducing regular expressions here to read the table (4 columns and lot of rows) to see if all the info in the table is matching the pattern
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <regex>

void display_field( const std::string& text, const std::string& name )
{
    // the field that we are interested in is the marked subexpression ([[:alnum:]]*)
    // ie. zero or more alphanumeric characters immediately after 'name='
    const std::regex regex( name + '=' + "([[:alnum:]]*)" ) ;
    std::smatch match_results ;
    std::regex_search( text, match_results, regex ) ;

    std::cout << "'" << name << "' == " ;
    if( match_results.empty() ) std::cout << "  --- (nonexistent field)\n\n" ;
    else if( match_results[1].length() == 0 ) std::cout << "''  --- (empty field)\n\n" ;
    else std::cout << "'" << match_results[1] << "'  --- (non-empty field)\n\n" ;
}

int main()
{
    const std::string text = "name=etrusks email= posts=168 phone= " ;
    for( std::string fn : { "name", "email", "posts", "age", "phone", "address" } )
        display_field( text, fn ) ;
}

http://coliru.stacked-crooked.com/a/ede44151eb5848e4
Wow, this is exactly what I was looking for (this is even better) so thank you a lot man :) Really really thanks for your effort :)
Hey etrusks,

I am one chapter behind you. But actually, I skipped Chapters 12 to 16 because I could not get the graphics to run. Were you able to get the graphics to run? If you did, please look at my post Window Graphics Errors dated May 31, 2015.
Hey etrusk,

I am one chapter behind you. But actually, I skipped Chapters 12 to 16 because I could not get the graphics to run. Were you able to get the graphics to run? If you did, please look at my post Windows Graphics Errors dated May 31, 2015.

Topic archived. No new replies allowed.