9 Replies - 1665 Views - Last Post: 21 December 2011 - 10:17 PM Rate Topic: -----

#1 LadyJoana  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-November 11

Question - How can i compare two names?

Posted 21 December 2011 - 11:13 AM

Hi i'm developing a program as part of a school project. In this program part of it asks the user to type in an activity to modify, in other words to change an activity for another. So my question is how can i verify if the activity i typed is valid, by which i mean it already exists in the program? Do i use strcmp? And if so with what do i compare it with, where do i get the activity from? I dont want to compare it with something definite because all the activities are typed in by the user.

Is This A Good Question/Topic? 0
  • +

Replies To: Question - How can i compare two names?

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Question - How can i compare two names?

Posted 21 December 2011 - 11:24 AM

it sounds like you want a container that maps strings to activities. if you using C++ you can do this with std::map. if your using C then you have to make you own data structure; a list of name/activity pairs in which you can scan for the matching name(or lack there of) would be the easiest.
Was This Post Helpful? 0
  • +
  • -

#3 LadyJoana  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-November 11

Re: Question - How can i compare two names?

Posted 21 December 2011 - 11:29 AM

But how can i do that if i'm dependent of something thats going to be typed in by the user?

I'm using C language by the way
Was This Post Helpful? 0
  • +
  • -

#4 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Question - How can i compare two names?

Posted 21 December 2011 - 11:52 AM

you can dynamically add elements to the sequence. for instance, you might have he struct...

typedef struct {
    char* str; //name of action
    void(*action)(); //function pointer to action
} key_value_pair;



which can be stored in an array, you may want to use a struct to group the size and array together
typedef struct {
    size_t size; //size of array
    key_value_pair* data; //the actual array
} key_value_array;



then you would resize that every so often to accommodate new actions; resizing every power of 2 is a common option.

this all of course assumes you know about dynamic memory allocation. what you asking can't really be done with out it. you *could* make a large static array and just set a cap on how many actions can be stored but it's not the best idea.

edit: a linked list is another option if you are more comfortable with that than an array.

This post has been edited by ishkabible: 21 December 2011 - 11:53 AM

Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Question - How can i compare two names?

Posted 21 December 2011 - 02:21 PM

Possibly overkill here, but you could create a hash table to map the names of your activities to their actual functions. Here's a snippit showing how to map string values to functions.

http://www.dreaminco...snippet6430.htm

This post has been edited by jjl: 21 December 2011 - 02:31 PM

Was This Post Helpful? 1
  • +
  • -

#6 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 656
  • View blog
  • Posts: 2,247
  • Joined: 31-December 10

Re: Question - How can i compare two names?

Posted 21 December 2011 - 02:27 PM

How about letting the OP write their own code without giving it to them? This is from the C/C++ rules thread:

Quote

If you are answering someone's question, only do so if they have made an attempt. We will delete any posts providing code when a user has not shown some effort. Kindly ask the user to post what they've done so far.

Was This Post Helpful? -1
  • +
  • -

#7 LadyJoana  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-November 11

Re: Question - How can i compare two names?

Posted 21 December 2011 - 02:32 PM

I apreciate the effort but i have no idea how to use any of the code points you gave me... i'm still new at this and i only know the basic key points (printf scanf puts gets), i'm just starting on how to make tables and matrix...

thanks anyways
Was This Post Helpful? 0
  • +
  • -

#8 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Question - How can i compare two names?

Posted 21 December 2011 - 02:33 PM

View Postvividexstance, on 21 December 2011 - 09:27 PM, said:

How about letting the OP write their own code without giving it to them? This is from the C/C++ rules thread:

Quote

If you are answering someone's question, only do so if they have made an attempt. We will delete any posts providing code when a user has not shown some effort. Kindly ask the user to post what they've done so far.


The code is unrelated enough to be shown to the OP, however I will post the link to the snippit it came from if it makes you happy.

Quote

I apreciate the effort but i have no idea how to use any of the code points you gave me... i'm still new at this and i only know the basic key points (printf scanf puts gets), i'm just starting on how to make tables and matrix...

thanks anyways


Could you explain what exactly you are trying to accomplish?

This post has been edited by jjl: 21 December 2011 - 02:35 PM

Was This Post Helpful? 1
  • +
  • -

#9 LadyJoana  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-November 11

Re: Question - How can i compare two names?

Posted 21 December 2011 - 02:48 PM

This project allows one to organize a time table, my problem is that i have to build this while learning how to construct the code. I'm at phase two of the project where i have to construct everything into submenus and organize the data the program requests, but part of that would mean t«i would have to access previous data.

For example

at the part of submenu MActividade
i have to modify an activity i inserted in previously to another one. My question is am i able to do this without having anything to compare it to and thus it doesnt show the option introduced is invalid.

PS. Be kind when writting this is not my native language, and the finer points of programming are somewhat lost during translation since my classes arent teached in english. So avoid using specific programming language names (ex: map strings-I have no idea how to even do this), because i will have no idea what that refers to.


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

void MenuPrincipal (void);
void MostrarMenuPrincipal (void);
void MenuGerirHorario (void);
void GerirHorario (void);
void IActividade (void);
void MActividade (void);
void RActividade (void);
void MHorario (void);
void MenuVisualizacao (void);
void Visualizacao (void);
void MHorarioCompleto (void);
void MHorarioEscolar (void);
void MHorarioHobby (void);
void MenuEstatistica (void);
void Estatistica (void);
void NHorasSemana (void);
void DSemana (void);
void Racio (void);
char Sair (void);

int main(void)  /*Programa Principal*/
{
    MenuPrincipal ();   /*Chama o Menu Principal*/

    return 0;
}

void MostrarMenuPrincipal() /*Mostra Menu Principal*/
{
    system ("cls");
    printf("*\t*\t*\t*\t*\t*\t*\t*\t*\t*\n");
    printf("\t\t\t\tMenu Princpal:\n");
    printf("*\t*\t*\t*\t*\t*\t*\t*\t*\t*\n");
    printf("\t\t\t\th) Gerir Horario\n");
    printf("\t\t\t\tv) Visualicao\n");
    printf("\t\t\t\te) Estatistica\n");
    printf("\t\t\t\ts) Sair\n");
    printf("\n");
}

void MenuPrincipal ()   /*Menu Principal*/
{
    char opmp;
    do
    {
        MostrarMenuPrincipal ();    /*Menu Principal*/
        printf ("\t\t\t\tOpcao Escolhida:");
        fflush(stdin);
        scanf("\t\t\t\t%c", &opmp);
        opmp=tolower(opmp);

        switch (opmp)
        {
        case 'h':
            GerirHorario(); /*Opcao Gerir Horario*/
            break;

        case 'v':
            Visualizacao ();    /*Opcao Visualizacao*/
            break;

        case 'e':
            Estatistica (); /*Opcao Estatistica*/
            break;

        case 's':   /*Opcao Sair*/
            opmp=Sair ();
            break;

        default:
            printf ("\t\t\t\tOpcao Invalida.\n");
            system ("pause");
        }

    }
    while (opmp!='s' && opmp!='S');

    return 0;
}

void MenuGerirHorario ()    /*Mostra Menu Gerir Horario*/
{
    system ("cls");
    printf("*\t*\t*\t*\t*\t*\t*\t*\t*\t*\n");
    printf("\t\t\t\tMenu Gerir Horario:\n");
    printf("*\t*\t*\t*\t*\t*\t*\t*\t*\t*\n");
    printf("\t\t\t\ti) Introduzir Actividade\n");
    printf("\t\t\t\tm) Modificar Actividade\n");
    printf("\t\t\t\tr) Remover Actividade\n");
    printf("\t\t\t\th) Mostrar Horario\n");
    printf("\t\t\t\tv) Voltar\n");
    printf("\n");
}

void GerirHorario ()    /*Gerir Horario*/
{
    char opsm1;
    do
    {
        MenuGerirHorario ();    /*Chama Menu Gerir Horario*/
        fflush(stdin);
        printf("\t\t\t\tSeleccione uma opcao:");
        scanf("\t\t\t\t%c", &opsm1);
        opsm1=tolower(opsm1);
        switch (opsm1)
        {
        case 'i':
            IActividade (); /*Opcao Introduzir Actividade*/
            break;
        case 'm':
            MActividade (); /*Opcao Modificar Actividade*/
            break;
        case 'r':
            RActividade (); /*Opcao Remover Actividade*/
            break;
        case 'h':
            MHorario ();    /*Opcao Mostrar Horario*/
            break;
        case 'v':
            printf("\t\t\t\tVoltar\n\t\t\t\t"); /*Voltar ao menu anterior*/
            break;
        default:
            printf ("\t\t\t\tOpcao Invalida.\n\t\t\t\t");
        }
        system ("pause");
    }
    while (opsm1!='v' && opsm1!='V');

    return 0;
}

void IActividade () /*Opcao Introduzir Actividade*/
{
    char act[100];
    short h, h1, m, m1;
    printf("\n");
    printf ("\t\t\t\tIntroduza uma actividade:");
    fflush(stdin);
    gets(act);
    printf("\n");
    printf ("\t\t\t\tIntroduza a hora e os minutos em que\n\t\t\t\tinicia esta actividade:");
    scanf ("%hi%hi", &h, &m);
        printf("\n");
    printf ("\t\t\t\tIntroduza a hora e os minutos em que\n\t\t\t\ttermina esta actividade:");
    scanf ("%hi%hi", &h1, &m1);
    if (strlen(act)>100 || strlen(act)<0)
        printf ("Actividade Invalida");
    else
    {
        printf ("\t\t\t\tActividade Introduzida:%s %hi:%hi-%hi:%hi\n\t\t\t\t", act,h,m,h1,m1);

        return 0;
    }
}

void MActividade () /*Opcao Modificar Actividade*/
{
    char act[100], act1[100];
    printf("\n");
    printf ("\t\t\t\tIntroduza a actividade a modificar:");
    fflush(stdin);
    gets (act);
    printf("\n");
    printf ("\t\t\t\tIntroduza a nova actividade:");
    fflush(stdin);
    gets (act1);
    printf("\n");
    if (strlen (act)>100 || strlen (act)<1 || strlen (act1)>100 || strlen(act1)<1)
        printf ("\t\t\t\tActividade Invalida\n");
    else
    {
        *act=*act1;
        printf ("\t\t\t\tActividade Modificada para:%s\n\t\t\t\t", act1);
    }
    return 0;
}

void RActividade () /*Opcao Remover Actividade*/
{
    char act[100];
    printf("\n");
    printf ("\t\t\t\tIntroduza a actividade a remover:");
    fflush(stdin);
    gets (act);
    printf("\n");
    printf ("\t\t\t\tActividade %s removida\n\t\t\t\t", act);
    return 0;
}

void MHorario ()    /*Opcao Mostrar Horario*/
{
    printf("\n");
    printf ("\t\t\t\tHorario:\n");
    printf("\n");
    printf ("\t\t\t\tEm Construcao...\n\t\t\t\t");
}

void MenuVisualizacao ()       /* Mostra Menu Visualizacao*/
{
    system ("cls");
    printf("*\t*\t*\t*\t*\t*\t*\t*\t*\t*\n");
    printf("\t\t\t\tMenu Visualizacao:\n");
    printf("*\t*\t*\t*\t*\t*\t*\t*\t*\t*\n");
    printf("\t\t\t\tc) Horario Completo\n");
    printf("\t\t\t\te) Horario Escolar\n");
    printf("\t\t\t\th) Horario dum Hobby\n");
    printf("\t\t\t\tv) Voltar\n");
    printf("\n");
}

void Visualizacao (void)    /*Opcao Visualizacao*/
{
    char opsm2;
    do
    {
        MenuVisualizacao ();    /*Mostra Menu Visualizacao*/
        fflush (stdin);
        printf("\t\t\t\tSeleccione uma opcao:");
        scanf("\t\t\t\t%c", &opsm2);
        opsm2=tolower(opsm2);

        switch (opsm2)
        {
        case 'c':
            MHorarioCompleto ();    /*Opcao Mostra Horario Completo*/
            break;
        case 'e':
            MHorarioEscolar (); /*Opcao Mostra Horario Escolar*/
            break;
        case 'h':
            MHorarioHobby ();   /*Opcao Mostra Horario de um Hobby*/
            break;
        case 'v':
            printf("\t\t\t\tVoltar\n\t\t\t\t"); /*Voltar ao menu anterior*/
            break;
        default:
            printf ("\t\t\t\tOpcao Invalida.\n\t\t\t\t");
        }
        system ("pause");
    }
    while (opsm2!='v' && opsm2!='V');

    return 0;
}

void MHorarioCompleto ()    /*Opcao Mostra Horario Completo*/
{
    printf("\n");
    printf ("\t\t\t\tHorario Completo\n");
    printf("\n");
    printf ("\t\t\t\tEm Construcao...\n\t\t\t\t");
}

void MHorarioEscolar () /*Opcao Mostra Horario Escolar*/
{
    printf("\n");
    printf ("\t\t\t\tHorario Escolar\n");
    printf("\n");
    printf ("\t\t\t\tEm Construcao...\n\t\t\t\t");

}

void MHorarioHobby ()   /*Opcao Mostra Horario de um Hobby*/
{
    char hobby[20];
    printf("\n");
    printf ("\t\t\t\tIntroduza o hobby pretendido:");
    fflush(stdin);
    gets (hobby);
    printf("\n");
    if (strlen(hobby)>10 || strlen(hobby)<1)
        printf ("\t\t\t\tHobby invalido\n");
    else
    {
        printf("\n");
        printf ("\t\t\t\tHorario para o hobby %s\n", hobby);
        printf("\n");
        printf ("\t\t\t\tEm Construcao...\n\t\t\t\t");
    }
}

void MenuEstatistica () /*Mostra Menu Estatistica*/
{
    system ("cls");
    printf("*\t*\t*\t*\t*\t*\t*\t*\t*\t*\n");
    printf("\t\t\t\tMenu Estatistica:\n");
    printf("*\t*\t*\t*\t*\t*\t*\t*\t*\t*\n");
    printf("\t\t\t\tt) Numero total de horas de aulas por semana\n");
    printf("\t\t\t\td) Dia de semana com maior carga de horario\n");
    printf("\t\t\t\tr) Racio entre numero de horas de hobbies e\n\t\t\t\t o numero de total de horas\n");
    printf("\t\t\t\tv) Voltar\n");
    printf("\n");
}

void Estatistica () /*Opcao Estatistica*/
{
    char opsm3;
    do
    {
        MenuEstatistica (); /*Mostra Menu Estatistica*/
        fflush(stdin);
        printf("\t\t\t\tSeleccione uma opcao:");
        scanf("\t\t\t\t%c", &opsm3);
        opsm3=tolower(opsm3);

        switch (opsm3)
        {
        case 't':
            NHorasSemana ();    /*Opcao Numero total de horas de aulas por semana*/
            break;
        case 'd':
            DSemana (); /*Opcao Dia de semana com maior carga de horario*/
            break;
        case 'r':
            Racio ();   /*Opcao Racio entre numero de horas de hobbies e o numero de total de horas*/
            break;
        case 'v':
            printf("\t\t\t\tVoltar\n\t\t\t\t"); /*Voltar ao menu anterior*/
            break;
        default:
            printf ("\t\t\t\tOpcao Invalida.\n\t\t\t\t");
        }
        system ("pause");
    }
    while (opsm3!='v' && opsm3!='V');

    return 0;
}

void NHorasSemana ()     /*Opcao Numero total de horas de aulas por semana*/
{
    printf("\n");
    printf ("\t\t\t\tEm Construcao\n\t\t\t\t");

}

void DSemana () /*Opcao Dia de semana com maior carga de horario*/
{
    printf("\n");
    printf ("\t\t\t\tEm Construcao\n\t\t\t\t");

}

void Racio ()   /*Opcao Racio entre numero de horas de hobbies e o numero de total de horas*/
{
    printf("\n");
    printf ("\t\t\t\tEm Construcao\n\t\t\t\t");

}

char Sair ()     /*Opção Sair*/
{
    char sair, opmp;
    do
    {
        system ("cls");
        printf("*\t*\t*\t*\t*\t*\t*\t*\t*\t*\n");
        printf("\t\t\t\tDeseja Sair (s/n)?\n");
        printf("*\t*\t*\t*\t*\t*\t*\t*\t*\t*\n");
        fflush(stdin);
        scanf("\t\t\t\t%c", &sair);
        if (sair=='s' || sair=='S')
            opmp='s';
        else if (sair=='n' || sair=='N')
            opmp='n';
        else
            opmp='c';
    }
    while (opmp=='c');

    return opmp;

}


Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is offline

  • member icon


Reputation: 4018
  • View blog
  • Posts: 12,400
  • Joined: 25-December 09

Re: Question - How can i compare two names?

Posted 21 December 2011 - 10:17 PM

One of the first things I will recommend it that you check your compiler settings and insure warnings are being emitted. When I compile your code I get the following messages:

Quote

main.cpp||In function ‘void MenuPrincipal()’:|
main.cpp|54|error: ‘tolower’ was not declared in this scope|
main.cpp|82|error: return-statement with a value, in function returning 'void'|
main.cpp||In function ‘void GerirHorario()’:|
main.cpp|108|error: ‘tolower’ was not declared in this scope|
main.cpp|133|error: return-statement with a value, in function returning 'void'|
main.cpp||In function ‘void IActividade()’:|
main.cpp|150|warning: comparison of unsigned expression < 0 is always false|
main.cpp|156|error: return-statement with a value, in function returning 'void'|
main.cpp||In function ‘void MActividade()’:|
main.cpp|179|error: return-statement with a value, in function returning 'void'|
main.cpp||In function ‘void RActividade()’:|
main.cpp|191|error: return-statement with a value, in function returning 'void'|
main.cpp||In function ‘void Visualizacao()’:|
main.cpp|224|error: ‘tolower’ was not declared in this scope|
main.cpp|247|error: return-statement with a value, in function returning 'void'|
main.cpp||In function ‘void Estatistica()’:|
main.cpp|308|error: ‘tolower’ was not declared in this scope|
main.cpp|331|error: return-statement with a value, in function returning 'void'|
||=== Build finished: 11 errors, 1 warnings ===|


You are missing at least one include file, ctype.h for tolower. Next you have several functions that have been defined not to return a value, that you are trying to return values from.

Next you are using fflush() on an input stream (stdin). This function is only defined, in the standard, to work on output streams. So using this on an input stream may or may not work as you expect it to work.

Next you are using a very dangerous function, gets(). You should replace this function with fgets() to avoid possible buffer overflows.

    if (strlen(act)>100 || strlen(act)<0)

Also strlen() returns an unsigned type, therefore it can not have a value less than 0.

There maybe other problems but these issues should be corrected now.

Jim

This post has been edited by jimblumberg: 21 December 2011 - 10:18 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1