本文介绍了对不起NOOB问题..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我刚刚开始做C并且还没有完成一些 年的编程......你们可以看看这个吗?谢谢你的 时间! 我的输入文件如下: 1.5 2.5 Bob, Joe 4.5 5.5 Bob,Jolene 第一次通过do while循环它工作正常但是第二次 时间它看起来像这样: Bob,Joe 38.50 12.75 490.88 Jolene 0.00 0.00 0.00 ,珍妮特0.00 0.00 0.00 - 它会切断姓氏,所有浮点数都显示为零... 命令行:./ a.out< inputFile.c int main() { 浮动inHrs,inRt,myTot; char inName [16]; 做{ / * while(fgets(inName,sizeof(inName),stdin)){ * / scanf("%f%f",& inHrs,& inRt); myTot = inHrs * inRt; fgets(inName,sizeof(inName),stdin); char * p = strchr(inName,''\ n''); if(p!= NULL){ * p =''\ 0''; }; printf ("%s \ t%4.2f%4.2f%4.2f \ n",inName,inHrs, inRt,myTot); inHrs = 0 ; inRt = 0; } while(fgets(inName,sizeof(inName),stdin)!= NULL); / * }; * / 返回0; } 解决方案 每次迭代不需要三次fgets()调用。 调用fgets()一次,然后解析字符串。 Strtok()可能很好剥离除了令牌。 您还可以使用ctype.h中声明的is *()函数,例如 isdigit(),isalpha(),ispunct() 摘要: 1.读取输入行 2.将行内容分成组件。您可以使用 strtok()甚至sscanf() 3.处理组件 4.获取下一行(如果有的话)并重复。 每次迭代不需要三次fgets()调用。 调用fgets()一次,然后解析字符串。 Strtok()可能会剥离令牌。 您还可以使用ctype.h中声明的is *()函数,例如 isdigit(),isalpha(),ispunct() 总结: 1.阅读输入行 2 。将行的内容分成组件。您可以使用 strtok()甚至sscanf() 3.处理组件 4.获取下一行(如果有的话)并重复。 感谢您的帮助! 由于代码中唯一的输入来自stdin,这个文件 与任何东西有什么关系。 此时您输入的是什么? 你在这里输入了什么? 你在这里输入了什么? 我认为你的问题至少有一部分是没有输入/> 提示并忘记了你的位置。您是否记得为此最后一个fgets输入了一个 虚拟名称(因为真实姓名是在您输入数字后输入的?) I''m just starting C and haven''t done programming for a fewyears...could you guys please take a look at this? Thanks for yourtime!I have an input file that looks like:1.5 2.5 Bob, Joe4.5 5.5 Bob, JoleneThe first time through the do while loop it works fine but the secondtime it looks like this:Bob, Joe 38.50 12.75 490.88Jolene 0.00 0.00 0.00, Janet 0.00 0.00 0.00- It cuts off last name and all of the floats show up zeros...command line: ./a.out < inputFile.cint main(){float inHrs, inRt, myTot;char inName[16];do {/* while ( fgets(inName, sizeof(inName), stdin) ) {*/ scanf ( "%f%f", &inHrs, &inRt );myTot = inHrs * inRt;fgets( inName, sizeof( inName ), stdin);char * p = strchr( inName,''\n'' );if ( p != NULL ){*p = ''\0'';};printf ( "%s\t%4.2f %4.2f %4.2f \n", inName, inHrs,inRt, myTot );inHrs = 0;inRt = 0;} while ( fgets(inName, sizeof(inName), stdin) != NULL );/*};*/return 0;} 解决方案No need for three fgets() calls per iteration.Call fgets() once and then parse the string.Strtok() is probably fine to peel apart the tokens.You can also use the is*() functions declared in ctype.h such asisdigit(), isalpha(), ispunct()In summary:1. Read a line of input2. Separate the contents of the line into components. You could usestrtok() or even sscanf()3. Process the components4. Get the next line (if any) and repeat.No need for three fgets() calls per iteration.Call fgets() once and then parse the string.Strtok() is probably fine to peel apart the tokens.You can also use the is*() functions declared in ctype.h such asisdigit(), isalpha(), ispunct()In summary:1. Read a line of input2. Separate the contents of the line into components. You could usestrtok() or even sscanf()3. Process the components4. Get the next line (if any) and repeat.Thanks for the help!Since the only input in your code is from stdin, what does this filehave to do with anything.Exactly what did you type in at this point?And what did you type here?And what did you type here?I think at least part of your problem is you have inputs withoutprompts and lost track of where you were. Did you remember to type adummy name for this last fgets (because the real name comes after youinput the figures)?Remove del for email 这篇关于对不起NOOB问题..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-22 19:37