当l == 0时,为什么我无法输入案例1 [英] Why I am not able to input case 1 when l==0

查看:65
本文介绍了当l == 0时,为什么我无法输入案例1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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");
}





我的尝试:



当我输入n = 2时,它要求一次,但它应该问两次..

问题是在某个地方//循环收到..

i输入n我要输入多少次字符串..



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..

推荐答案

无法看到你的循环运行时出现问题0..n- 1所以它必须是fgets行为。我会hazzard猜测cin输入案例数量不会删除新行字符,fgets会看到它并且通过第一次丢弃。



可能对用户和调试很好,也可以在该循环的屏幕上显示提示..比如Entry x::-)

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 << ":";





基本上只需使用调试器在fgets语句中设置一个断点并跟踪发生的情况。



我不确定你是不是想开始混合fgets和cin,因为cin不是基于换行符而fgets是。



其他狡辩的主要格式通常是



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)



和struct case应该真正定义在main和outside之外,因为最终你会希望过程和函数能够使用它。


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.


这篇关于当l == 0时,为什么我无法输入案例1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆