File Input Help Please!
Dec 4, 2014 at 9:36pm UTC
I need this program to be able to read a file with 3 items on a line, e.g. "ATLANTICCITY 10123 12343". I used "1" as a way to see if my program was loading the data correctly but it keeps coming out to something like "00192020 0018e198 0018a310". What am I doing wrong and how can I fix it?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void Load(string Name[],double Outages[], double TotalCust[], int &count)
{
ifstream in;
in.open("data.txt" );
for (int i=0; i<600; i++)
{
in >> Name[i];
in >> Outages[i];
in >> TotalCust[i];
count++;
}
in.close();
}
int main()
{
cout << "===================================" << endl;
cout << " LIPA ASSIST " << endl;
cout << "===================================" << endl;
// Declaration of variables
string Name[2000];
double Outages[2000];
double TotalCust[2000];
int count = 0;
Load(Name, Outages, TotalCust, count);
while (true )
{
cout << endl;
cout << "1 - Query City" << endl;
cout << "2 - City With Most Outage" << endl;
int choice;
cout << "What do you want to do: " ;
cin >> choice;
if (choice == 1)
{
for (int i=0; i<200; i++)
{
cout << Name << " " << Outages << " " << TotalCust << endl;
}
}
if (choice == 2)
{
}
if (choice == 0)
{
cout << "BYE BYE!" << endl;
break ;
}
}
return 0;
}
Dec 4, 2014 at 9:55pm UTC
cout << Name
Name is an array. What do you think it should output?
To get some value from array, you need to use subscript on it:
1 2
std::cout << Name[0];
//Outputs first value in the array
Same for others arrays
Dec 5, 2014 at 1:54pm UTC
Yeah I realized that after I put it yesterday so i changed it to:
cout << Name[i] << " " << Outages[i] << " " << TotalCust[i] << endl;
but now instead of 3 random numbers with spaces in between, I get 2 numbers with a space between.
Dec 5, 2014 at 2:03pm UTC
, I get 2 numbers with a space between.
You got:
empty string (i.e. nothing),
space ,
number ,
space ,
number .
Check if file was properly open. Check for any failed reads. Check your file structure.
Topic archived. No new replies allowed.