Given an int variable x write some statements that attempt to open a file named “table20″ and read a value into x; if that turns out not to be possible your code should then read avalue from standard input into x.

LANGUAGE: C++

CHALLENGE:

Given an int variable x write some statements that attempt to open a file named “table20″ and read a value into x; if that turns out not to be possible your code should then read avalue from standard input into x.

SOLUTION:



ifstream filename;
filename.open("table20");
if (filename.fail()){
    cin >> x;
}else{
    filename >> x;
}