C++ code help[See edit 2]

I am learning how to program and one of the exercises is to make a program that allows me to input strings line by line. So essentially an extremely basic text editor. The problem that I have is that when I run the code, I put in one line, and the program ends. I can't figure out why. I get no errors on compile. (I apologize if the code is hard to read. Please keep in mind that I am still on a some-what basic level, I have not studied the concepts of classes or heavy OOP yet.) Here is my code:

Code ends there. Thanks in advance.

Pastebin link: http://pastebin.com/keP25udC -Devon

Edit 1- removed the source in the post. Devon graciously added the pastebin link. Thanks.

Edit 2- Here is more code that I am having trouble with. I am sure it looks like crap to any experienced people but I cant see what is going on with it. The code throws so many errors its not even funny. Most of it is a bunch of C++ jargon it seems. Almost nothing about the code it self. Just parts of the C++ language that doesnt agree with my code I guess. I will post the errors if needed. I cant make any sense from most of it anyway. Pastebin link: http://pastebin.com/znUKpwcv 

I understand that the if(! filename) statement is not in the right spot. It should be higher up in the code. Just noticed that after posting. Sorry.

It has been a while since I have coded but I think you are using strcmp wrong. With that function a zero value is returned if the strings are equal  which will be interpreted as false by the if statement. You also dont seem to be updating output in the while loop.which will make an infinite loop unless the inital value is the termanating string.

Sombody feel free to correct me if I am wrong.

I will set up a test program to test the strcmp command as soon as I can get back on the computer. The while loop loops until the if statement is triggered and it breaks. The point that you make about strcmp makes sense so that with be my first experiment. Thanks!

I will also test using the strlen function 

Why the hell are you using C functions in C++? That's stupid.

Its what the book teaches. Is "char" C? Im changing everything over to "string" once i get this version working anyway. 

You can use char's but they are hard to handle, that's why C++ has the std::string class. If there is a high level standard: use it. If you don't you could write C code instead everywhere. Mixing stuff like string based streams with C string functions is just a mess.

I looked at std::string and it looks much cleaner. Awesome. 

Pushing to feed

You are using strings but havent put #include<string>

if(input_line == 'Q' || 'q') This will always be true if I remeber correctly as it will be viewed more like if((input_line == 'Q') || 'q') when you want it to be taken as if((input_line == 'Q') || (input_line == 'q'))

atoi is a function that works with c strings and you are using trying to use a string object with it. I am not really sure why you would want that function anyhow but as it is you arent calling the function in your while loop correctly. it should be more like some_integer = check_input(c, input_line); where as yours just says int but isnt actually giving an int type to return a value to.

Well hopefully that is a start. I unfortunatly dont have a lot of time at the moment :/

Good luck man.

My only goal is to get a working program that gets a filename or file path from the user, and print out the contents line by line. When the program prints out the 24th line, the program will prompt the user for lines (as in the user will enter x number, it will be as a string, the program translates that string into a number, and the program will display x many lines) and if the user enters "Q", well its pretty obvious what happens after that. So the program will loop until the end of the file is reached. I have a working version of this using std::char, but not with std::string. So obviously my whole goal for doing this is to get a working std::string version. I guess I just need to go to the C++ website and look at usable functions there. I tried making sense of them earlier but I couldnt.

I'm trying to keep using c as my "line number" variable so I only have to use one 'for' loop. So basically instead of using atoi(which won't take string type variables. Speaking of, I saw somewhere about 'stoi'. Is that a real function?), I should use 'c = input_line[0];'? Or 'c = input_line(c, input_line);'? If I used the second one, how does that work? I don't see how the data in the parentheses affects the function. 

Yeah I see that you were trying to use atoi to convert a string into a int it just seemd easier to me to just take a int directly. But you can do that with string class objects but you will be doing it with sstream. Of course if you really want to use atoi you can convert the string object to a c string when you make the call to atoi by using the c_str() function of the string class but you might as well do it with stringstream.


Okay thanks for the link. It helps if I understand what is going on so I will just explain what I see. 'stringstream compare(Text)' takes the string and pulls numbers until the first unvalid character(That is said in the second comment I think). The number(s) are placed in 'compare', so now every time 'compare' is referenced, it passes on the numbers it holds. Correct? If what I just said is correct, can I just do c = compare(Or is it better practice to use the >> operators?)? My train of thought on that comes from when 'ifstream file_in(//insert filename here)' is called, file_in represents the accessed file until the file is closed or assigned to a different file.

I would stick with the stream extraction opperator. c is an int and the other value is a stringstream object.

Okay. It seems that the string-to-number is fixed. The only other errors that are being thrown have to do with the "reading the file" parts. Here is the error log:

readtxt_rev2.cpp: In function ‘int main()’:
readtxt_rev2.cpp:14: error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::string&)’
/usr/include/c++/4.2.1/fstream:465: note: candidates are: std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/fstream:451: note: std::basic_ifstream<_CharT, _Traits>::basic_ifstream() [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/iosfwd:89: note: std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)
readtxt_rev2.cpp:15: error: statement cannot resolve address of overloaded function
readtxt_rev2.cpp:29: error: no match for ‘operator==’ in ‘input_line == 'Q'’
readtxt_rev2.cpp:29: error: no match for ‘operator==’ in ‘input_line == 'q'’
readtxt_rev2.cpp:43: error: statement cannot resolve address of overloaded function

Here is the updated code: http://pastebin.com/hNqLgurk

The first error message says to me that you cant open  a file with std::string so you will need to convert it to a c string for that purpose something like   ifstream file_in(filename.c_str());

i missed this last time but with this  if(input_line == 'Q' || (input_line == 'q')) it is also compairing a string to a charater literal which i dont think == has been overloaded to do.. so instead id try it with double quotes so it is string to string literal like  if((input_line == "Q") || (input_line == "q"));


In this case calling .open(); might be redundent since you provided the file name when you constructed the stream which i think opens it in a sort of defult manner.. but i cant quite remember.

Thanks a bunch. Youve really helped.