Stack Program Error

This is a discussion on Stack Program Error within the C++ Programming forums, part of the General Programming Boards category; This is the stack project I had to make. I'm posting my code after what the project is supposed to ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    109

    Stack Program Error

    This is the stack project I had to make. I'm posting my code after what the project is supposed to be. and then I'll post the error i get from compiling that i can't figure out.

    What Project Is.
    Project:

    For this project you will write a program that will read in strings from a file named "reverse.in". Each word may be marked with an even number of "at" characters ('@'). The first '@' character signals that the following characters up to the next '@' are in reverse order. Your program will print the result to an output file called "reverse.out". You must use an STL stack to reverse the order of the characters.

    For example, if the input file looks like this:

    @egelloC@
    CS174
    re@srev@e
    sup@ilacre@fragilist@ilaipxeci@docious
    id@@en@@ti@@ty

    then the output would be:

    College
    CS174
    reverse
    supercalifragilisticexpialidocious
    identity
    code i wrote
    Code:
    #include <stack>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    int main()
    {
    int i;
    bool forward;
    string word;
    stack<char> letter;
    string   inFileName = "reverse.in";
    string   outFileName = "reverse.out";
    ifstream inFile;
    ofstream outFile;
      
    inFile.open(inFileName.c_str());
    outFile.open(outFileName.c_str());
    forward = true;
    
    inFile.getline(word);
    while(inFile)
    {
    	for(i=0;i<=word.length();i++)
    	{
    		if(forward)
    		{
    			outFile << word[i];
    			if(word[i] == '@')
    			{
    				forward = false;
    			}
    		}
    		else(!forward)
    		{
    			if(word[i]!='@')
    			{
    				letter.push(word[i])				
    			}
    			else if(word[i] == '@')
    			{
    				forward = true;
    				while(!letter.empty)
    				{
    					outFile << letter.top();
    					letter.pop;
    				}
    			}
    		}
    	}
      inFile.close();          
      outFile.close();
      return 0;
    };
    the error i get from compiling
    Code:
    main.cpp: In function ‘int main()’:
    main.cpp:21: error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::getline(std::string&)’
    /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/istream:581: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]
    /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/istream:397: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]
    main.cpp:35: error: expected `;' before ‘{’ token
    main.cpp:54: error: expected `}' at end of input
    I looked at like 35 and line 54 but i have what the compiler is talking about, and I have no idea what the first error I have is. Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Maybe you need to #include <string>

    Oh, and I think your for loop runs one iteration more than it should.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    else(!forward)

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> inFile.getline(word);
    There is a different version of getline for strings. Use:
    Code:
    getline(inFile, word);

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    my only error now is
    Code:
    main.cpp: In function ‘int main()’:
    main.cpp:36: error: expected `;' before ‘{’ token

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Post your updated code.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    Code:
    #include <stack>
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    int main()
    {
    int i;
    bool forward;
    string word;
    stack<char> letter;
    string   inFileName = "reverse.in";
    string   outFileName = "reverse.out";
    ifstream inFile;
    ofstream outFile;
      
    inFile.open(inFileName.c_str());
    outFile.open(outFileName.c_str());
    forward = true;
    
    getline(inFile,word);
    while(inFile)
    {
    	for(i=0;i<=word.length();i++)
    	{
    		if(forward)
    		{
    			outFile << word[i];
    			if(word[i] == '@')
    			{
    				forward = false;
    			}
    		}
    		else(!forward)
    		{
    			if(word[i]!='@')
    			{
    				letter.push(word[i]);				
    			}
    			else if(word[i] == '@')
    			{
    				forward = true;
    				while(!letter.empty)
    				{
    					outFile << letter.top();
    					letter.pop;
    				}
    			}
    		}
    	}
    	getline(inFile,word);
    }
      inFile.close();          
      outFile.close();
      return 0;
    };

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    See anon's post. That's not legal syntax. A simple else will do.

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    This is the code I have:
    Code:
    #include <stack>
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    int main()
    {
    int i;
    bool forward;
    string word;
    stack<char> letter;
    string   inFileName = "reverse.in";
    string   outFileName = "reverse.out";
    ifstream inFile;
    ofstream outFile;
      
    inFile.open(inFileName.c_str());
    outFile.open(outFileName.c_str());
    forward = true;
    
    
    getline(inFile,word);
    while(inFile)
    {
    	for(i=0;i<=word.length();i++)
    	{
    		if(forward)
    		{
    			outFile << word[i];
    			if(word[i] == '@')
    			{
    				forward = false;
    			}
    		}
    		else
    		{
    			if(word[i]!='@')
    			{
    				letter.push(word[i]);				
    			}
    			else if(word[i] == '@')
    			{
    				forward = true;
    				while(!letter.empty())
    				{
    					outFile << letter.top();
    					letter.pop();
    				}
    			}
    		}
    	}
    	getline(inFile,word);
    }
      inFile.close();          
      outFile.close();
      return 0;
    };
    in the reverse.out file i have @tsal@ and when i ran the program it printed @tsal^@...what am i doing wrong here? Its not supposed print the @ signs.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Its not supposed print the @ signs.
    Then don't print the @ signs. You already have if statements in there to determine if the character is an @ or not.

    Also, you didn't fix the second part of laserlight's first post. Your loop still runs one extra time.

  11. #11
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    ok i think i fixed the loop issue laserlight pointed out...how would i decide not to print the @ sign?...also at the end of each line ^@ is printed idk what that is from. heres my updated code:
    Code:
    #include <stack>
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    int main()
    {
    int i;
    bool forward;
    string word;
    stack<char> letter;
    string  inFileName = "reverse.in";
    string  outFileName = "reverse.out";
    ifstream inFile;
    ofstream outFile;
      
    inFile.open(inFileName.c_str());
    outFile.open(outFileName.c_str());
    forward = true;
    
    
    getline(inFile,word);
    while(inFile)
    {
    	for(i=1;i<=word.length();i++)
    	{
    		if(forward)
    		{
    			outFile << word[i];
    			if(word[i] == '@')
    			{
    				forward == false;
    			}
    		}
    		else if(!forward)
    		{
    			if(word[i]!='@')
    			{
    				letter.push(word[i]);				
    			}
    			else if(word[i] == '@')
    			{
    				forward = true;
    				while(!letter.empty())
    				{
    					outFile << letter.top();
    					letter.pop();
    				}
    			}
    		}
    	}
    	outFile << endl;
    	getline(inFile,word);
    }
      inFile.close();          
      outFile.close();
      return 0;
    };

  12. #12
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Nope, you haven't fixed what laserlight pointed out. The answer is in "for(i....)". And i should start from 0.

    Also, you need to nest "outfile << word[i]" inside another conditional so that @ does not get printed.

  13. #13
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    I got it thank you all for helping me out i greatly appreciate it.
    Last edited by DarkDot; 03-30-2007 at 05:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  4. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  5. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 05:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21