CSC 123 chapter 6 my pro lab

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Write the necessary preprocessor directive to enable the use of the exit function.

#include <cstdlib>

To create an input file in a program , the program must contain the __________ directive

#include <fstream>

Write the necessary preprocessor directive to enable the use of file streams.

#include <fstream>

fstream read by char

#include <fstream> #include <string> #include <iostream> using namespace std; int main () { char ch; ifstream myFile ("sky.txt") ; if (! myFile) // Always test file open { cout << "Error opening output file" << endl; return -1; } while (! myFile.eof()) //Loop through lines { myFile.get(ch); cout << ch; ch=' '; } myFile.close(); // Always close the file return 0; }

File Appender

#include <fstream> #include <string> #include <iostream> using namespace std; int main ( ) { string str = "\tThe Ballad of Reading Gaol"; str.append("\n\t\t\tOscar Wilde 1898"); ofstream myFile("sky.txt", ios::app); if (! myFile) { cout << "Error opening output file" << endl; return -1; } myFile << str << endl; myFile.close() ; return 0; }

fstream read by one line

#include <fstream> #include <string> #include <iostream> using namespace std; int main () { string str; ifstream myFile ("sky.txt") ; if (! myFile) // Always test file open { cout << "Error opening output file" << endl; return -1; } while (! myFile.eof()) //Loop through lines { getline(myFile, str); cout << str << endl; } myFile.close(); // Always close the file return 0; }

Write the necessary preprocessor directive to enable the use of the stream manipulators like setw and setprecision.

#include <iomanip>

/* Character operator */

#include <iostream> #include <cctype> using namespace std; int main () { cout << "Enter a character:"; char ch; cin >> ch; fflush(stdin); cout<< "you entered " << ch << endl; if (islower(ch)) { cout << "It is a lower case letter " << endl; cout << "it's equivalent upper case letter is " << static_cast<char>(toupper(ch)) << endl; } else if (isupper(ch)) { cout << "It is an upper case letter " << endl; cout << "it's equivalent lower case letter is " << static_cast<char>(tolower(ch)) << endl; } else if (isdigit(ch)) { cout << "It is a digit character" << endl; } else if (isspace(ch)) { cout << "It is a space character" << endl; } else { cout << "It is a symbol character" << endl; } getchar (); return 0; }

cin.get()

If you want the computer to recognize the Enter key as the input, you would use the _______. will read the next character entered, even whitespace used same as getline, but with a character and not a string

Define an object named infile that can be used to read data into program variables from a file.

ifstream infile;

anyway, it reads from a given text file, and everything works fine with it, but there is a bug: if the file contents ends with a space, the last word is repeated. therefore, if the text file contains "hello " the output to the screen is "hello hello". but if the text file just contains "hello" it outputs "hello" just fine..

This is because a whitespace is still data, so after you read your last word, if there is a whitespace in the file then "eof()" does not return true. But the ">>" operator you use to copy the stream from "filein" to "b" generally ignores whitespace. You could clear "b" at the start of every iteration in your loop. Inseart a line between Lines 13 and 14 and put b = ""; There by setting it to nothing. This way if you hit a white space at the end of file, you won't see a repeat of the last word read because "b" won't contain anything.

getline (string) in C++ getline() is a standard library function in C++ and is used to read a string or a line from input stream. It is present in the <string> header.

Prototype Syntax: Syntax 1: istream& getline (istream& is, string& str, char delim); is : It is an object of istream class and tells the function about the stream from where to read the input from. str : It is a string object, the input is stored in this object after being read from the stream. delim : It is the delimitation character which tells the function to stop reading further input after reaching this character. Syntax 2: istream& getline (istream& is, string& str); The second declaration is almost same as that of the first one. The only difference is, it does not accept any delimitation character. This function consider new line or ('\n') character as the delimitation character.

A variable c of type char has been declared. Write the necessary code to read in the next character from standard input and store it in c, regardless of whether is a whitespace character.

cin.get(c);

Assume that a,b and c are char variables have been declared. Write some code that reads in the first character of the next line into a, the first character of the line after that into b and the first character of the line after that into c. Assume that the lines of input are under 100 characters long.

cin.ignore(100, '\n') >> a; cin.ignore(100, '\n') >> b; cin.ignore(100, '\n') >> c;

Assume that c is a char variable has been declared. Write some code that reads in the first character of the next line into c. Assume that the lines of input are under 100 characters long.

cin.ignore(100,'\n')>>c;

Given a bool variable isReadable write some statements that assign true to isReadable if the file "topsecret" exists and can be read by the program and assigns false to isReadable otherwise.

ifstream fin; fin.open("topsecret"); if(fin.fail()) { isReadable=false; } else { isReadable=true; }

Given that corpdata is an ifstream object that has been used for reading data and that there is no more data to be read, write the necessary code to complete your use of this object.

corpdata.close();

Given three variables, a, b, c, of type double that have already been declared and initialized, write a statement that prints each of them on the same line, separated by one space, in such a way that scientific (or e-notation or exponential notation) is avoided. Each number should be printed with 5 digits to the right of the decimal point. For example, if their values were 4.014268319, 14309, 0.00937608, the output would be: |4.01427x14309.00000x0.00938 NOTE: The vertical bar, |, on the left above represents the left edge of the print area; it is not to be printed out. Also, we show x in the output above to represent spaces-- your output should not actually have x's!

cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(5); cout<<a<<" "<<b<<" "<<c;

Given six variables, a1, b1, a2, b2, a3, b3, of type double that have already been declared and initialized, write some code that prints their values (in the order given above) in 15-position columns. The values of the "a" variables are printed in the first column, those of the "b" variables in the second column. Each value is displayed in a way that avoids scientific (also called exponential notation or e-notation) and in each case there are five digits shown after the decimal point, regardless of the value. For example, if the values of a1, a2, a3 were 1, 2, 3 respectively and the values of b1, b2, b3 were 0.000000814, 26890, 123.456 respectively |xxxxxxxx1.00000xxxxxxxx0.00000 |xxxxxxxx2.00000xxxx26890.00000 |xxxxxxxx3.00000xxxxxx123.45600 NOTE: The vertical bar, |, on the left above represents the left edge of the print area; it is not to be printed out. Also, we show x in the output above to represent spaces-- your output should not actually have x's!

cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(5); cout<<setw(15)<<a1<<setw(15)<<b1<<endl; cout<<setw(15)<<a2<<setw(15)<<b2<<endl; cout<<setw(15)<<a3<<setw(15)<<b3<<endl;

Given three variables, a, b, c, of type double that have already been declared and initialized, write some code that prints each of them in a 15 position field on the same line, in such a way that scientific (or e-notation or exponential notation) is avoided. Each number should be printed with 5 digits to the right of the decimal point. For example, if their values were 24.014268319, 14309, 0.00937608, the output would be: |xxxxxxx24.01427xxxx14309.00000xxxxxxxx0.00938 NOTE: The vertical bar, |, on the left above represents the left edge of the print area; it is not to be printed out. Also, we show x in the output above to represent spaces-- your output should not actually have x's!

cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(5); cout<<setw(15)<<a<<setw(15)<<b<<setw(15)<<c;

Given three variables, k, m, n, of type int that have already been declared and initialized, write some code that prints each of them left-justified in a 9-position field on the same line. For example, if their values were 27, 987654321, -4321, the output would be: |27xxxxxxx987654321-4321xxxx NOTE: The vertical bar, |, on the left above represents the left edge of the print area; it is not to be printed out. Also, we show x in the output above to represent spaces-- your output should not actually have x's!

cout.setf(ios::left); cout<<setw(9)<<k<<setw(9)<<m<<setw(9)<<n;

Write a statement that reads 5 successive integers into these variables that have already been declared: x1 x2x3x4 x5. Then write a statement that prints each out on its own line so that they form a right-justified column with a 5-digit width. If any of the integers are 5-digits in size, then they will start at the very beginning of their lines. For example: |54213 | 8713 | 23 | 147 | 15

cout.setf(ios::right); cin>>x1>>x2>>x3>>x4>>x5; cout<<setw(5)<<x1<<endl; cout<<setw(5)<<x2<<endl; cout<<setw(5)<<x3<<endl; cout<<setw(5)<<x4<<endl; cout<<setw(5)<<x5<<endl;

Given three variables, a, b, c, of type double that have already been declared and initialized, write some code that prints each of them in scientific (also known as e-notation or exponential notation) in a 10 position field on the same line. Each number should be printed with 3 digits to the right of the decimal point. For example, if their values were 987654321, 1234, 0.00987654321, the output would be: |x9.877e+08x1.234e+03x9.877e-03 NOTE: The vertical bar, |, on the left above represents the left edge of the print area; it is not to be printed out. Also, we show x in the output above to represent spaces-- your output should not actually have x's!

cout.setf(ios::scientific); cout.precision(3); cout<<" "<<a<<" "<<b<<" "<<c;

Assume that m is an int variable that has been given a value. Write a statement that prints it out in a print field of 10 positions.

cout<<setw(10)<<m;

Given three variables, k, m, n, of type int that have already been declared and initialized, write some code that prints each of them in a 9 position field on the same line. For example, if their values were 27, 987654321, -4321, the output would be: |xxxxxxx27987654321xxxx-4321 NOTE: The vertical bar, |, on the left above represents the left edge of the print area; it is not to be printed out. Also, we show x in the output above to represent spaces-- your output should not actually have x's!

cout<<setw(9)<<k<<setw(9)<<m<<setw(9)<<n;

D O C R(W) C

declare open check read(write) close

Given a bool variable fileExists write the necessary code to set this variable to true if the file "crucial_data" exists and sets it to false otherwise.

ifstream indata; indata.open("crucial_data"); if(indata.fail()) { fileExists =false; } else { fileExists =true; }

Define two objects named infile1 and infile2 that can be used to read data into program variables from two different files.

ifstream infile1,infile2;

exit and return

exit can works everywhere; return only works in the "main" function;

When reading a file by character. You should include cctype as a library

false

You can read and write from the same file at the same time

false

Which of these writes the value of quantity to fout.txt given the file object fout

fout << quantity << endl;

given fout is a file object for a file fout.txt Which is a legal statement

fout.close();

given the ofstream fout which of the choices adds to the data in the file at the end

fout.open("fout.txt",ios::app);

getline differs from cout

getline use new line as a delimiter

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.

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

You use the class ______ to instantiate an input file object

ifstream

Given four files named winter2003.txt, spring2003.txt, summer2003.txt, fall2003.txt, define four ifstream objects named wntr, sprg, sumr, and fall, and use them, respectively, to open the four files for reading.

ifstream wntr,sprg,sumr,fall; wntr.open("winter2003.txt"); dprg.open("spring2003"); sumr.open("summer2003.txt"); fall.open("fall2003.txt");

Given an ifstream object named input, associate it with a file named hostdata by opening the file for input.

input.open("hostdata");

Given the availability of an ifstream object named input, write the other statements necessary to read an integer into a variable datum that has already been declared from a file called rawdata. Assume that reading that one integer is the only operation you will carry out with this file. (Note: write just the statements, do not define a main function.)

input.open("rawdata"); input>>datum;

Given an ifstream object named input1, associate it with a file named winterdata.txt by opening the file for reading.

input1.open("winterdata.txt");

Use an ifstream object named indata to read the first three integers from a file called lottowins and write each number to standard output, on a line by itself. Assume that this is the extent of the input that this program will do.

int a,b,c; indata.open("lottowins"); indata>>a; cout<<a; cout<<endl; indata>>b; cout<<b; cout<<endl; indata>>c; cout<<c; cout<<endl;

Given the availability of an ifstream object named indata and an ofstream object named outdata, write the other statements necessary to read one integer from a file called currentsales and write twice its value into a file called projectedsales. Assume that this is the extent of the input and output that this program will do.

int a; indata.open("currentsales"); indata>>a; outdata.open("projectedsales"); outdata<<2*a; indata.close(); outdata.close();

Given the availability of a file named numbers write the statements necessary to read an integer from standard input and then read in that many values from numbers and display their total.

int a=0,b=0,total=0; cin>>a; ifstream fin("numbers"); for(int i=0;i<a;i++) { fin>>b; total+=b; } cout<<total;

lab 6 read by a word

int main(){ //make sure while loop to run char another='y'; //loop for the program while(another=='y'||another=='Y') { //storage string uname,bname,gname; int urank=0,grank=0,brank=0; //declare the file ifstream fin; fin.open("babynames2012.txt"); //check the file if(fin.fail()) { cout<< "Opening file error.\n"; exit(-1); } //prompt cout<< "\nWhat is the Name (first letter CAP) \n"; cin>> uname; cout<<endl; //read name in the file while(fin>>urank) { fin>>bname; fin>>gname; //output the result for boy name if (uname==bname) { brank=urank; cout<< bname<<" is "<<urank<< " popular boy name.\n"; cout<< endl; } //output the result for girl name if(uname==gname) { grank=urank; cout<< gname<<" is "<<urank<< " popular girl name.\n"; cout<< endl; } } //situation for not including in the file if(grank==0&&brank==0) { cout<<uname<< " is not a popular name.\n"; } //prompt cout<< "\nTry another name or exit the program?\n"; cout<< "(Y to input or N to exit)\n"; cin>>another; //situation for invalid input while(another!='y'&&another!='Y'&&another!='n'&&another!='N') { cout<< "\nInvalid Input."; cout<< "\nTry another name or exit the program?\n"; cout<< "(Y to input or N to exit)\n"; cin>>another; } //close the file fin.close(); } //situation to quit if(another=='n'||another=='N') { cout<<"\nQuit the program."; cout<<endl; exit(0); } return 0; }

Which mode tells the computer to open a file to write over with input

ios:app

eof()

means "end of file." true if the stream's eofbit error state flag is set (which signals that the End-of-File has been reached by the last input operation). false otherwise.

Given an fstream object named menu, associate it with a file named todaysmenu.txt for output in a way that truncates (erases) any existing data in the file.

menu.open("todaysmenu.txt", ios::out|ios::trunc); menu.open("todaysmenu.txt"); menu<<"";

Given four files named asiasales2009.txt, europesales2009.txt, africasales2009.txt, latinamericasales2009.txt, define four ofstream objects named asia, europe, africa, and latin, and use them, respectively, to open the four files for writing.

ofstream asia,europe,africa,latin; asia.open("asiasales2009.txt"); europe.open("europesales2009.txt"); africa.open("africasales2009.txt"); latin.open("latin2009sales.txt");

Given a file named execution.log write the necessary code to add the line "Program Execution Successful" to the end of the file.

ofstream fout; fout.open("execution.log", ios::app); fout<< "Program Execution Successful"; fout.close();

Define two objects named outfile1 and outfile2 that can be used to write data from program variables to two different files.

ofstream outfile1,outfile2;

Define an object named outfile that can be used to write data from program variables to a file.

ofstream outfile;

Given the availability of an ofstream object named output, write the other statements necessary to write the string "3.14159" into a file called pi. (Do not define a main function.)

output.open("pi"); output<<"3.14159";

Given an ofstream object named output, associate it with a file named yearsummary.txt by opening the file for writing.

output.open("yearsummary.txt");

Given an ofstream object named output, associate it with a file named yearsummary.txt by opening the file for appending.

output.open("yearsummary.txt", ios::app);

Given an ofstream object named output, associate it with a file named yearsummary.txt for output in a way that truncates (erases) any existing data in the file.

output.open("yearsummary.txt", ios::out|ios::trunc);

Read first a user's given name followed by the user's age from standard input. Then use an ofstream object named outdata to write this information separated by a space into a file called outdata. Assume that this is the extent of the output that this program will do.

string name; int age; cin>>name>>age; outdata.open("outdata"); outdata<<name<<" "<<age; outdata.close(); string name,age; cin>>name; cin>>age; outdata.open("outdata"); outdata<<name<<" "<<age<<"\n"; outdata.close();

Given an fstream object named todo, associate it with a file named todolist by opening the file for appending.

todo.open("todolist", ios::app);

Which string does NOT match the regular expression: "v.x.*"?

v.x.* * vx v.x vxxxxxxx vvx

Write the definition of a function named panic. The function prints the message "unrecoverable error" and then terminates execution of the program, indicating failure..

void panic() { cout<<"unrecoverable error"; cout<<endl; exit(-1); } void panic() { cout<<"unrecoverable error"; cout<<endl; exit(1); }

Given the file object fout, write a while statement to read a file to the end.

while ( ! fout.eof())


Ensembles d'études connexes

u. s. government parts of the constitution

View Set

BUS470 WARSING TEST 1 QUIZ & CONNECT QUESTIONS

View Set

Agile Project Management Essentials

View Set

Chapter 54: Management of Patients With Kidney Disorders

View Set

business law bul 2241 fiu ch 4 tort law

View Set

Intro to Public Health: Midterm 1, Chapter 1

View Set

Online school first eight chapters

View Set