Input pattern and read text

K

kittykat

Hi again,
Ok, i've solved my own problem, but now i have a tiny problem that i can't
seem to solve. :( here's my code:
#include <fstream>
#include <string>
#include <iostream>

using namespace std;

int main()
{ string pattern;
ifstream myFile("data.txt");

cout << "Enter the item you are searching for: ";
cin >> pattern;

if (getline(myFile, pattern))
cout << "The pattern was found " << endl;
else
cout << "The pattern was not found in the list\n";

}

my code complied correctly. my problem is, when it runs, the window
appears and then disappears really quickly. i was wondering how i could
solve that?? is there something wrong with my code?
 
D

Dietmar Kuehl

kittykat said:
Ok, i've solved my own problem,

Actually, I doubt that you did: the code you created does not search
for a pattern in any way!
if (getline(myFile, pattern))

The above line just reads a line and stores it in the variable
'pattern', overwriting its contents. It does not care about the
contents prior to the statement.
my code complied correctly. my problem is, when it runs, the window
appears and then disappears really quickly.

Start your program from the command line! If you insist in starting
it from some IDE, you probably have to delay the termination of the
program, e.g. making the use hit return:

std::string dummy;
std::getline(std::cin, dummy);
 
K

kittykat

thanx for pointing that out. could you explain this bit please:
"Start your program from the command line! If you insist in starting it
from some IDE, you probably have to delay the termination of the program".


I dont understand what an IDE is.
 
S

Shailesh Humbad

kittykat said:
thanx for pointing that out. could you explain this bit please:
"Start your program from the command line! If you insist in starting it
from some IDE, you probably have to delay the termination of the program".


I dont understand what an IDE is.
IDE means integrated development environment. For example, Eclipse and
Visual Studio are IDEs. What's being integrated? The compiler, linker,
and various other tools that help you build your program. One thing the
IDE does is allow you to press a button to run your program instead of
manually running it from a command line by typing its name. In your
case, you probably want to do the latter so you can see all the output.
 
K

kittykat

I'm using Dev-C++, so how shall i run my program manually. what shall i
use? and what commands shall i use? i'm a beginner in C++, and have been
thrown in the deep end, so forgive me if my questions are really basic!
 
S

Shailesh Humbad

kittykat said:
I'm using Dev-C++, so how shall i run my program manually. what shall i
use? and what commands shall i use? i'm a beginner in C++, and have been
thrown in the deep end, so forgive me if my questions are really basic!
Just go to start menu and click on "Run...". Then type in "cmd" or
"command". At the prompt, type "cd c:\my\program\directory". Then type
"myprogramname". You have to use your own directory name and program
name. Also, if you need further help, I suggest continuing this thread
by posting in the more topical alt.comp.lang.learn.c-c++. This
newsgroup is really only for C++ language-related discussion.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,537
Latest member
BellCorone

Latest Threads

Top