Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 cstring question
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
jin




PostPosted: Sat Nov 04, 2006 5:36 pm   Post subject: cstring question

Hey i am tryin to read in a file and store the letters in a 2Ds ctstring. So that words[word number][number of letters]. In java i would have used a string tokenizer to determine when a word finishes and the next starts. Any help is appreciated.

thnx.
Sponsor
Sponsor
Sponsor
sponsor
md




PostPosted: Sat Nov 04, 2006 7:11 pm   Post subject: (No subject)

You could use std::strings instead of character strings. And use std::vector instead of an array, it automatically manages it's size.
wtd




PostPosted: Sat Nov 04, 2006 7:16 pm   Post subject: (No subject)

Unless of course this question is limited to C.
jin




PostPosted: Sat Nov 04, 2006 7:34 pm   Post subject: (No subject)

thnx for the reply but i havent yet learned about std::string and std::vector. i think i might have gotten it.

code:


int y = 0;
       int x = 0;
       char letter;

       while (! inFile.eof())
      {
              inFile.get (letter);

              if (!isspace(letter))
              {
               words [x] [y] = letter;
               y++;
               }

               else
               {
               y=0;
               x++;
               }
     }



This is what i have so far and it works if there is one space between characters but if there is more. it saves the space in the variable. Any thoughts.

thnx
jin




PostPosted: Sat Nov 04, 2006 9:45 pm   Post subject: (No subject)

File contents are:
abc <space> def <space><space>ghi <enter>

the output i get is:
abc
def
<empty line>
ghi

the output i need is:
abc
def
ghi

thnx.
wtd




PostPosted: Sun Nov 05, 2006 9:51 am   Post subject: (No subject)

First off, let's try formatting this code nicely.

code:
int y = 0;
int x = 0;
char letter;

while (!inFile.eof())
{
   inFile.get(letter);

   if (!isspace(letter))
   {
      words[x][y] = letter;
      y++;
   }
   else
   {
      y = 0;
      x++;
   }
}


Now, let's step through what happens on input of:

code:
abc def  ghi


code:
x = 0
y = 0
letter = 'a'
'a' is not a space
words[0][0] = 'a'
y = 1

x = 0
y = 1
letter = 'b'
'a' is not a space
words[0][1] = 'b'
y = 2

x = 0
y = 2
letter = 'c'
'c' is not a space
words[0][2] = 'c'
y = 3

x = 0
y = 3
letter = ' '
' ' is a space
y = 0
x = 1

x = 1
y = 0
letter = 'd'
'd' is not a space
words[1][0] = 'd'
y = 1

x = 1
y = 1
letter = 'e'
'e' is not a space
words[1][1] = 'e'
y = 2

x = 1
y = 2
letter = 'f'
'f' is not a space
words[1][2] = 'f'
y = 3

x = 1
y = 3
letter = ' '
' ' is a space
y = 0
x = 2

x = 2
y = 0
letter = ' '
' ' is a space
y = 0
x = 3

x = 3
y = 0
letter = 'g'
'g' is not a space
words[3][0] = 'g'
y = 1

x = 3
y = 1
letter = 'h'
'h' is not a space
words[3][1] = 'h'
y = 2

x = 3
y = 2
letter = 'i'
'i' is not a space
words[3][2] = 'i'
y = 3


Now, do you see where the error is occurring?
OneOffDriveByPoster




PostPosted: Sun Nov 05, 2006 10:06 am   Post subject: (No subject)

@jin:

if using C++, look into iostream too;

if using C, look into scanf (careful what you do with it though).

In your current program:
To get rid of the extra spaces, simply read away all spaces before a word.
(A loop will do). ungetc() can help.
jin




PostPosted: Sun Nov 05, 2006 10:45 am   Post subject: (No subject)

thnx for all your help i got it working now.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: