问题描述
#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..
推荐答案
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!