Thread: xy was not declared in this scope: compiler error!

  1. #16
    registered citizen twilight's Avatar
    Join Date
    Aug 2009
    Location
    Germany
    Posts
    23
    :-) thx again!

  2. #17
    registered citizen twilight's Avatar
    Join Date
    Aug 2009
    Location
    Germany
    Posts
    23
    so, I just had some time and thought "hey, let's begin with the second method (i.e. read_file()

    I made some mistakes, now it works that it does recognize when there is no file that could be opend AND it opens a file, and prints out the first word. Why only the first word? Shouldn't it print out the whole content of the file?

    Code:
    void read_file(){
    
    	std::string readfile; 	// this is the object where the filename goes into
    	std::string readtext;	// this is the object where the read in text goes into
    	std::cout << "Please enter the filename of the file you want to open: \n";
    	std::getline(std::cin,readfile);	// get the user to input the filename
    	std::ifstream userfile (readfile.c_str());
    
    	if(!userfile.is_open()){     // check if the file could be opend
    		std::cout<<"The file you tried to open could not be opened, sorry!";
    	}else{
    		std::cout<<"You loaded the file " << readfile << "! Content below\n";
    		userfile>>readtext;
    		std::cout<<readtext;
    	}
    }

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The >> operator only reads a word by default. Use std::getline.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    registered citizen twilight's Avatar
    Join Date
    Aug 2009
    Location
    Germany
    Posts
    23
    thanks, that did the trick :-)

    the tutorial used the >> operator with a char text[] and it worked with more than one word, so I thought I could use it the same way with the ifstream-struct, but that was wrong as it seams.

    Now it works!

    changed the line from userfile>>readtext; to getline(userfile,readtext);

    Edit says: wait a minute. I just switched to eclipse for writing my files, and it gave me an error once (not now) saying ifstream was a struct. In the c++.reference i just read it says ifstream is a class?! Why would eclipse call it a struct?
    Last edited by twilight; 08-08-2009 at 02:39 AM.

  5. #20
    registered citizen twilight's Avatar
    Join Date
    Aug 2009
    Location
    Germany
    Posts
    23
    So, after I got a little confused with how the tutorial on this page explains things, I found myself another tutorial, that suits me "needs" way better i.e. describes things how I need to hear them to understand. So now I am following 2 tutorials and using them complementary.

    Anyhow, I tried to get on with my little program, so I had at least one thing finished ;-) Here is my new void appendto_file() - function. When I compile it, I get the following errors, what do I wrong? Thanks again for any help!! :-)

    Code:
    ../util.cpp: In function 'void appendto_file()':
    ../util.cpp:76: error: no match for 'operator==' in 'isfile == 'y''
    ../util.cpp:76: error: no match for 'operator==' in 'isfile == 'Y''
    ../util.cpp:81: error: 'ios' has not been declared
    ../util.cpp:81: error: 'app' was not declared in this scope
    Code:
    void appendto_file()
    {
    	std::cout << "Please enter the filename of the file you want to append text to: ";
    	std::string filename;
    	std::getline(std::cin,filename);
    	std::cout << "\n";
    
    	std::ifstream appendfile (filename.c_str());
    	if(!appendfile.is_open())
    	{
    		std::cout<<"The file you tried to open could not be opened, sorry!\n";
    	}
    	else
    	{
    		std::cout<<"Is this the file, you wanted to open?\n\n";
    		std::string readtext;
    		getline(appendfile,readtext);
    		std::cout<<readtext;
    		std::cout<<"\n\n";
    		std::cout<<"Enter y for yes and n for no: ";
    		appendfile.close();
    		std::string isfile;  		// now we want to check if that was the file or not.
    		std::getline(std::cin,isfile);
    		if(isfile == 'y' || isfile == 'Y')
    		{
    			std::cout<<"Please enter the text you want to append: \n";
    			std::string appendtext;
    			std::getline(std::cin,appendtext);
    			std::fstream appendfile (filename, ios::app);
    			appendfile<<appendtext;
    			appendfile.close();
    		}
    		else
    		{
    			std::cout<<"Sorry, please try again!\n";
    		}
    	}
    }
    Last edited by twilight; 08-12-2009 at 04:22 PM.

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You cannot check whether a string equals a character, because it can't. Perhaps you want to check whether the string equals the string "Y".

    Also, it's std::ios::app.

  7. #22
    registered citizen twilight's Avatar
    Join Date
    Aug 2009
    Location
    Germany
    Posts
    23
    thx, I changed ios::app into std::ios::app.

    Ok, so check a string against a character is not possible. Makes sense I guess. :-D But what when I use the string.c_str()-function. So to say:

    Code:
    if(isfile.c_str() == 'y' || isfile.c_str() == 'Y')
    When I do that, the compiler complains about:

    Code:
    ../util.cpp:76: error: ISO C++ forbids comparison between pointer and integer
    But I seem to fail to see where I use a pointer.

  8. #23
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    if(isfile.c_str()[0] == 'y' || isfile.c_str()[0] == 'Y')
    Of course in this case, you can just drop the c_str()
    Code:
    if(isfile[0] == 'y' || isfile[0]== 'Y')
    bit∙hub [bit-huhb] n. A source and destination for information.

  9. #24
    registered citizen twilight's Avatar
    Join Date
    Aug 2009
    Location
    Germany
    Posts
    23
    thx, the error is now gone.

    just for my understanding: isfile[0] looks like I am accessing the first char of an array of chars, but isfile is a string, or more precisely: an object. What exactly does the [0] do with an object?

    Also, the following line now gets an error it did not get before :-(

    Code:
    std::fstream appendfile (filename, std::ios::app);
    error:
    Code:
    ../util.cpp: In function 'void appendto_file()':
    
    ../util.cpp:86: error: no matching function for call to 'std::basic_fstream<char, std::char_traits<char> 
    >::basic_fstream(std::string&, const std::_Ios_Openmode&)'
    
    /usr/include/c++/4.0.0/fstream:704: note: candidates are: std::basic_fstream<_CharT, _Traits>
    ::basic_fstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
    
    /usr/include/c++/4.0.0/fstream:690: note: std::basic_fstream<_CharT, _Traits>::basic_fstream()
    [with _CharT = char, _Traits = std::char_traits<char>]
    
    /usr/include/c++/4.0.0/iosfwd:95: note: std::basic_fstream<char, std::char_traits<char> >
    ::basic_fstream(const std::basic_fstream<char, std::char_traits<char> >&)
    make: *** [util.o] Error 1

  10. #25
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The [] operator can be overloaded by any object. std::string overloads it to return the character at the passed index.

    The fstream constructor takes a C style string, not a std::string. Add a .c_str() to the end of filename.
    bit∙hub [bit-huhb] n. A source and destination for information.

  11. #26
    registered citizen twilight's Avatar
    Join Date
    Aug 2009
    Location
    Germany
    Posts
    23
    oh, man. I had that once before :-( thx for the hint again!!

    now it works. At least, it compiles. my appendto_file() function now asks for the filename, prints an error if it can't be opened, prints the content of the file if it can be opened.

    the question if this is the right file works, too. y and Y gives the expected result, n or anything else too.

    But what happens when the user presses y and then inputs his text is another question. The user can input text, but that text does not get appended :-( So something in the following code-snippet must be wrong:

    Code:
    std::cout<<"Please enter the text you want to append: \n";
    std::string appendtext;
    std::getline(std::cin,appendtext);
    std::fstream appendfiletwo;
    appendfiletwo.open (filename.c_str(), std::ios::app);
    appendfiletwo<<appendtext.c_str();
    appendfiletwo.close();
    Last edited by twilight; 08-12-2009 at 05:03 PM.

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by twilight View Post
    When I do that, the compiler complains about:

    Code:
    ../util.cpp:76: error: ISO C++ forbids comparison between pointer and integer
    But I seem to fail to see where I use a pointer.
    c_str() returns a legacy C-style string and in C, a string is considered to be a pointer to char (char*). c_str() returns const char*, thus you try to compare a const char* to char, which is illegal (and it's not sensible either).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    registered citizen twilight's Avatar
    Join Date
    Aug 2009
    Location
    Germany
    Posts
    23
    oh, tanks, that clears that issue :-)

    what about my code-snippet from the most recent post? Any hint you have for me there?

  14. #29
    registered citizen twilight's Avatar
    Join Date
    Aug 2009
    Location
    Germany
    Posts
    23
    I just thought a further check would be nice to see if the file-opening for the fstream worked out, so I changed the snippet from:

    Code:
    std::cout<<"Please enter the text you want to append: \n";
    std::string appendtext;
    std::getline(std::cin,appendtext);
    std::fstream appendfiletwo;
    appendfiletwo.open (filename.c_str(), std::ios::app);
    appendfiletwo<<appendtext.c_str();
    appendfiletwo.close();
    into:

    Code:
    std::cout<<"Please enter the text you want to append: \n";
    std::string appendtext;
    std::getline(std::cin,appendtext);
    std::fstream appendfiletwo;
    appendfiletwo.open (filename.c_str(), std::ios::app);
    
    if(!appendfiletwo.is_open())
    {
    	std::cout<<"Your file could not be opend for appending, sorry.\n";
    }
    else
    {
    	appendfiletwo<<appendtext.c_str();
    	appendfiletwo.close();
    	std::cout<<"Your data has been appended!\n";
    }
    And guess what I am getting when I try to append something?

    Your file could not be opend for appending. So something is not working with my fstream. But what? I can't figure it out right now :-(

    Edit: a string does not lose it's content once I used it by string.c_str() or does it? If it does it would be clear why fstream could not open, because my string is empty. But if it does not, then I have a problem I really can't see :-(

  15. #30
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Is the name what you think it is?

    (Since this is called filetwo....) Is the file already open?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM