Thread: Brainstorming for Stacks Project

  1. #16
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    my problem here is how do i read from the infile that one line and write it then when its done go onto the next one. I am unfamiliar with fstream any help would be greatful. I know how to open and close the files, thats about all. well also that to read from an infile i use >> rather than << i think.

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Can you read from cin? It's very similar. getline reads a line, operator>> reads one word.

  3. #18
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    no i have to read from the infile

  4. #19
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    this is the code i've come up with so far. I figure i can make word equal to the line of the string its reading and loop through going word[i] printing all the chars to the outfile, then put an if statement saying if word[i] = '@' then reverse. Then when it gets to the end of the line go to the next and start all over.
    Code:
    #include <stack>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    stack<char> rev;
    	char tmp;
    	int counter;
    	void Reverse()
    	{
    		while(word[i] != '@')
    		{	
    			rev.push(word[i]);
    			++counter;
    		}
    		for(i=1;i<=counter;i++)
    		{
    			tmp = rev.top();
    			outFile << tmp;
    			rev.pop();
    		}
    	}
    
    int main()
    {
    	string inFileName = "reverse.in";
    	string outFileName = "reverse.out";
    	ifstream inFile;
    	ofstream outFile;
    Last edited by DarkDot; 03-29-2007 at 10:12 PM.

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> no i have to read from the infile
    I said it is similar. I didn't mean that you should use cin. If you know how to use cin, then you can do (almost) the exact same thing and use infile instead.

    You've still got a ways to go with your code, but the idea seems good. I would do less code before compiling and testing. For example, start by just reading in each word and outputting it without paying any attention to the @ symbol and without using the stack. This will give you practice with using an ifstream. Once you get the code working so that it copies the words exactly, then you can add more code to try to handle the reversing part.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM