What is the error in the program?

1)Define a function Intext(), to create a text file named Story.txt, which should contain text lines entered by the user.
Define another function Outtext , to read and display each word of the text file Story.txt on different lines on screen.
Write a program which uses the above two function to create and display the contents of the text file Story.txt


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
PROGRAM:
#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>

void Intext()
{
	ofstream fil;
	fil.open("Story.txt");
	char line[80], ch;
	do
	{
		cout<<"Enter text:"<<endl;
		gets(line);
		fil<<line<<endl;
		cout<<"Continue?(y/n): ";
		cin>>ch;
	}while(ch=='y');
	fil.close();
}


void Outtext()
{
	ifstream fil;
	fil.open("Story.txt");
	char w[20];
	while(!fil.eof())
	{
		fil>>w;
		cout<<w<<endl;
	}
}

void main()
{
	clrscr();
	Intext();
	Outtext();
	getch();
}


OUTPUT:

Enter text:
Trees are the best friend of man on Earth.
Continue?(y/n): y
Enter text:
Trunks of the trees have concentric rings in their cross section.
Continue?(y/n): n
Trees
are
the
best
friend
of
man
on
Earth.
Trunks
of
the
trees
have
concentric
rings
in
their
cross
section.


2)Write a program which uses a function RevText(), to read the contents of the text file Story.txt (created using above program), and prints in reverse of those lines which start with an alphabet 'T'.

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
PROGRAM:
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

void RevText()
{
	ifstream fil;
	fil.open("Story.txt");
	int count=0;
	char Lin[80];
	while(count<80)
	{
		count++;
		while(fil.getline(Lin,80))
		{
		 if(Lin[1]=='T')
		 {
			 while(fil.getline(Lin,80))
			 {
				 for(int I=strlen(Lin)-1;I>=0;I--)
					 cout<<Lin[I];
				 cout<<endl;
			 }

		 }
		}
	}
}

void main()
{
	clrscr();
	RevText();
	getch();
}



OUTPUT:

That is, the screen remains blank. Whereas the screen should have shown:
.htreE no nam fo dneirF tseb eht era seerT 
.noitces ssorc rieht ni sgnir cirtncnoc evah seert eht fo sknurT


But why doesn't out put appear? Instead just a blank screen appears.. WHY?
Last edited on
closed account (Dy7SLyTq)
a) should be int main
b) does story.txt already exist?
c) it should be @ line 18 if(Lin[0] == 'T') because arrays start counting at zero
@DTSCode
Why should a) be int main()? The function main doesn't return any integer value. Rather it executed statements which is considered to be void type. So why would it be int main() and not void main()?
@DTSCode
I did Lin[0] instead of Lin[1]..but then the output comes like:
.noitces ssorc rieht ni sgnir cirtncnoc evah seert eht fo sknurT

Whereas it should have been like:
.htreE no nam fo dneirF tseb eht era seerT 
.noitces ssorc rieht ni sgnir cirtncnoc evah seert eht fo sknurT

What's the problem?

closed account (Dy7SLyTq)
it used to be void main, but the standard was changed. it actually does return an int, but it is not required to explicitly write return int. its like constructors. c++ will generate an initial constructor for you if you dont have one. in the main function it will have implicitly return (i think) 0 (it might be one) if you dont have it return something else
Okay but what about the 2nd program? Why is the output 1 line less?
closed account (Dy7SLyTq)
a) i would try flushing the input buffer
b) whats up with the first while loop in the second program. its unneccesary and pointless.
c) can you use std::string? it would make this program a lot cleaner
I removed the first while loop but I am still unable to get the desired output..What to do?
closed account (Dy7SLyTq)
could you post your code, and the contents of story.txt
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
CODE:
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

void RevText()
{
	ifstream fil;
	fil.open("Story.txt");
	int count=0;
	char Lin[80];

		while(fil.getline(Lin,80))
		{
		 if(Lin[0]=='T')
		 {
			 while(fil.getline(Lin,80))
			 {
				 for(int I=strlen(Lin)-1;I>=0;I--)
					 cout<<Lin[I];
				 cout<<endl;
			 }

		 }
		}
}

void main()
{
	clrscr();
	RevText();
	getch();
}


Contents of Story.txt:
Trees are the best friend of man on Earth.
Trunks of the trees have concentric rings in their cross section.
closed account (Dy7SLyTq)
remove the second while loop. it grabs the first line, tests for the T, and if its there grabs the next line and reverses it. your structure should look like this:
while(...)
if(...)
for(...)

notice that there is no while loop between the if and the for, like in your code
Topic archived. No new replies allowed.