PICTURE OF ACTOR

So in this nine week class I started with doing this WSQ09. I started reviewing in creating and calling functions in C++.#Mastery06, #Mastery07, #Mastery16 Use of recursion for repetitive algorithms, #Mastery17 When to use what type of repetition in a program, #Mastery18 Creation and use of Arrays/ Vectors in C++.

Futhermore in this assignment we have two new mastery topics covered #Mastery19 Creation and use of strings and #Mastery21 Reading and writing of text files.

What I did for this numeric program is solving the problem to the user by writing a function that receives as parameter the name of a file (this would be a string value like data.txt) and this function counts the number of lines and the number of characters in the file which it returns as a single value (but with two values). I will want to look at how to create/define and return a struct value from a function and how to open and read text files line by line.

Hence, the resources I need it to solve this program are here:

ken bauer

How to convert string to char

C++ Tutorial for Beginners 43 – How to Read from a .txt file using C++

The following photograph shows the solution to this problem:

wsq9v1

wsq9v2wsq9v3wsq9v4

Picture of author

So at first I wrote the same structure of the program just did the same as what i did in Hello World: Second Class, Second Blog (Blog of the second class 12/01/17) and Hello World.cpp,  #WSQ01 Post Fun with Numbers 16/01/17 and WSQ1.cpp#WSQ02 Post Temperature 23/01/17 and WSQ02.cpp#WSQ03 Post Pick a Number 23/01/17 and WSQ03.cpp#WSQ04 Post Sum of Numbers 23/01/17 and WSQ04.cpp#WSQ05 Six Tutorial On To Functions 12/02/17 and WSQ05.cpp#WSQ06 Factorial Calculator 12/02/17 and WSQ06.cpp and #WSQ07 Lists 03/03/17 and WSQ07.cppwhere i explained the application of the #MasteryTopic01 that it is for comments that could be very useful when debugging and #MasteryTopic04 that is basic output for data. Also I did some other mastery topics.

What i first put in the code was the library to call all the fuctions <iostream> of inputs and outputs of data in languague C++ #MasteryTopic06 Calling Functions and #MasteryTopic08 Importing and using libraries.

#include <iostream> //Library to call all the
//fuctions of inputs and outputs of data in languague
//C++ #MasteryTopic01

Next, I need a Library to call all the fuctions of reading AND writing files in languague
C++ #MasteryTopic21

#include <fstream> //Library to call all the
//fuctions of reading AND writing files in languague
//C++ #MasteryTopic21

Next, I need a Library to call all the fuctions of strings of data in languague C++ #MasteryTopic19

#include <string>//Library to call all the
//fuctions of strings of data in languague
//C++ #MasteryTopic19

Then i had to write a command that everytime goes with the instructions of input and outputs of data that is call std but with this command helps writing these std out of the main because the machine factorizes them. Futhermore, you need a library to use all math expresions.

using namespace std; //In C++ we need a command
// that everytime goes with the instructions of input and outputs
//of data that is call std but with this command helps writing
//these std out of the main because the machine factorizes them

Next I initialize the begin of my program int main()

int main() {

After that I had to declarate a filetype varible STRUCKANS from the struct function

filetype struckans; //Declarate filetype varible STRUCKANS from the struct function

Hence, we need to know what is a struct function that is a new function where you can return or send more compound values to other functions.This is the structure of a function where you have subrutines in order to make different tasks, in this case we have a STRUCT function with the name «filetype». In addition you need to write outside of the main function and use the ‘;’.

struct filetype {//This is the structure of a function where you have subrutines in order to make
//different tasks, in this case we have a STRUCT function with the name "filetype".

int counter;//Declarate decimal variable counter
int lengthfinal;//Declarate decimal variable lengthfinal
int backup;//Declarate decimal variable backup

}; //end of struct

Futhermore, we need to declarate string varible filename and initialized it to «data.txt» #Mastery19 and #Mastery20

string filename="data.txt"; //Declarate string varible filename and initialized it to "data.txt" #Mastery19 and #Mastery20

Next, THIS ASSIGNATION HELPS TO CALL THE FUNCTION OF «input» TO MAKE THE TASK OF COUNT THE VALUE OF THE NUMBER WITH THE HELP OF THE VARIABLE filetype ‘struckans’ with the parameter filename.c_str() IN ORDER to convert string to char as a parameter #Mastery06 and #Mastery07

struckans = input(filename.c_str()); //THIS ASSIGNATION HELPS TO CALL THE FUNCTION OF "input" TO MAKE THE TASK OF
//COUNT THE VALUE OF THE NUMBER WITH THE HELP OF THE VARIABLE filetype 'struckans' with the parameter filename.c_str()
//IN ORDER to convert string to char as a parameter #Mastery06 and #Mastery07

After this last action, the program escapes from the main function to the filetype input function in order to make the task of the total value of the numbers with the help of the string variable converted to a char variable.This is the structure of a function where you have subrutines in order to make different tasks, in this case we have a STRUCT function with the name «input» and with CHAR parameter*filename where I am using the #Mastery19 Creation and use of strings in C++ in order to have struct results inside this function. Therefore, I am using the char in order to reference the string parameter filename with combining it with a loop.

filetype input(const char *filename) //This is the structure of a function where you have subrutines in order to make
//different tasks, in this case we have a STRUCT function with the name "input" and with CHAR parameter
//*filename where I am using the #Mastery19 Creation and use of strings in C++ in order to have struct results inside this function.
//Therefore, I am using the char in order to reference the string parameter filename with combining it with a loop.

Next, Inside of the struct filetype function ‘input’ I have to declarate filetype varible STRUCKANS from the struct function. (This is a local variable).

filetype struckans; //Declarate filetype varible STRUCKANS from the struct function

Next, we need to declarate a file variable in order to write or read #Mastery21 Reading and writing of text files.Therefore, this is a variable called file where you also make the function of giving the value of filename to file that in this case is data.txt

ifstream file(filename); ///Declarate a file variable in order to write or read #Mastery21 Reading and writing of text files
//Therefore, this is a variable called file where you also make the function of giving the value of filename to file that in this case
//is data.txt

Next, we need to declarate string variable line

string line; //Declarate string variable line

After that, we need to initialize the value of the struct variable counter to 0 #Mastery19 and #Mastery21

struckans.counter=0; //Initialize the value of the struct variable counter to 0 #Mastery19 and #Mastery21

Next, we need to initialize the value of the struct variable backup to 0 #Mastery19 and #Mastery21

struckans.backup=0; //Initialize the value of the struct variable backup to 0 #Mastery19 and #Mastery21

Then we have a command of data in form of text space

cout<<endl; //command of data in form of text space

Next I need to use the #Mastery10 use of the if statement. This command allows the condition if the file is open and when this condition will be true, the tasks inside of the statement will execute. In this case if the value file is open this will occur:

if(file.is_open()) //#Mastery10 use of the if statement //This command allows the condition if the file is open and when this
//condition will be true, the tasks inside of the statement will execute. In this case if the value file is open
//this will occur:

Inside of the condition we have a command, with this command while is utilized as a loop WHILE execute all the instruccions that has this loop until that does not complete that we get all the characters of lines in the file

while(getline(file,line)) //With this command while is utilized as a loop WHILE execute all the
//instruccions that has this loop until that does not complete that we get all the characters of lines in the file

how many characters in this line?. Therefore, we need this is a string operation between ‘line’ and ‘struckans’ and stands for do the length of the file and saved it in struct variable ‘lengthfinal’ #Mastery19 Creation and use of strings

// how many characters in this line?
struckans.lengthfinal = line.length();//This is a string operation between 'line' and 'struckans' and stands for do the length of the
//file and saved it in struct variable 'lengthfinal' #Mastery19 Creation and use of strings

After that, we need this is a math operation between ‘backup’ and ‘lengthfinal’ and stands for do the sum between those values and saved it in struct variable ‘BACKUP’

struckans.backup = struckans.backup + struckans.lengthfinal;//This is a math operation between 'backup' and 'lengthfinal' and stands
//for do the sum between those values and saved it in struct variable 'BACKUP'

Then, we need this math operation between ‘counter’ and ‘1’ and stands for
do the sum between those values and saved it in struct variable ‘counter’. THIS IS FOR HAVE A COUNTER OF THE NUMBER OF LINES THAT THE data.txt FILE HAS.


struckans.counter = struckans.counter + 1;//This is a math operation between 'counter' and '1' and stands for
//do the sum between those values and saved it in struct variable 'counter'

Next, we have a command of data in form of text space

cout<<endl;//command of data in form of text space

After that, we need this text we show the authentic output of the value entered of struckans.counter in the terminal interface #MasteryTopic04. THIS IS TO TELL THE USER THE NUMBER OF THE LINES OF DATA.

cout<<"The number of the lines of data.txt="<<struckans.counter<<endl; //In
//this text we show the authentic output of the value entered of struckans.counter in the terminal interface #MasteryTopic04

Then, we need a command of data in form of text space

cout<<endl;//command of data in form of text space

After that, we need this text where shows the authentic output of the value entered of struckans.BACKUP in the terminal interface #MasteryTopic08

cout<<"The number of the lines of data.txt="<<struckans.counter<<endl; //In
//this text we show the authentic output of the value entered of struckans.counter in the terminal interface #MasteryTopic04

Next, we need a command of data in form of text space

cout<<endl;//command of data in form of text space

Finally, we have a command that makes that the file is close

file.close(); //command that make that the file is close

Next, the program goes back to the main function and I need this command allows label the final of the function main ()

return 0; // This command allows label the final of the function main ()

And i also resolved it by doing this video tutorial that i made reafirming these parts at this link :#WSQ09 Tutorial 9 Multipart Data and Files 10/03/17 and WSQ9.cpp

Finally with the command ./a. out tells the terminal to run de C++ file:

charactes

This code for solution will be here and in Github:

Link in Github:WSQ9.cpp

If you do not want the WSQ9.cpp there is it in here with comments. The comments are very useful to detect errors and create new things #Mastery01 #Masterytopic01 #MasteryTopic01 #MASTERY01 #MASTERYTOPIC01 :

Also in the last class I learned how to present my C++ in a better ‘readable’ way after doing it #Mastery02 #Masterytopic02 #MasteryTopic02 #MASTERY02 #MASTERYTOPIC02:

#include <iostream> //Library to call all the
//fuctions of inputs and outputs of data in languague
//C++ #MasteryTopic01
#include <fstream> //Library to call all the
//fuctions of reading AND writing files in languague
//C++ #MasteryTopic21

#include <string>//Library to call all the
//fuctions of strings of data in languague
//C++ #MasteryTopic19

using namespace std; //In C++ we need a command
// that everytime goes with the instructions of input and outputs
//of data that is call std but with this command helps writing
//these std out of the main because the machine factorizes them

struct filetype {//This is the structure of a function where you have subrutines in order to make
//different tasks, in this case we have a STRUCT function with the name "filetype".

int counter;//Declarate decimal variable counter
int lengthfinal;//Declarate decimal variable lengthfinal
int backup;//Declarate decimal variable backup

}; //end of struct

filetype input(const char *filename) //This is the structure of a function where you have subrutines in order to make
//different tasks, in this case we have a STRUCT function with the name "input" and with CHAR parameter
//*filename where I am using the #Mastery19 Creation and use of strings in C++ in order to have struct results inside this function.
//Therefore, I am using the char in order to reference the string parameter filename with combining it with a loop.
{
filetype struckans; //Declarate filetype varible STRUCKANS from the struct function

ifstream file(filename); ///Declarate a file variable in order to write or read #Mastery21 Reading and writing of text files
//Therefore, this is a variable called file where you also make the function of giving the value of filename to file that in this case
//is data.txt

string line; //Declarate string variable line
struckans.counter=0; //Initialize the value of the struct variable counter to 0 #Mastery19 and #Mastery21
struckans.backup=0; //Initialize the value of the struct variable backup to 0 #Mastery19 and #Mastery21
cout<<endl; //command of data in form of text space

if(file.is_open()) //#Mastery10 use of the if statement //This command allows the condition if the file is open and when this
//condition will be true, the tasks inside of the statement will execute. In this case if the value file is open
//this will occur:
{


while(getline(file,line)) //With this command while is utilized as a loop WHILE execute all the
//instruccions that has this loop until that does not complete that we get all the characters of lines in the file
{

// how many characters in this line?
struckans.lengthfinal = line.length();//This is a string operation between 'line' and 'struckans' and stands for do the length of the
//file and saved it in struct variable 'lengthfinal' #Mastery19 Creation and use of strings

struckans.backup = struckans.backup + struckans.lengthfinal;//This is a math operation between 'backup' and 'lengthfinal' and stands
//for do the sum between those values and saved it in struct variable 'BACKUP'

struckans.counter = struckans.counter + 1;//This is a math operation between 'counter' and '1' and stands for
//do the sum between those values and saved it in struct variable 'counter'


}




cout<<endl;//command of data in form of text space
cout<<"The number of the lines of data.txt="<<struckans.counter<<endl; //In
//this text we show the authentic output of the value entered of struckans.counter in the terminal interface #MasteryTopic04
cout<<endl;//command of data in form of text space


cout<<"The number of the characters of data.txt="<<struckans.backup<<endl;//In
//this text we show the authentic output of the value entered of struckans.BACKUP in the terminal interface #MasteryTopic08
cout<<endl;//command of data in form of text space


file.close(); //command that make that the file is close

}



}

int main() {

filetype struckans; //Declarate filetype varible STRUCKANS from the struct function
string filename="data.txt"; //Declarate string varible filename and initialized it to "data.txt" #Mastery19 and #Mastery20


struckans = input(filename.c_str()); //THIS ASSIGNATION HELPS TO CALL THE FUNCTION OF "input" TO MAKE THE TASK OF
//COUNT THE VALUE OF THE NUMBER WITH THE HELP OF THE VARIABLE filetype 'struckans' with the parameter filename.c_str()
//IN ORDER to convert string to char as a parameter #Mastery06 and #Mastery07


return 0; // This command allows label the final of the function main ()

}
Anuncio publicitario

11 comentarios en “#WSQ09 Multipart Data and Files 10/03/17 and WSQ09.cpp

Deja una respuesta

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Salir /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Salir /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Salir /  Cambiar )

Conectando a %s