English 中文(简体)
未指定值
原标题:Value not being assigned
  • 时间:2012-05-22 16:13:48
  •  标签:
  • c

这是我的小片段,给我带来麻烦:

int main(int argc, char** argv) {

char string[75] = {0时 时;
char *pChar;
int count = 0;

printf("String: ");
fgets(string, sizeof string, stdin);

printf("Numero parole: %d
", countWords(string, strlen(string)));

// Suddivido la stringa nelle varie parole
pChar = strtok(string, " ");
while(pChar){
    if(isWord(pChar, strlen(pChar))){

        count += strlen(pChar);

    时 时
    pChar = strtok(NULL, " ");

时 时

printf("Lettere totali: %d
", count);

return (EXIT_SUCCESS);

时 时

问题在于没有指定值来计算变量。 我知道有问题,但我还是不知道什么是错的。

谢谢你的帮助

P.S. I 正在学习 C, 所以这可能是个愚蠢的问题。

< em> P. P. S. 按此请求设置为 word 函数 :

// Controlla se è una parola
int isWord(char string[], int length){

int i = 0;                  // Contatore
int countAlpha = 0;         // Se il carattere è alfabetico. Non vengono
                            // contate le parole che contengono numeri

// Inizio scorrendo tutta la stringa tranne l ultimo carattere che è un
// terminatore di stringa
for(i; i < length - 1; i++){

    // Se il carattere è alfabetico allora aumento il contatore isAlpha
    if(isalpha(string[i])){

        countAlpha++;

    // Altrimenti il carattere non è una lettera
    时 时 else {

        countAlpha = 0;

    时 时

时 时

if(countAlpha == i){

    return 0;

时 时 else {

    return 1;

时 时

时 时

最佳回答

如果 count 不是递增,则是由于以下原因之一:

strlen( pChar); 返回 0

(b) if(isWord(pChar,strlen(pChar))) 绝非真实

c) why( pChar) 永远无效

您可以使用交互式调试器, 并在代码执行时逐行跳过代码, 来验证哪些是有效的, 哪些是无效的 。

根据您自首次张贴此文件以来的额外更改, 问题似乎在于您的 isWord 函数。 函数似乎正在返回您想要返回的相反值。 更改它, 以便 if( count Alpha = i) 返回 1 (真实), 其 else 返回 0 (虚假) 。

问题回答

fgets might store a new line character so isWord returns false because isalpha( ) is false.

小片段可能会带来一个小小的代码解释:)

以其他人指出的方式来说,问题在于函数 isWord 的返回值,但更重要的是,你为什么要做所有这些工作?

函数 isWord 如果单词包含所有字母的话, 函数应该计算。 如果是的话, 则返回成功代码其他失败代码。 成功的主要函数将按字符串 < code>strlen 的递增计算 。

但这都有必要吗?

更改建议 : 而不是返回错误代码或成功代码, 然后让 strlen 使 isWord 返回成功字符的总数, 返回失败和递增 编号 的字符总数。 这将节省您在 strlen 函数调用 : )

这看起来好多了:难道你不同意吗?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int isWord(char string[], int length){

int i = 0,countAlpha = 0;

for(; i < length - 1; i++){

    if(!isalpha(string[i])){

        countAlpha = -1;
        break;

    }else {

        countAlpha++;

    }

}

return countAlpha;

} 

int main(int argc, char* argv[]){

char string[75] = {0};
char *pChar;
int ret = 0,count = 0;

printf("String: ");
fgets(string, sizeof string, stdin);

// Suddivido la stringa nelle varie parole
pChar = strtok(string, " ");

while(pChar){

    if( (ret=isWord(word, strlen(word))) != -1){

        count += ret;

    }

    pChar = strtok(NULL, " ");  

}

printf("Lettere totali: %d
", count);

return 0;
}




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...