Hi everyone

I need to be able to read each character from a text file and store them in an array
here is the text file:
*********************
Ace of Spades
10 of Spades
3 of Clubs
King of Spades
5 of Spades
*********************
Queen of Diamonds
King of Hearts
Queen of Hearts
Queen of Clubs
Queen of Spades
*********************
2 of Clubs
2 of Hearts
Ace of Clubs
2 of Diamonds
Ace of Diamonds
*********************
2 of Hearts
3 of Hearts
4 of Hearts
5 of Hearts
6 of Hearts

for instance i need to be able to store the integers in an array.. and then i need to store the string hearts ,clubs in another string array.and i need to ignore the spaces, the'*' so far i have not been able to do it.. i seriously need help. i've been trying different thongs like getline(), indata.getline, getline(indata, str)
none have been working

here's what i did..i managed to make the program read the file

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

int main()
{
string str;
ifstream inData;
string filename;
cout << "Enter the name of the file containing the hands: " << flush;
cin >> filename;
inData.open(filename.c_str( ));


but now i don't know how to continue. i am really bad at programming.:( plz someone help me... thanks

when i tried this it's actually outputing something....plzz plzzz someone help

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

int main()
{
char a[25];
ifstream inData;
string filename;
cout << "Enter the name of the file containing the hands: " << flush;


cin >> filename;

inData.open(filename.c_str( ));
inData >> a;
std::cin.ignore( std::numeric_limits<streamsize>::max(), '\n' );
cout << a << endl;
std::cin.ignore( std::numeric_limits<streamsize>::max(), '\n' );
}

Read the file line by line using the version of getline() you need depending on the type of string you use. Once you have the string you can evaluate whether it is a data string or a spacer string (all *s). If it's a data string then parse it (break it into pieces) break it into three pieces using a stringstream object and the >> operator. Store the appropriate value in the appropriate array. (What numeric value are you going to assign to ACE?)

thanks.... in order to read the file line by line should i use this:
while(inData.getline(line,maxLength))
std::cin.ignore( std::numeric_limits<streamsize>::max(), '\n' );

{
cout << line <<endl;

}

Okay, now if you can print the file data line by line to the screen look up how to use stringstreams to break up each line into individual words in the line (or substrings of the original string if you prefer), or figure out a way that you can do it yourself reading each char in the line one by one, recognizing that any whitespace, such as a space char or the newline char, idicates the end of a given word in the line. If you aren't allowed to use things you haven't already learned in class then you may be restricted in your options.

oh it's not even printing to the screen...the screen just disappears immediately after i've entered the file name.... what should i do... i have been inserting the std::cin.ignore( std::numeric_limits<streamsize>::max(), '\n' ); everywhere.. but still it just disappears.... what should i put so that it does nt disappear..

try a call or two to cin.get() just before the closing } of main(), (just before where return 0; would have been to exit main() if you are familiar with that syntax).

Thanks for helping..and sorry because i know my questions are really silly. Now i did what you said. and it's not printing anything from the text file. It's just printing the characters which i enter. thus the cin.get function is working but the indata.getline is not. I included the program below.

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

int main()
{
int maxLength=100;
char c;
char line[maxLength];
ifstream inData;
string filename;
cout << "Enter the name of the file containing the hands: " << flush;


cin >> filename;


inData.open(filename.c_str( ));

inData.getline(line,maxLength);
cout << line << endl;


std::cin.ignore( std::numeric_limits<streamsize>::max(), '\n' );
std::cin.ignore( std::numeric_limits<streamsize>::max(), '\n' );
cin.get(c);
cout << c << endl;
std::cin.ignore( std::numeric_limits<streamsize>::max(), '\n' );
std::cin.ignore( std::numeric_limits<streamsize>::max(), '\n' );

return 0;

}

plz someone help me

the screen is still disappearing.
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
using namespace std;

int main()
{
int maxLength=100;
char c;
char line[maxLength];
ifstream inData;
string filename;
cout << "Enter the name of the file containing the hands: " << flush;


cin >> filename;


inData.open(filename.c_str( ));

inData.getline(line,maxLength);
cout << line << endl;


cin.get();
cin.get();

return 0;

}

ah now i know..should have used the system("pause")
and then return 0

Ok, couple things. First, in the above posted code, maxLength should be declared as type const int, not int. At least, so far as I know, the option to declare arrays using variables rather than constant variables has not been implemented. In your earlier versions you were using STL strings, which don't have to be "sized" before use or you used the literal constant int of 25, which would work instead of const int maxLength, though the const int would be prefered to the "magic number" of 25 or 35 or 190 or whatever the "magic number" might be. Furthermore, I don't particularly like mixing STL strings and C style strings in the same program when I don't have to, but that's more personal taste than anything else, as it reminds me if I have to do memory management or not.

Second, when mixing >> and getline() in the same program you want to clear the input buffer by calling ignore() between each call to >> and getline(); or to make it easier, before the call to getline(). The reason is >> doesn't remove the terminating char, in this case, as it often is, the new line char, from the input buffer that was put in the input buffer when you hit the enter key. On the other hand, getline() does remove the terminating char from the input buffer. Thus, when you get filename with >> there is a newline char remaining in the buffer. If you had called >> right away again it would have ignored the new line char remaining in the buffer and gone on it's merry way. However, when getline() goes to get information from the input buffer the first thing it sees is that darn newline char sitting there which tells getline() to stop input into line (since the newline char is the default terminating char for getline()) meaning nothing is entered into line, and it looks like nothing got done, when in fact it's doing exactly what it is supposed to do.

I like the idea you're breaking the code down to simpler and simpler detail trying to figure it out. Good job!

EDIT: I don't recommend calling system("pause"); as bringing system() file in opens you up to a bunch of potential problems you can avoid with minimal effort.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.