c++ - How do I modify the following code so that I'm actually reading in the contents of the file into an array then printing out the array? -
the following code reads in text file prints screen. works want first read in contents array print out contents , i'm not sure how so.
#include <fstream>  #include <iostream>  #include <string> using namespace std;  int main()  {  string input;  //open file
std::ifstream infile;  infile.open("input.txt");  while (true)  {  //get input
infile >> input; //avoid repetition of last line:
if(infile.eof() ) break; //print input on screen
if (input == "a")     std::cout << "variable: " << input << "\n";  if (input == "=")     std::cout << "assignment: " << input << "\n";  if (input == "3")     std::cout << "integer: " << input << "\n";  if (input == ";")     std::cout << "semicolon: " << input << "\n";  if (input == "+")     std::cout << "operator: " << input << "\n";  if (input == "5")     std::cout << "integer: " << input << "\n";  if (input == "31")     std::cout << "integer: " << input << "\n";  if (input == "b")     std::cout << "variable: " << input << "\n";  if (input == "*")     std::cout << "operator: " << input << "\n";  if (input == "a")     std::cout << "variable: " << input << "\n";  if (input == "4")     std::cout << "integer: " << input << "\n";  if (input == "-")     std::cout << "operator: " << input << "\n";  if (input == "quit")     std::cout << "word: " << input << "\n"; }  //close file
infile.close(); return 0; }  end of program
you store elements in std::vector<int> , push_back each element 1 one until end of file.then loop through vector display elements.
if prefer using simple arrays, have count how many elements have in file, allocate memory array new, , store elements array. don't forget delete[] your_array in end. 
Comments
Post a Comment