大家好,请您帮我编码 [英] Hello people, can you please help with my coding

查看:85
本文介绍了大家好,请您帮我编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,请看看...

问题是:

1)我无法在黑屏上输入第二个字

2)我需要第二个单词的索引,但没有结果



我已经多次尝试纠正它,但我找不到,dunno如何处理它

Here are my codes, take a look please...
The problems are:
1) I cannot type the second word on the black screen
2) I need an index of that second word, but no result

I have tried several times to correct it, but i couldn't find, dunno how to deal with it

#include <iostream>
using namespace std ;

char IndexWord(char* s, int n);
char IndexWord(char* s, int n)
{
    int num = strlen(s);		
    char *word = new char[100];
    int index;	
    int count = 0;     
    for (int i = 0 ; i < num; i++) { 
        if (s[i] == ' ') { 
            if (s[i+1] != ' ') { 
                count++; 	 
    	   }    	         
            if (count = n){
                for (int j =0; j <= s[i]; j++){
                    if ( word[j] = s[i]){
                        index = j; 
                        return j;
                    }					  
                }
            } 
            else if (count != n){
                return -1;
            }			   
        }			  
    }
}

int main( ) 
{    
    char s[100];
    int n;   
    cout << "Enter a word:" << endl;
    cin >> s; 
    cout << "Enter a number of any word, from previous word:" << endl;
    cin >> n;   
    cout << IndexWord(&s[100],n) << endl;
    // as the result i should get the index of the number of word i have typed
    system("pause");
    // but i don't know what's wrong with these codes
    return 0;
 }

推荐答案

除了解决方案1中的内容之外,您的代码还存在以下问题:





1)缓冲区的硬编码大小。



2) word 从不删除,似乎没有任何实际用途。



3)大多数循环不检查字符串字符结束:('\ 0')。



4) if(word [j] = s [i])条件看起来很可疑,因为它是一个赋值,并且将 word 内容未初始化且随机。如果分配是正确的,那么它将是无用的,因为变量不会在之后重复使用。



5)分配而不是在中进行比较if( count = n){



6)循环结束条件 j< = s [i] 可能是错误的。你确定你想要那个位置的值吗?



7)条件 if(count!= n)是无用的(假设在前面,如果它应该是 == 而不是 = )。 br />


8)我想你想一次输入所有单词,因为提示是误导性的。



9)目前尚不清楚第二个提示意味着什么(即数字 n )。





顺便说一下,你应该学会使用调试器并跟踪你的代码来找出问题。



或者编写一部分代码一段时间(并输出你正在做的事情),当你得到预期的输出时,移动到下一步。
In addition to what is said in solution 1, there are the following problem with your code:


1) Hard coded size for buffers.

2) word is never deleted and it does not seems to have any real use.

3) Most loops does not check for end of string character: ('\0').

4) The if (word[j] = s[i]) condition look suspicious as is it an assignation and it won't make sense to do an equality comparison as word content as not been initialized and would be random. If the assignation is correct, then it would be useless as the variable is not reused afterward.

5) Assignment instead of comparison in if (count = n){.

6) The loop ending condition j <= s[i] is probably wrong. Are you sure that you want the value at that position?

7) The condition if (count != n) is useless (assuming that in the previous if it should be an == instead of an =).

8) I think that you want to enter all words at once, thuis the prompt is misleading.

9) It is not clear what the second prompt mean (that is number n).


By the way, you should learn to use a debugger and trace your code to figure out the problem.

Or write the code one part at a time (and output what you are doing) and when you got the expected output, move top the next step.


&s [100]给出s中最后一个元素的地址数组,然后将其传递给函数。因为你要离开最后一个索引strlen(s)将为零。



在第二个cin中,你试图将它粘在一个整数中,所以它不会让你输入一个字符串。



我认为你应该看一下关于指针和运算符地址的参考,以及使用cin和变量在再次尝试之前输入。
&s[100] gives the address of the last element in the "s" array, then passes it to the function. Since you are passing off the last index strlen(s) is going to be zero.

In the second cin, you are trying to stick it in an integer, so its not letting you enter a string.

I think you should take a look at a reference about pointers and the address of operator, along with the use of cin and variable typing before trying this again.


这篇关于大家好,请您帮我编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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