singular word to plural

hey guys can u help me with this code?
so i have an assignment to make a program to convert from singular to plural
and this is the sample run

sample input sample output
Contest contests
lady ladies
book books
hero heroes

this is my code


#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void main ()
{
string str,str2;
int n;
char ch,ch2;

ifstream cinInput;
cinInput.open("file1.txt");
cout<<"singular\t\tplural\n"<<endl;
while (!cinInput.eof())
{
cinInput>>str;
n=str.length();
ch=str[n-1];
ch2=str[n-2];
if (ch=='y')
str2=str.substr(0,n-1)+"ies";
else
if (ch=='o'||ch=='s'||ch=='x')
str2=str+"es";
else
if (ch=='h'&& ch2=='c')
str2=str+"es";
else
if
(ch== 'f')
str2=str.substr(0,n-1)+"ves";
else
if (ch=='e'&&ch2=='f')
str2=str.substr(0,n-2)+"ves";
else
str2=str+"s";
cout<<str<<"\t\t\t"<<str2<<endl;
}
cinInput.close();
}



i was wondering if it's correct
and why it repeats the last input twice
Looks correct to me. I tested it in the console (replaced fstream stuff with iostream and didn't have any problems).

Here it is with my test case + code tags:
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
#include <iostream>
#include <string>
using namespace std;

void main ()
{
	string str,str2;
	int n;
	char ch,ch2;

	cout<<"singular\t\tplural\n"<<endl;

	while (true)
	{
		cin>>str;
		n=str.length();
		ch=str[n-1];
		ch2=str[n-2];

		if (ch=='y')
			str2=str.substr(0,n-1)+"ies";
		else if (ch=='o'||ch=='s'||ch=='x')
			str2=str+"es";
		else if (ch=='h'&& ch2=='c')
			str2=str+"es";
		else if (ch== 'f')
			str2=str.substr(0,n-1)+"ves";
		else if (ch=='e'&&ch2=='f')
			str2=str.substr(0,n-2)+"ves";
		else
			str2=str+"s";
		
		cout<<str<<"\t\t\t"<<str2<<endl;
	}
}
Looks good to me except: The reason you are getting the last input twice is that you aren't testing for end of file after you read: You are testing it at the start of your loop. Add this code to see what I mean:

1
2
3
4
5
6

    cerr << "I just read " << str;

    cerr << ": EOF is " << boolalpha << cinInput.eof() << "." << endl << flush;



When you run your program, redirect stderr to a file like this: main 2>main.err
thank you soooooo much
u really helped me :D
Topic archived. No new replies allowed.