reading text from file

i have a txt file, it has series like:
1
a
2
b
.
.
.
12
l
*
.
.
.
something like this..and i wanted to print the text till * when it finds 12..but my code keeps on printing 12,12,12...for infinite times...please chk my code.
please ignore the random number part becoz i was trying to do it with any random number. anyway that loops does not make any difference...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "stdafx.h"
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () 
{
	char answer;
	int min=1,max=100,c[100]={0}; 
									
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
	  for (int i=1; i<=100; i++)
	  {again:
	int randNum = rand()%(max-min + 1) + min;
	
		c[i]=randNum;
		for (int j=1; j<=100; j++)
		{
			if (c[j-1]==randNum)    //[0 12 41 56 41]
				goto again;
			else
				{			
				while ( getline (myfile,line) )
				{
				
					if (line == "12")
					{
						while ( line != "*" )
						{
							
							cout << line << '\n';
						}
					}
				}
				}
			myfile.close();
		}
	}
  }
	
			
			else cout << "Unable to open file"; 
			
			return 0;
}

Issue is by your while loop,

when you say
While(line != "*")

the first while loop which is while(geline(myfile,line)
is paused and will wait for its turn to run after the
while(line !="*")

so getline(myfile, line) no longer runs and everytime while(line != "*") checks the line it still 12....

just add getline(myfile, line) inside the second while loop.
Last edited on
it worked mann...thanx..but 1 more thing, i want it to stop printing as soon as it finds * i tried placing the break statement at some places where i thought it would work but its not working..can u please have a look at it.?
Simply do and if statement in the the while loop if its * it breaks and if its not it prints
i have done this...but it is still not stopping...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// readin.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () 
{
	char answer;
	int min=1,max=100,c[100]={0};//c is started from 0 value since i want to make sure that a random no should never be 
									//repeated and if repeated, should be checked one prior to the latest no generated
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
	  for (int i=1; i<=100; i++)
	  {again:
	int randNum = rand()%(max-min + 1) + min;
	
		c[i]=randNum;
		for (int j=1; j<=100; j++)
		{
			if (c[j-1]==randNum)    //[0 12 41 56 41]
				goto again;
			else
				{			
				while ( getline (myfile,line) )
				{
					if (line == "12")
					{
							while ( getline (myfile,line) )
							{
								if (line == "*")
									break;
								else
								cout << line << '\n';
							}
						}
						
					}
						
				}
				
				}
			myfile.close();
		
	}
  }
	
			
			else cout << "Unable to open file"; 
			
			return 0;
}
Topic archived. No new replies allowed.