Hi... Do you remember me? Am the amateur programmer who disturbs you with my never ending doubts.. :D As you guys advised I have decided to start learning c++ from scratch..! ;)As far as am concerned Files in c++ is the most difficult part in c++ in my syllabus ..! yet it is the most satisfying and amusing part of it.. It feels great to create text files with out notepad..! I finds it the most satisfying face of c++ ! anyway I am still a beginner who is hungry for knowledge...Today I wrote a very simple c++ programme to show the contents of a text file which was create using notepad.. the program works correctly ,the desired output is obtained.. but the getch(); function seems to be dead.. The programming is not termination with a keystroke .. getch() has gone out of scope.. What is causing the error? Am I missing {} ?
here is the code... hoping that you would help this little bro..

#include<fstream.h>
#include<conio.h>
void main()
{
char a[20];
ifstream infile("venu.txt");
while(infile)
{
infile.getline(a,50);
cout<<a;
}
getch();

}

Recommended Answers

All 8 Replies

A lot of words for not a lot of information. :icon_rolleyes: Perhaps you can explain what you mean exactly by "The programming is not termination with a keystroke". I daresay that you're not starting off the right way. Your code is horrible, your choice of compiler is probably not going to help you learn C++, and whatever reference materials you're using are wrong.

Here's the program you should have written if your compiler were modern enough to puke on your program and you had a book that properly taught C++ (like Accelerated C++):

#include <fstream>
#include <iostream>
#include <string>

int main()
{
  std::ifstream in ( "venu.txt" );

  if ( in ) {
    std::string line;

    while ( std::getline ( in, line ) )
      std::cout<< line <<'\n';
  }

  std::cin.get();
}

At least that way you wouldn't be relying on all kinds of non-standard behavior.

Buddy..As I have already mentioned, am from rural area in kerala.. we have very limited resources.. please understand me.. We have turbo c++ v3 at school and am compiled to use that compiler.. :D So please correct the error in my c++ so that it would work with turbo c++

>we have very limited resources..
BS. You clearly have an internet connection, so I don't believe that you don't have access to any of the freely available modern compilers or good resources. You have no excuse for making a beeline to the worst possible learning materials.

>So please correct the error in my c++ so that it would work with turbo c++
You still haven't adequately explained what the problem is. Allow me to show you why:

>but the getch(); function seems to be dead..
This is a meaningless statement. "seems to be dead" doesn't tell me anything.

>The programming is not termination with a keystroke ..
Does this mean you don't get a chance to type anything before the program terminates, or does it mean that you can type all you want and nothing happens?

>getch() has gone out of scope..
C++ has a completely different meaning for the word scope, so this statement is nonsensical.

If you want to learn C++, you should care enough to do it properly. If you don't care enough to do it properly, I don't care enough to help you. But since you're so adamant about becoming a bad programmer, try this:

#include<fstream.h>
#include<stdlib.h>

void main()
{
  char a[20];
  ifstream infile("venu.txt");

  while(infile) {
     infile.getline(a,50);
    cout<<a;
  }

  system("PAUSE");
}

It is not completely his fault though, I know the schools in India, they teach "Ancient" C++. But I completly agree with Narue's first comment. You have a internet connection. So go and get "Decent Book" (I told you about Thinking in C++ by Bruce Eckels).
Be with the Standard C++ and just pour all that crap on your answer sheet in the examination , what your teacher expects from you.

You have a internet connection. So go and get "Decent Book"
Be with the Standard C++ and just pour all that crap on your answer sheet in the examination , what your teacher expects from you.

Totally agreed with that:$, it's better to learn the new C++ standard instead of the old.

BTW, The code Narue provided you with is 100% standard C++ code, if that old crappy compiler can't handle this, you should quickly get a new one, such as the free Borland compiler (or MinGW) (as already mentioned, you've internet access)...

getch() seems to be dead means implies that the programming is not terminating after displaying the contents of the file.. Nothing happens even if any key pressed.. !! Sorry for the ambiguity..

Sid, I have already downloaded Thinking in c++.. Will start studying standard c++ soon after my final exams... Until then I could do nothing but cope up with this "ancient" version

>Nothing happens even if any key pressed..
That sounds like an infinite loop. getch isn't working because you never get to that call. If my suggested fix also exhibits the same behavior, that's likely to be the problem. Fix your loop like this:

while ( infile.getline ( a, 50 ) )
  cout<< a <<'\n';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.