Hi, I\'ve wrote this program in hopes that it would workperfectly.I\'ve complete
ID: 3617779 • Letter: H
Question
Hi, I've wrote this program in hopes that it would workperfectly.I've completed the program so far, but there's a problemwith reading from the input file. The program is to read from a file number of lines, it skipsthe ones start with a space, and stores the ones that doesn't startwith a space. when outputting it should only output the first 4letters of the line that doesn't start with space, inexample: if the input file consists of the following Data: AAA BBBB the output would be: Label Adress ==== ===== BBBB 1tho, if I start it, it would go in an infinite loop? can someone help please? the code is as shown below:
#include "stdafx.h" #include <iostream> #include <fstream> #include <string> using namespace std;
int main()
{
ifstream infile("tiny2.txt");//initialize the inputfile. int PC= 0;// to count the lines int symAddress = 0;// to store the address of the lines withno spaces from "PC" string symName;// to store the names int symSize = 0;// to indicate the size of the file string name;//to check if the lines starts with space, if notit stores it and assigns it to symName.
infile.open("tiny2.txt");//to open the file.
//outputting label lines. cout << "Label" <<""<<"Address"<<endl; cout<<"======="<<""<<"======="<<endl; while (name !=" ZZZ ")//check if any of thelines is equal to " ZZZ " which is the mark end ofthe line. { getline(infile,name);//read line while(name.substr (0,4)==" ")//check andsee if the input is not equal to space. { PC++;//increment PC. getline(infile,name)//input another line. } symName=name.substr (0,4);//get the first 4 characters of thestring variable.
symAddress=PC ;//assign the value of PC to thesymAddress to show the right address. symSize++; //increment the size.
cout << symName<<""<<symAddress<<endl;//output the values. PC++;//increment . } return 0; } The program is to read from a file number of lines, it skipsthe ones start with a space, and stores the ones that doesn't startwith a space. when outputting it should only output the first 4letters of the line that doesn't start with space, inexample: if the input file consists of the following Data: AAA BBBB the output would be: Label Adress ==== ===== BBBB 1
tho, if I start it, it would go in an infinite loop? can someone help please? the code is as shown below:
#include "stdafx.h" #include <iostream> #include <fstream> #include <string> using namespace std;
int main()
{
ifstream infile("tiny2.txt");//initialize the inputfile. int PC= 0;// to count the lines int symAddress = 0;// to store the address of the lines withno spaces from "PC" string symName;// to store the names int symSize = 0;// to indicate the size of the file string name;//to check if the lines starts with space, if notit stores it and assigns it to symName.
infile.open("tiny2.txt");//to open the file.
//outputting label lines. cout << "Label" <<""<<"Address"<<endl; cout<<"======="<<""<<"======="<<endl; while (name !=" ZZZ ")//check if any of thelines is equal to " ZZZ " which is the mark end ofthe line. { getline(infile,name);//read line while(name.substr (0,4)==" ")//check andsee if the input is not equal to space. { PC++;//increment PC. getline(infile,name)//input another line. } symName=name.substr (0,4);//get the first 4 characters of thestring variable.
symAddress=PC ;//assign the value of PC to thesymAddress to show the right address. symSize++; //increment the size.
cout << symName<<""<<symAddress<<endl;//output the values. PC++;//increment . } return 0; } #include "stdafx.h" #include <iostream> #include <fstream> #include <string> using namespace std;
int main()
{
ifstream infile("tiny2.txt");//initialize the inputfile. int PC= 0;// to count the lines int symAddress = 0;// to store the address of the lines withno spaces from "PC" string symName;// to store the names int symSize = 0;// to indicate the size of the file string name;//to check if the lines starts with space, if notit stores it and assigns it to symName.
infile.open("tiny2.txt");//to open the file.
//outputting label lines. cout << "Label" <<""<<"Address"<<endl; cout<<"======="<<""<<"======="<<endl; while (name !=" ZZZ ")//check if any of thelines is equal to " ZZZ " which is the mark end ofthe line. { getline(infile,name);//read line while(name.substr (0,4)==" ")//check andsee if the input is not equal to space. { PC++;//increment PC. getline(infile,name)//input another line. } symName=name.substr (0,4);//get the first 4 characters of thestring variable.
symAddress=PC ;//assign the value of PC to thesymAddress to show the right address. symSize++; //increment the size.
cout << symName<<""<<symAddress<<endl;//output the values. PC++;//increment . } return 0; }
Explanation / Answer
please rate - thanks //#include "stdafx.h" #include #include #include using namespace std; int main() { ifstreaminfile;// ("tiny2.txt");//initialize the input file. int PC= 0;// to count the lines int symAddress = 0;// to store the address of the lines with nospaces from "PC" string symName;// to store the names int symSize = 0;// to indicate the size of the file string name;//to check if the lines starts with space, if not itstores it and assigns it to symName. int m; infile.open("tiny2.txt");//to open the file. if(infile.fail()) //is it ok? { coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.