468,491 Members | 1,992 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

Reading commands into an array from a text file

5
Hello,
I need to read in commands from a text file into an array and i just can't do it. I am trying to use the line filein>>patternarray[command];
and it doesn't work! can anyone help me?

Cheers
Aug 26 '06 #1
7 4543
Banfa
9,055 Expert Mod 8TB
You need to give a bigger example of your code and to describe how it doesn't work.
Aug 26 '06 #2
pb1uk
5
Hello,
the below is a segment of code from my program and is part of a switch statement case1:

cout <<"Enter the name of the pattern file you wish to load: " << endl;
cin >> patternfile;
filein.open(patternfile.c_str());
if(filein.good())
{
filein>>firstword;
while (firstword =="PATTERN")
{
filein>>patternarray[command];
}
}
i need the program to read in commands from a text file into an array so the array can be accessed to draw a pattern from.
Thanks for your time
Peter
Aug 26 '06 #3
pb1uk
5
Hello,
the below is a segment of code from my program and is part of a switch statement case1:

cout <<"Enter the name of the pattern file you wish to load: " << endl;
cin >> patternfile;
filein.open(patternfile.c_str());
if(filein.good())
{
filein>>firstword;
while (firstword =="PATTERN")
{
filein>>patternarray[command];
}
}
i need the program to read in commands from a text file into an array so the array can be accessed to draw a pattern from.
when executing i get the following error:
C:\Documents and Settings\Peter Briers\My Documents\NEW COURSE\SW design and Imp\Labs\wk12\soft10071\cwork1\main5.cpp(60) : error C2677: binary '[' : no global operator defined which takes type 'class std::basic_string<char,struct std::char_traits<c
har>,class std::allocator<char> >' (or there is no acceptable conversion)
Error executing cl.exe.
which is to do with line 60 which is:
filein>>patternarray[command];

Thanks for your time
Peter
Aug 26 '06 #4
Banfa
9,055 Expert Mod 8TB
So not at execution time but at compile time then.

I am guessing that command is not an integer type, the error is basically telling you that whatever type command is (and I think it is a string from the error) it can not be used as an array index to patternarray because patternarray type doesn't define the [] operator with string as a type.

What type is patternarry? (vector of strings ???)
Aug 26 '06 #5
pb1uk
5
command is type string as is the array:

string command;
string patternarray[100];
Aug 26 '06 #6
Banfa
9,055 Expert Mod 8TB
OK well in that case

filein>>patternarray[command];

is never going to work

Since patternarray is an array (hmmm the clue seems to have been in the name :D) the expression in the [ ] must evaluate to an integer. Command is clearly a string not an integer, if it is a string containing an integer then you need to convert it to an integer first before using it. String may have a method or look up atoi and strtoul.
Aug 27 '06 #7
D_C
293 100+
Perhaps you should research enum.
Aug 28 '06 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

40 posts views Thread by googler | last post: by
10 posts views Thread by T Cordon | last post: by
3 posts views Thread by lizii | last post: by
21 posts views Thread by Stephen.Schoenberger | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.