Inputting values into an array from a file with C++

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Inputting values into an array from a file with C++



The file does open and I get the message "File opened successfully". However I can't input data from the array in file "random.csv" into my inputFile object.



The data in random.csv is:


Boston,94,-15,65

Chicago,92,-21,72

Atlanta,101,10,80

Austin,107,19,81

Phoenix,112,23,88

Washington,88,-10,68



Here is my code:


#include "main.h"

int main()

string item; //To hold file input
int i = 0;
char array[6];
ifstream inputFile;
inputFile.open ("random.csv",ios::in);

//Check for error
if (inputFile.fail())
cout << "There was an error opening your file" << endl;
exit(1);
else
cout << "File opened successfully!" << endl;


while (i < 6)
inputFile >> array[i];
i++;


for (int y = 0; y < 6; y++)
cout << array[y] << endl;



inputFile.close();

return 0;





while (i < 6) purpose?
– macroland
5 mins ago


while (i < 6)





Hint: char array[6] can only hold 5 characters (1 byte each)
– CinCout
4 mins ago


char array[6]





Don't forget arrays of chars should be terminated with null!
– TheEngineer
40 secs ago









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS