Reading data file into arrays.

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 5345 Views - Last Post: 24 April 2010 - 08:12 PM Rate Topic: -----

#1 VyrossNakizo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 24-April 10

Reading data file into arrays.

Posted 24 April 2010 - 01:23 PM

I've got an issue reading my data file into an array. The file opens just fine, but when i do my reading, i get illegal indirection errors.

Here's my code(the bit that's giving me a headache):

while(!input.eof()){
		for(int j=0; j<49; j++){
			input.get(sCarray, 25);
			for(int h=0; h<24; h++){
				if(sCarray[h] == ","){
					sCarray[h] = "";
				}
			}
			states[j] = sCarray;

			for(int i=0; i<2; i++){
				input >> stats [j] [i];
			}
		}
	}

Explanation: sCarray is a char array with 25 values, which is supposed to copy the first 25 chars in each line of the file, then put each set into a string value of states. the other for-loop is used to put the series of ints that follows the text data in the file.

my file is organized like this:

Nebraska, 2 211 144

the comma is used to indicate the end of the text data (but this might not be necessary, im not sure).

The errors im getting are
error C2446: '==' : no conversion from 'const char *' to 'int'
error C2040: '==' : 'int' differs in levels of indirection from 'const char [2]'
error C2440: '=' : cannot convert from 'const char [1]' to 'char'


All of my errors point to the use of an integer inside the [] to indicate index. this is how you do it... right? evidently not, it isnt working :(

What's going on here?
please help!

This post has been edited by VyrossNakizo: 24 April 2010 - 01:26 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Reading data file into arrays.

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Reading data file into arrays.

Posted 24 April 2010 - 01:31 PM

if(sCarray[h] == ","){


Use double quotes for character arrays (or STL strings). When working with one character, use single quotes:
if (sCarray[h] == ',') {


EDIT: this won't work too:
sCarray[h] = "";


Firstly, because of the double quotes. But if you use something like this:
sCarray[h] = '';


won't work too, because you have an empty character constant, which is invalid in C/C++.

This post has been edited by sarmanu: 24 April 2010 - 01:42 PM

Was This Post Helpful? 0
  • +
  • -

#3 VyrossNakizo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 24-April 10

Re: Reading data file into arrays.

Posted 24 April 2010 - 01:42 PM

View Postsarmanu, on 24 April 2010 - 12:31 PM, said:

if(sCarray[h] == ","){


Use double quotes for character arrays (or STL strings). When working with one character, use single quotes:
if (sCarray[h] == ',') {




-facepalm-

Thanks, LOL.

lets see if it works now...
Was This Post Helpful? 0
  • +
  • -

#4 VyrossNakizo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 24-April 10

Re: Reading data file into arrays.

Posted 24 April 2010 - 01:50 PM

oof, when i get to the end of file (!input.eof()) it doesn't register. it just starts all over again...
Was This Post Helpful? 0
  • +
  • -

#5 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Reading data file into arrays.

Posted 24 April 2010 - 02:03 PM

Use while(input.good()){ instead of while(!input.eof()){. If input.get(sCarray, 25); faults the loop will never reach EOF. You should almost never control loops with a check for EOF because of this.

When reading a stream inside a loop it's good practice to check the state of the stream after an operation:

...
for(int j=0; j<49; j++){
    input.get(sCarray, 25);
    if (!input.good()) {
        // do whatever error handling needed
    }
    for(int h=0; h<24; h++){
...


This post has been edited by n8wxs: 24 April 2010 - 02:03 PM

Was This Post Helpful? 0
  • +
  • -

#6 VyrossNakizo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 24-April 10

Re: Reading data file into arrays.

Posted 24 April 2010 - 02:24 PM

my teacher (and the book) dont even mention .good().

thanks!
Was This Post Helpful? 0
  • +
  • -

#7 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Reading data file into arrays.

Posted 24 April 2010 - 02:30 PM

View PostVyrossNakizo, on 24 April 2010 - 01:24 PM, said:

my teacher (and the book) dont even mention .good().

thanks!

See ios - State flag functions:
Was This Post Helpful? 0
  • +
  • -

#8 VyrossNakizo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 24-April 10

Re: Reading data file into arrays.

Posted 24 April 2010 - 03:14 PM

whole new problem now. When i try to put each char array into a string, which i then put into my string array, it doesnt work.

to be more specific: it reads the first 15 chars (what i want(i think)), puts them into a string (holder), and then i want holder to go into states at position j.
here's what i want states[] to look like:
(index) (value)
0 Alabama
1 Alaska
2 Arizona

etc.

what'm i doing wrong?

while(input.good()){
		for(int j=0; j<49; j++){
			input.get(sCarray, 15);
			string holder(sCarray, 15);
			states[j] = holder;
			
			for(int i=0; i<3; i++){
				input >> stats[j][i];
			}
			input.ignore();
		}
		input.close();
	}

Was This Post Helpful? 0
  • +
  • -

#9 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Reading data file into arrays.

Posted 24 April 2010 - 03:18 PM

Quote

When i try to put each char array into a string, which i then put into my string array, it doesnt work.
So after your program runs, it crashes your computer, right? Do you get a blue screen, or a black screen?
Was This Post Helpful? 0
  • +
  • -

#10 VyrossNakizo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 24-April 10

Re: Reading data file into arrays.

Posted 24 April 2010 - 03:21 PM

neither. it fills the string array with chars. what i get is states[] = 'A', 'l', 'a','b','a','m','a',' ',' ',' ', ' ,' '

i DO in fact want the blank spaces, but i want the whole thing concatenated into a STRING, so i can put 49 more strings in with it.

EDIT: to be more clear, it doesnt crash or freeze it just gives me garbage.

also, my int 2d array isnt working; it fills up with 3 numbers in the way i want in the rows (or maybe columns) but it never rotates which column (or maybe row) it uses.

i dont really know which is which (columns and rows) with 2d arrays - i'm familiar with coordinates, but not so much with spreadsheets.

EDIT AGAIN:
im using Microsoft VS 2005, in case it matters

This post has been edited by VyrossNakizo: 24 April 2010 - 03:26 PM

Was This Post Helpful? 0
  • +
  • -

#11 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Reading data file into arrays.

Posted 24 April 2010 - 03:27 PM

Oh, so let me rephrase your question. Given a C string, how do you idiomatically get a std::string?

Well, simple. When you read documentation, you find that string constructor can take in a C string.

EDIT:

Quote

it just gives me garbage.
So, when you run the program, the printer starts up, printing out pages of 0s and 1s, thus filling up your garbage bin?

Quote

also, my int 2d array isnt working; it fills up with 3 numbers in the way i want in the rows (or maybe columns) but it never rotates which column (or maybe row) it uses.
Why is that not working? I mean, if you program it to not rotate, it's clearly not going to rotate by design.

Quote

i dont really know which is which (columns and rows) with 2d arrays
There's no such thing as 2D arrays.

Quote

but not so much with spreadsheets.
Spreadsheets have nothing to do with this program.

This post has been edited by Oler1s: 24 April 2010 - 03:30 PM

Was This Post Helpful? 0
  • +
  • -

#12 VyrossNakizo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 24-April 10

Re: Reading data file into arrays.

Posted 24 April 2010 - 03:36 PM

come again? that went way over my head, i just want to know how to make an array of chars into a single string, not a series of chars (which seems like what it's doing).

the entire reason i put "string holder" in there was so it would get the value, and then i could put holder (which is supposed to be a string at this point) into my states[index]. then i use the for loop to change the index, and put a new string value in, etc...


EDIT: no, of course it isnt printing. im getting garbage; crap; useless stuff; stuff i already knew; stuff that isnt useful to me or my program.

there is SUPPOSED to be such a thing as a 2d array, because we had an entire 2 hour lecture about the damned things. syntax(according to my notes):

int array1[ROWS][COLS];

and i AM rotating it. there's a small for-loop for the numbers on each line of the text file, plus the outer for-loop that rotates through the lines. the first bit of code snags the string in front of my numbers off the file, the second bit of code (the inner for-loop) reads the numbers. the outer for loop then moves to the next line.

THAT part works fine. it goes on down, and my char array goes all the way to Wyoming, but my string array stays at Alabama, filled with chars instead of strings.

and my int array which should be 3 columns by 50 rows fills up it's columns and never moves on.

and i KNOW that this doesnt have anything to do with excel, i was stating that i get rows and columns mixed up.

This post has been edited by VyrossNakizo: 24 April 2010 - 03:43 PM

Was This Post Helpful? 0
  • +
  • -

#13 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Reading data file into arrays.

Posted 24 April 2010 - 03:41 PM

Quote

i just want to know how to make an array of chars into a single string, not a series of chars (which seems like what it's doing).
I just answered that.

Quote

the entire reason i put "string holder" in there was so it would get the value, and then i could put holder (which is supposed to be a string at this point)
And it seems like you answered that yourself.

I'm acting like this on purpose. You're vaguely describing the problem, and contradicting yourself too in your posts. Post the entire code.

EDIT:

Quote

there is SUPPOSED to be such a thing as a 2d array
There is not. You can, however, create arrays of arrays.

There is no concept in C++ of rows and columns. You make that up.

You describe garbage, things going up, things going down, etc. I can't make any meaningful sense of that. We work on code examples, observed output, and expected output. A self contained example that demonstrates an issue.

This post has been edited by Oler1s: 24 April 2010 - 03:45 PM

Was This Post Helpful? 0
  • +
  • -

#14 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Reading data file into arrays.

Posted 24 April 2010 - 03:51 PM

How is states[] declared?

On second thought, please post all your current code.

This post has been edited by n8wxs: 24 April 2010 - 03:52 PM

Was This Post Helpful? 0
  • +
  • -

#15 VyrossNakizo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 24-April 10

Re: Reading data file into arrays.

Posted 24 April 2010 - 04:00 PM

void LoadArray(string states[50], int stats[50][3]){
	char sCarray[25];
	stringstream ss;

	//Open file
	ifstream input;
	input.open(FILE_IN);
	
	//check is-open
	if(!input)
	{
		cout << "\nCan't find input file " << FILE_IN;
		cout << "\nExiting...\n";
		exit(1);
	}
	
	//read data into array...
	while(input.good()){
		for(int j=0; j<49; j++){//"OUTER" for-loop, this cycles each row. j=row number.
			input.get(sCarray, 15);//this is to get the first 15 letters and put them in a char array. The file is formatted so the first 15 chars are supposed to be used.
			string holder(sCarray, 15);//this is where things stop working. instead of looking like "Abc" it looks like 'a''b''c' - MULTIPLE CHARS instead of ONE STRING.
			states[j] = holder;//and then these multiple chars get put into my array. wtf?
			
			for(int i=0; i<3; i++){//"INNER" for-loop, THIS cycles each COLUMN of the int array.
				input >> stats[j][i];//gets filled with 3 ints in each row (but it never cycles the rows, for reasons i can't fathom.
			}
			input.ignore();//this is to take off the end-of-line character for each line
		}
		input.close();//this doesnt seem to have a whole lot of effect, but IDK.
	}

	cout << states << stats; //this is just so i'd see what was coming out. All im getting it a bit of hex. not 5 pages, just a few letters.

}


ok, first: its a void, i know that. i'm going to use pointers to return the value when the function is done, but until then im not worried about actually getting data back into main()- i just want the correct data. I have included comments to help explain each bit.

arrays of arrays, 2d arrays, 3d arrays, 4d arrays. it's the same thing, stop dogging me on the way i say things and please just answer my question.

http://www.cplusplus...utorial/arrays/ <-- here, they refer to it as 2d arrays.

EDIT: states[] is declared in main and passed as a parameter to my loadarray function.

here's how i did it:

string states[50];
int stats[50] [3];
LoadArray(states, stats);

^inside main^

if you guys really want to see my ENTIRE code (some of it is broken outside of this part, but i'm about 99.999999% sure the problems arent related), here:

#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>



using namespace std;

#define FILE_IN "Data.txt"

void LoadArray(string states[], int stats[][3]);
void Greet();
void Leave();
void DisplayList();
void Sort();
void Compare();
int GetOption();
void error(int type);

int main(){

	//Variables...
	string states[50];
	int stats[50] [3];
	int repeat = 0;
	int menuChoice = 0;

	Greet();
	//call function to load array with text file... --LoadArray()
	LoadArray(states, stats);
	//call function to greet user --Greet()

	//do...while loop
	do{

		//call function to get user's menu choice --GetOption()
		cout << "\n		MAIN MENU\n\n1: Display Statistics\n2: Sort..."
			<< "\n3: Compare Statistics\n4:Help\n5: Exit";
		cout << "\n\nPlease make a selection: ";
		cin >> menuChoice;
		cin.ignore();
		repeat = 0;
		//SWITCH statement for analyzing user's inputs
		switch (menuChoice){
		case 1:
			//Display List is selected...  --DisplayList()
			DisplayList();
			repeat = 1;
			break;

		case 2:
			//Sort... is selected...  --Sort()
			Sort();
			repeat = 1;
			break;

		case 3:
			//comparison is selected...  --Compare()
			Compare();
			repeat = 1;
			break;

		case 4:
			//exit is selected...  set int repeat = 0
			repeat = 0;
			repeat = 1;
			break;

		default:
		//nothing is selected, tells user to enter valid choice
		error(1);
		repeat = 1;
		//end of switch statement 
		}

		//end of do...while loop checks for exit condition
	}
	while(repeat = 1);
	//call function to say goodbye --Leave()
	Leave();

}

void LoadArray(string states[50], int stats[50][3]){
	char sCarray[25];
	stringstream ss;

	//Open file
	ifstream input;
	input.open(FILE_IN);
	
	//check is-open
	if(!input)
	{
		cout << "\nCan't find input file " << FILE_IN;
		cout << "\nExiting...\n";
		exit(1);
	}
	
	//read data into array...
	while(input.good()){
		for(int j=0; j<49; j++){
			input.get(sCarray, 15);
			string holder(sCarray, 15);
			states[j] = holder;
			
			for(int i=0; i<3; i++){
				input >> stats[j][i];
			}
			input.ignore();
		}
		input.close();
	}

	cout << states << stats;

}

void Greet(){
	//greeting statement
}

void DisplayList(){
	cout << "\n\n1: Sort...\n2: Compare Statistics\nPress any key to return to Main Menu\n   :";
	int option;
	cin >> option;
	cin.ignore();
	switch(option){
		case 1:
			Sort();
			break;
		case 2:
			Compare();
			break;
		default:
			break;
	}
}

void Sort(){
}

void Compare(){
}

void Leave(){
}
int GetOption(){
	int input;
	
	return input;
}

void error(int type){
	switch(type){
		case 1:
			cout << "\nYou have not entered a valid menu choice. Please enter 1-4";
			break;
		case 2:
			cout << "";
			break;
		default:
			cout << "";
	}
}


This post has been edited by VyrossNakizo: 24 April 2010 - 04:07 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3