Click here to Skip to main content
15,920,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a post-lab question at the university
and i tried to write the code but it did not work

this is my post-lab question:

Plural Form of Nouns

In the English language, nouns are inflected by grammatical number — that is singular or plural. In this problem we use a simple model of constructing plural from a singular form.

This model doesn't always make English plural forms correctly, but it works in most cases. Forget about the real rules you know while solving the problem and use the statement as a formal document.

You are given several nouns in a singular form and your program should translate them into plural form using the following rules:

· If a singular noun ends with ch, x, s, o the plural is formed by adding es. For example, witch-> witches, tomato-> tomatoes.

· If a singular noun ends with f or fe, the plural form ends with ves. For example, leaf-> leaves, knife-> knives. Pay attention to the letter f becoming v.

· Nouns ending with y change the ending to ies in plural. For example, family-> families.

· In all other cases plural is formed by adding s. For example, book->books.


Input (READ from a file)

Read from the file (file1.txt) many words. A word consists from 2 to 25 lowercase Latin letters. It is not guaranteed that the given words are real English words from vocabulary.

Output (WRITE into a file)

Print n given words in their plural forms on separate lines on a file. Keep the words in the same order as they are given in the input.

What I have tried:

i start to write it like so



C++
#include <iostream>
#include <string>
#include <fstream>
using namespace std;


int main(){
	string singular;
	ifstream writing;
	ifstream reading;
	writing.open("singular.txt");

	while (!writing.eof())
	{
	cin>>singular;
		writing>>singular;
		for(int i=0;i<singular.length();i++)
			if (singular[i]>='a' && singular[i]<='z')
				continue;
			else 
			break;

			if (singular.length()-1=='o' || singular.length()-1=='x' || singular.length()-1=='s' )
				writing>>singular+"es\n";
			else if ( singular.length()-1=='f')
				writing>>singular+"ves\n";
			else if (singular.length()-2=='ch')
				writing>>singular+"es\n";
			else if (singular.length()-2=='fe')
			{	writing>>singular+"ves\n";
			writing>>singular.erase(singular.length()-1)+"ies\n";}

     		else if ( singular.length()-1=='y')
				
				writing>>singular.erase(singular.length()-1)+"ies\n";
			else
				writing>>singular+"s\n";

	}
	
	
writing.close();
return 0;}
Posted
Updated 14-Feb-18 0:44am
v2
Comments
CPallini 14-Feb-18 5:48am    
And what is the problem of your code?
OriginalGriff 14-Feb-18 5:53am    
"it did not work" is one of the error reports we get quite often, and it's always completely useless - it tells us nothing about your problem other than "I have a problem" and we knew that because you are asking a question!

What did it do that you didn't expect, or not do that you did?
When did it do it?
Are there any error messages?
What did you do to make it do that?
What have you tried to do to find out why?
What were the results?
What help do you need?

These are all questions we need an answer to - or we can't help you at all!
Member 13677678 14-Feb-18 6:07am    
well when i run it ,it shows me the black screen and enable me to writ a word
and then nothing happen
it has not write in the file and even has not create the file
i do not know why cause this way worked in the lab
// i work on visual studio 2012

The first thing you need to do is: learn to use the debugger!
This is a tool - built into Visual studio - that lets you run your code and take complete control over what happens, looking at data, executing lines one by one, and generally following what is going on.
So, it's going to be up to you. Start here: Walkthrough: Debugging a Project (C++)[^]

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Why are you reading text from the stream named writing? and every reference to that stream is reading data in, whereas the changed data should be written out. Think about the steps you need to perform in logical order.
InStream reader = // open the input file
OutStream writer = // open as output
While NOT end of file on reader
    read next token into the string variable
    if the string ends with "ch", or "x", or "s", or "o"
        Add "es" to the end
    else if ... other tests here

    write the amended string to the output file
// continue the while loop
close both files and end
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900