Input Array from file.

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
const int ARRAY_SIZE = 10;
const int MAXL = 40;
char Narray[ARRAY_SIZE] [MAXL];
double numbers[ARRAY_SIZE];
int count;
ifstream inputFile;
//import
inputFile.open("in.txt");

/* for(count=0; count < ARRAY_SIZE; count++)
{
inputFile.getline(Narray[count], MAXL);
inputFile >> numbers[count];
} */


while (!inputFile.eof())
{
inputFile.getline(Narray[count], MAXL);
inputFile >> numbers[count] >> ws;
count++;
}

inputFile.close();

//sort
double Temp =0;
int x;
int i;
for(i=0;i<ARRAY_SIZE-1;i++)
for(x=1;x<ARRAY_SIZE-i;x++)
if(numbers[x]<numbers[x-1])
{
Temp=numbers[x];
numbers[x]=numbers[x-1];
numbers[x-1]=Temp;
}
int countx;
for(countx=0;countx<ARRAY_SIZE;countx++)
cout << numbers[countx] << " ";
cout << endl;


//output

ofstream outputFile;
outputFile.open("SavedNumb.txt");
int countxy;
for(countxy=0;countxy<ARRAY_SIZE;countxy++)
outputFile << numbers[countxy] << "\n ";
outputFile.close();

system("pause");


return 0;
}



I'm trying to pull this information from a file and separate the names to an array, and the numbers to another. Then I want to sort the times with the bubble sort code. Right now the import is bringing in some weird data. Would someone run this, and see maybe how I can fix it.

Input File:

Archer, Adam
44.33
Baker, Bob
47.55
Cheeze, Chuck
45.82
Duck, Donny
46.11
Elk, Eli
47.51
Fish, Freddie
45.19
Juice, Josh
10.12
Voster, Van
30.92
Moose, Mayor
48.92
Larrz, Larison
99.90





having sort of skimmed/half-read your code, here are a few ideas I had. I was unable to compile as I'm not on my own computer at the moment.

When you take input, take input into an std::string and then use string::strtok to separate that string into separate pieces (there's documentation for that on this site, that's possibly the easiest way for you to do this). You could then use your separate strings in comparisions, or add something to the beginning of each word that denotes number or word (i.e. start all words with "^.^" and all numbers with "###" and compare just string[1] through string[3].

Also, you could use a switch or other control statement while getting your input (again on this site) and check the first character of each word (so it'd combine sorting and importing). It would (in psuedocode) look something like this:

If !eof


within a loop:

if(array[current_char]>=65&&array[current_char]<=90) //checks for capital letters
|
--> loop of any form:
-------> place characters into array that holds names
-------> check if current character is a space (if no, continue)
if yes->break

else if(array[current_char]==" "||array[current_char]=="."||) //continue this to check for punctuation
-->reiterate home loop

else //must be a number
--->store to number array
------>check for space (if no, continue)
if yes--->break



I know that's really rough, so if you have questions feel free to ask them
Topic archived. No new replies allowed.