Jump to content

Duvida em usar uma função em C++


Joao brandao
 Share

Recommended Posts

De certeza que não tens que ser tu a definir essa função? É que nas bibliotecas comuns essa função não existe. Mas em principio tens que dar os dois argumentos que estão indicados no comentário...

“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Link to comment
Share on other sites

Eu tive a procura de mais informaçao e encontrei num post feito anteriormente e la o que tem é isto:

int strcountc(char *str, char ch){
    int i,noc=0;
    for(i=0;str!='\0';i++)
    if(str==ch) noc ++;
    return noc;
    }

eu ja vi e percebo +/- o que ele tem aqui mas mesmo assim não compreendo como usar a função.

O meu codigo ate agora estava assim:

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int soma;
    char texto[100],palavra[100];
    
    cout<<"Digite o Texto"<<endl;
    cin.getline(texto,100);
    cout<<"Digite a palavra"<<endl;
    cin.getline(palavra,100);

   soma=strcountc(texto,palavra); // é so um teste isto mas mesmo assim esta mal
    
    cout<<soma<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

Se alguem souber onde estou a errar ou como usar esta função eu agradecia 😛

Link to comment
Share on other sites

A função diz especificamente:

int strcountc(char *str, char ch)

Leva um ponteiro para char e um char. Tu estás a passar dois ponteiros para char para a função, é normal que não funcione.

Para além disso tens que colocar a função antes do main.

EDIT: E essa função está mal. Estás a comparar um ponteiro com um char e não termina, porque o ponteiro está sempre no mesmo sítio.

int strcountc(char *str, char ch){
    int i,noc=0;
    for(i=0;str!='\0';i++)  // ciclo infinito
        if(str==ch) noc ++; // comparação ponteiro vs char
    return noc;
}

“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.