In: Computer Science
Name your c++ file Word_LastNameFirstName.cpp. Create a struct that has a word and the length of the word. Next, use the struct to store the word read from a text file call “word.txt” and the length of the word. Print out the word x number of times based on the length of the word as shown below. Also, print a statement with the length and determine if the string length has an odd number or even number of characters. You need to create your own text file and place in the proper folder.
For instance, if the file contains the string "Program", its length is 7, 7 is a odd number and it will be printed 7 times like the below. Print to the console like below
OUTPUT :
Program
Program
Program
Program
Program
Program
Program
The word "Program" has a length of 7 and has an odd number of character
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
// word.txt
Program
______________________
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
//Create Struct Word
struct Word
{
//Declaring variables
string word;
int length;
};
int main() {
//Declaring variables
string word;
ifstream dataIn;
//Open the input file
dataIn.open("word.txt");
//checking whether the file name is valid or not
if(dataIn.fail())
{
cout<<"** File Not Found **";
return 1;
}
else
{
//Reading the word from the input file
dataIn>>word;
//Creating the instance of struct Word
struct Word wd;
wd.word=word;
wd.length=word.length();
dataIn.close();
for(int i=0;i<wd.length;i++)
{
cout<<wd.word<<endl;
}
cout<<"The word \""<<wd.word<<"\"
has a length of "<<wd.length<<" and has an ";
if(wd.length%2==0)
{
cout<<"even";
}
else
{
cout<<"odd";
}
cout<<" number of character"<<endl;
}
return 0;
}
____________________________
output:
_______________Could you plz rate me well.Thank You