r3r
asked on
Visual c++: populating a string array with values from a data file..
Ok--
This seems easy, but apparently I've overlooked something.
What I'm trying to do is this:
1. I have to read the contents of a *.dat file (in this case the file contains 12 names, each on its own line right after one another)
2. Use a bubble sort routine to put the names in ascending order
3. display the sorted files to the screen
simple, right?
The bubble sort is not a problem if I could get these strings into an array. Then I would use string class method swap to do the rest.
I'm runnning into problems when I try populating the string array. I'm not getting compiler errors, but when I test outputing the array I get nothing. I've written stuff before using character arrays to do the same thing, but I can't do this here. I've looked in this forum and elseware but nothing I've found difinitively explains how a string array works. My text book has nothing on this, either, nor was this ever discussed in my class. I've tried double scripted arrays, but that didn't work either. Please help...This will be on my Monday's exam. This is NOT a homework assignment.
Thanks in advance,
r3r
int element=0;
ifstream inFile("strings.dat", ios::in); //declare ifstream object
string stringInput; //string variable placeholder
string sArray[30]; //string array to hold 30 strings
inFile.clear(); //clear EOF
inFile.seekg(0); //start reading at beginning of file
while (inFile >> stringInput)
{
sArray[element] = stringInput;
++element;
cout << "stringInput = " << stringInput << endl; //testing output... Nothing displays here????
cout << "sArray = "<< sArray[element] << endl; //testing output... Nothing displays here, either
}
cout << sArray[3] << endl //more testing
<< sArray[6] <<endl; //more testing
This seems easy, but apparently I've overlooked something.
What I'm trying to do is this:
1. I have to read the contents of a *.dat file (in this case the file contains 12 names, each on its own line right after one another)
2. Use a bubble sort routine to put the names in ascending order
3. display the sorted files to the screen
simple, right?
The bubble sort is not a problem if I could get these strings into an array. Then I would use string class method swap to do the rest.
I'm runnning into problems when I try populating the string array. I'm not getting compiler errors, but when I test outputing the array I get nothing. I've written stuff before using character arrays to do the same thing, but I can't do this here. I've looked in this forum and elseware but nothing I've found difinitively explains how a string array works. My text book has nothing on this, either, nor was this ever discussed in my class. I've tried double scripted arrays, but that didn't work either. Please help...This will be on my Monday's exam. This is NOT a homework assignment.
Thanks in advance,
r3r
int element=0;
ifstream inFile("strings.dat", ios::in); //declare ifstream object
string stringInput; //string variable placeholder
string sArray[30]; //string array to hold 30 strings
inFile.clear(); //clear EOF
inFile.seekg(0); //start reading at beginning of file
while (inFile >> stringInput)
{
sArray[element] = stringInput;
++element;
cout << "stringInput = " << stringInput << endl; //testing output... Nothing displays here????
cout << "sArray = "<< sArray[element] << endl; //testing output... Nothing displays here, either
}
cout << sArray[3] << endl //more testing
<< sArray[6] <<endl; //more testing
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>++element;//Put this after out put.
Good point.
Good point.
>>As usual, jkr get it first
Don't blame that on me, the TV program in the morning here is just awful :o)
Don't blame that on me, the TV program in the morning here is just awful :o)
ASKER
Hmmm... OK, now I really feel stupid.
jkr: when I try your example I get the same result: nothing prints out
Axter: Your code gives me a compiler error (sorry!)
There must be something else I'm not doing or haven't included in the header. Both your codes make sense to me. I don't think either of you are wrong. Here's the entire code, including the header
// general code to test how string class works
#include <iostream>
using namespace std;
#include <iomanip>
using std::setw;
#include <fstream>
using std::ofstream;
using std::ifstream;
using std::fstream;
#include <string>
using namespace std;
#include <sstream>
using std::istringstream;
using std::ostringstream;
#include <new>
int main(){
int element=0;
ifstream inFile("strings.dat", ios::in); //declare ifstream object
string stringInput; //string variable placeholder
string sArray[30]; //string array to hold 30 strings
inFile.clear(); //clear EOF
inFile.seekg(0); //start reading at beginning of file
// jkr's code~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~
while (std::getline(inFile, stringInput)){
sArray[element] = stringInput;
cout << "stringInput = " << stringInput << endl; //testing output... Nothing displays here????
cout << "sArray = "<< sArray[element] << endl; //testing output... Nothing displays here, either
++element;//Put this after out put.
}
// jkr's code~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~
// Axter's code~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~
while (!inFile.eof())
{
getline ( inFile, stringInput);
sArray[element] = stringInput;
cout << "stringInput = " << stringInput << endl; //testing output... Nothing displays here????
cout << "sArray = "<< sArray[element] << endl; //testing output... Nothing displays here, either
++element;
}
// Axter's code~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~
cout << sArray[3] << endl //more testing
<< sArray[6] <<endl //more testing
<< stringInput;
system ("pause");
return 0;
}
jkr: when I try your example I get the same result: nothing prints out
Axter: Your code gives me a compiler error (sorry!)
There must be something else I'm not doing or haven't included in the header. Both your codes make sense to me. I don't think either of you are wrong. Here's the entire code, including the header
// general code to test how string class works
#include <iostream>
using namespace std;
#include <iomanip>
using std::setw;
#include <fstream>
using std::ofstream;
using std::ifstream;
using std::fstream;
#include <string>
using namespace std;
#include <sstream>
using std::istringstream;
using std::ostringstream;
#include <new>
int main(){
int element=0;
ifstream inFile("strings.dat", ios::in); //declare ifstream object
string stringInput; //string variable placeholder
string sArray[30]; //string array to hold 30 strings
inFile.clear(); //clear EOF
inFile.seekg(0); //start reading at beginning of file
// jkr's code~~~~~~~~~~~~~~~~~~~~~~
while (std::getline(inFile, stringInput)){
sArray[element] = stringInput;
cout << "stringInput = " << stringInput << endl; //testing output... Nothing displays here????
cout << "sArray = "<< sArray[element] << endl; //testing output... Nothing displays here, either
++element;//Put this after out put.
}
// jkr's code~~~~~~~~~~~~~~~~~~~~~~
// Axter's code~~~~~~~~~~~~~~~~~~~~~~
while (!inFile.eof())
{
getline ( inFile, stringInput);
sArray[element] = stringInput;
cout << "stringInput = " << stringInput << endl; //testing output... Nothing displays here????
cout << "sArray = "<< sArray[element] << endl; //testing output... Nothing displays here, either
++element;
}
// Axter's code~~~~~~~~~~~~~~~~~~~~~~
cout << sArray[3] << endl //more testing
<< sArray[6] <<endl //more testing
<< stringInput;
system ("pause");
return 0;
}
ASKER
Ok, I goofed. The friggin name of my text file is string.dat, not strings.dat
Gee, helps when you try to open the right file!
code works fine. Thanks guys.
Gee, helps when you try to open the right file!
code works fine. Thanks guys.
Exactly what compiler error are you getting?
What line is giving you the compile error?
What line is giving you the compile error?
ASKER
Axter,
because I was trying to open the wrong file I was getting,
Unhandled exception at 0x0041f456 in LAB_12 String Class.exe: 0xC0000005: Access violation writing location 0xcccccccc.
Once I changed to the file name to the CORRECT name (Doh!) it went away. Sorry for the mix up, and thanks
r3r
because I was trying to open the wrong file I was getting,
Unhandled exception at 0x0041f456 in LAB_12 String Class.exe: 0xC0000005: Access violation writing location 0xcccccccc.
Once I changed to the file name to the CORRECT name (Doh!) it went away. Sorry for the mix up, and thanks
r3r
*Cough* this would have been an excellent candidate for a split - Axter mentioning "++element;//Put this after out put." for sure contributed to this solution.
ASKER
jkr,
OK, I'm new to this forum and I should have read and understood all the policies and procedures before I started posting, my mistake. Can I email the moderator and as him to split the points since they have already been assigned?
sorry, axter!
r3r
OK, I'm new to this forum and I should have read and understood all the policies and procedures before I started posting, my mistake. Can I email the moderator and as him to split the points since they have already been assigned?
sorry, axter!
r3r
You can post a request at the Community Support topic area.
http://oldlook.experts-exchange.com:8080/Community_Support/
http://oldlook.experts-exchange.com:8080/Community_Support/
ASKER
Axter,
I asked the points to be equally split between you and rjr. I appreciate the help.
r3r
I asked the points to be equally split between you and rjr. I appreciate the help.
r3r
>>I asked the points to be equally split between you and rjr. I appreciate the help.
Glad to help.
Glad to help.
However, don't forget to move the ++element