Saturday, May 9, 2009

C++ code???

I have to write a program for my class that reads a file that contains words and numbers. Once they are read in I have to use either peek() or putback() to determain weather or not they are words or numbers and then take them and put them in to 2 different files.





With the code that I have attemted to write I cant get the whole word or number to get put in to the file I get something like this





In the file I have hello 543


the output I get for this is


h


e


l


l


o





5


4


3


3


rather then


hello


543





how do I use these to functions to write the code???





Thanks


Nick

C++ code???
Well, your output has a newline or '\n' after each char. That's why the printout looks like that. And you are getting the last char read twice. make sure you read until EOF





You should read the file by char. If you use the peek, you get one char, determine what it is, say a char and then peek at the next, if its a char, get it, if not put it back and get the next char. Loop through till end of file. Close file.








Then open File do the same thing for numbers. Then each will have their own file.
Reply:u must have used line feed dont use line feed
Reply:actually it depends upon your code.. what u r using.. so better to give description of the code.


but it seems to be..that there might be a mistake in declaring variable type.. like int,or float. etc.


also check foe getch(); and clrscr(); library functions
Reply:Question to your question actually, what function are you using to put the characters into the file? I'm sure people could be much more helpful if you are willing to post the code you do have so far.
Reply:peek() returns one integer at a time. That integer represents the ascii character code. If you're using peek to read in a whole line, you'll have to read until you see the newline character '\n'.


No comments:

Post a Comment