Click here to Skip to main content
15,915,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include<iostream>
#include<string>
#include<stdio.h>
void main()
{
	using namespace std;
	struct cases
	{
		char str[30];
		int i, j, end;

	};
	cases c[5];
	int n,l;
	cout << "Enter the no. of cases\n";
	cin >> n;
	//now looping to recieve the strings
	for ( l = 0;l < n;l++)
	{
		fgets(c[l].str, 25, stdin);
	//get each string info
		c[l].end = strlen(c[l].str)-2;
		c[l].i = c[l].end;
	}
	//now looping to initiate each process
	for (int l = 0;l < n;l++,cout<<"\n")
	{ 
		
		cout << "case #" << l + 1<<": ";
		//scan the word
		a:
		while ((c[l].str[c[l].i]!= ' ') && (c[l].i>-1))
		{
			c[l].i--;
		}
		for (c[l].j = (c[l].i) + 1;c[l].j <= c[l].end; c[l].j++)
		{
			cout << c[l].str[c[l].j];
		}
		cout << " ";
		(c[l].i)--;
		c[l].end = (c[l].i);
		if (c[l].i != -2)
			goto a;

	}





	system("pause");
}


What I have tried:

when i input n= 2 , it asks for once but it should ask twice ..
the problem is in somewhere //looping to recieve ..
i input n that how many times i want to input the strings..
Posted
Updated 19-Jun-16 16:55pm

1 solution

Can't see a problem with your loops they run from 0..n-1 so it must be fgets behaviour. I will hazzard a guess cin for entering number of cases doesn't remove the new line character and fgets sees it and drops thru first time.

Might be nice for user and debugging to put a prompt on the screen in that loop as well .. like "Entry x: " :-)
cout << "Entry " << l+1 << ":";


Basically just use the debugger put a break point on the fgets statement and follow what happens.

I am not sure you want to start mixing fgets and cin as cin isn't based around newline behaviour while fgets is.

Other quibbles the format of main is usually
int main(int argc, char**argv)

and struct case should really be defined above and outside main as eventually you will want procedure and functions to be able to use it.
 
Share this answer
 
v10

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