关于这个问题的简单问题是,为什么它不打印字符串上的第二个值(转换后的第二个值)?
快速提问,我在这里做错了什么。此代码的目的是将输入获取为字符串,输入为“12 34”,“12”和“32”之间有一个空格,并转换并打印来自整数变量的两个单独的数字数字。为什么对函数 copyTemp 的第二次调用不产生值 34?我有一个 index_counter 变量,它跟踪字符串索引及其意味着跳过“空格”字符?我做错了什么?
谢谢。
#include <stdio.h>
#include <string.h>
int index_counter = 0;
int number;
void copyTemp(char *expr,char *temp);
int main(){
char exprstn[80]; //as global?
char tempstr[80];
gets(exprstn);
copyTemp(exprstn,tempstr);
printf("Expression: %s\n",exprstn);
printf("Temporary: %s\n",tempstr);
printf("number is: %d\n",number);
copyTemp(exprstn,tempstr); //second call produces same output shouldnt it now produce 34 in the variable number?
printf("Expression: %s\n",exprstn);
printf("Temporary: %s\n",tempstr);
printf("number is: %d\n",number);
return 0;
}
void copyTemp(char *expr,char *temp){
int i;
for(i = index_counter; expr[i] != '\0'; i++){
if (expr[i] == '0'){
temp[i] = expr[i];
}
if (expr[i] == '1'){
temp[i] = expr[i];
}
if (expr[i] == '2'){
temp[i] = expr[i];
}
if (expr[i] == '3'){
temp[i] = expr[i];
}
if (expr[i] == '4'){
temp[i] = expr[i];
}
if (expr[i] == '5'){
temp[i] = expr[i];
}
if (expr[i] == '6'){
temp[i] = expr[i];
}
if (expr[i] == '7'){
temp[i] = expr[i];
}
if (expr[i] == '8'){
temp[i] = expr[i];
}
if (expr[i] == '9'){
temp[i] = expr[i];
}
if (expr[i] == ' '){
temp[i] = '\0';
sscanf(temp,"%d",&number);
index_counter = i+1; //skips?
}
}
// is this included here? temp[i] = '\0';
}
Quick question, What have I done wrong here. The purpose of this code is to get the input into a string, the input being "12 34", with a space in between the "12" and "32" and to convert and print the two separate numbers from an integer variable known as number. Why doesn't the second call to the function copyTemp, not produce the value 34?. I have an index_counter variable which keeps track of the string index and its meant to skip the 'space' character?? what have i done wrong?
thanks.
#include <stdio.h>
#include <string.h>
int index_counter = 0;
int number;
void copyTemp(char *expr,char *temp);
int main(){
char exprstn[80]; //as global?
char tempstr[80];
gets(exprstn);
copyTemp(exprstn,tempstr);
printf("Expression: %s\n",exprstn);
printf("Temporary: %s\n",tempstr);
printf("number is: %d\n",number);
copyTemp(exprstn,tempstr); //second call produces same output shouldnt it now produce 34 in the variable number?
printf("Expression: %s\n",exprstn);
printf("Temporary: %s\n",tempstr);
printf("number is: %d\n",number);
return 0;
}
void copyTemp(char *expr,char *temp){
int i;
for(i = index_counter; expr[i] != '\0'; i++){
if (expr[i] == '0'){
temp[i] = expr[i];
}
if (expr[i] == '1'){
temp[i] = expr[i];
}
if (expr[i] == '2'){
temp[i] = expr[i];
}
if (expr[i] == '3'){
temp[i] = expr[i];
}
if (expr[i] == '4'){
temp[i] = expr[i];
}
if (expr[i] == '5'){
temp[i] = expr[i];
}
if (expr[i] == '6'){
temp[i] = expr[i];
}
if (expr[i] == '7'){
temp[i] = expr[i];
}
if (expr[i] == '8'){
temp[i] = expr[i];
}
if (expr[i] == '9'){
temp[i] = expr[i];
}
if (expr[i] == ' '){
temp[i] = '\0';
sscanf(temp,"%d",&number);
index_counter = i+1; //skips?
}
}
// is this included here? temp[i] = '\0';
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的程序中存在一些问题:
expr
和temp
数组。这适用于第一次,两者都将为
0
首先但是当你想要的时候
处理
2nd
数字,你需要将索引重置到
temp
数组中返回
0
。显然这不可能使用单个索引完成。你会
必须使用两个索引,
i
和j
。处理第二个数字(
34
)“12 34”
)您将到达字符串,因此
sscanf
永远不会第二次运行(在
一般用于最后一次)。所以
在 for 循环之后你需要另一个
sscanf 提取最后一个数字。此外,一旦您从字符串中提取了数字并增加了 i,您就应该从函数返回。
gets
() 并使用出于安全考虑,用
fgets
() 代替原因。
将数字放入单个测试中作为
显示:
类似这样的东西。
经过这些更改后,它按预期工作:
您的方法非常脆弱......如果用户在输入数字之间给出多个空格......您的程序将失败。
There are a few problems in your program:
expr
andtemp
arrays. This works forthe first time since both will be
0
to start with but when you want to
process the
2nd
number, you need toreset the index into the
temp
arrayback to
0
. Clearly this cannot bedone using a single index. You'll
have to use two indices,
i
andj
.processing of the 2nd number (
34
in"12 34"
) you'll reach the end of thestring and hence the
sscanf
nevergets run on the second occasion ( in
general for the last occasion). So
after the for loop you need another
sscanf to extract the last number. Also you should return from the function once you've extracted the number from the string and incremented i.
gets
() and usefgets
() instead because of securityreasons.
the digits into a single test as
shown:
Something like this.
After these changes it works as expected:
Your approach is very fragile...if a user gives multiple spaces between the input numbers..your program will fail.
主要问题是
copyTemp
写入temp[i]
,但每次调用copyTemp
都会将i
初始化为 < code>index_counter,而不是 0。这意味着每次调用copyTemp
都会追加到现有的temp
缓冲区,而不是覆盖旧内容,并且因此 sscanf 总是重新读取相同的字符串。您需要使用单独的索引来跟踪从输入缓冲区读取的位置以及写入输出缓冲区的位置。
附加问题:
* 永远不要**使用
ggets
。 曾经。请改用fgets
。* 您在
copyTemp
中复制了大量代码。相反,您可以这样做:或者更好:
copyTemp
应该采取一些预防措施,以免溢出其目标缓冲区。 (请注意,copyTemp
甚至不需要将目标缓冲区作为参数。)您应该避免使用全局变量。
copyTemp
最好采用一个参数来指定从输入字符串开始读取的位置以及是否返回停止位置的索引。The main problem is that
copyTemp
writes totemp[i]
, but each call tocopyTemp
initializesi
toindex_counter
, not to 0. This means that each call tocopyTemp
appends to the existingtemp
buffer instead of overwriting the old contents, andsscanf
thus always re-reads the same string. You need to use separate indices to keep track of where to read from the input buffer and where to write to the output buffer.Additional problems:
* Never** use
ggets
. Ever. Usefgets
instead.* You duplicate a lot of code in
copyTemp
. You instead could do:or better:
copyTemp
should take some precautions to not overflow its destination buffer. (Note thatcopyTemp
shouldn't even need to take a destination buffer as an argument.)You should avoid using global variables. It'd be better for
copyTemp
to take an argument specifying where to start reading from the input string and if it returned the index where it left off.