#Help pls

16 messages · Page 1 of 1 (latest)

warm hinge
#

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

define N 50

int main(){
//variaveis
char bdNomec[N];
char bdSenhac[N];
char bdNomel[N];
char bdSenhal[N];
char bdNome_armz[N];
char bdSenha_armz[N];
int opc;
int loop;
//loop
do{
//opcao login ou entrada
printf("Banco de dados\nSelecione a opcao de entrada\n(1)Login (2)Cadastro:");
scanf("%d", &opc);
//cadastro
if(opc == 1){

    printf("Introduza o seu nome: ");
    gets(bdNomec);
    fflush(stdin);
    printf("Introduza o sua senha: ");
    gets(bdSenhac);
    fflush(stdin);
    
    if(strcmp(bdNomec, bdNome_armz) == 0){
        printf("Nome ja existente"); continue;
    }
    else{
        printf("Cadastro realizado com sucesso !");
        strcpy(bdNome_armz, bdNomec);
        strcpy(bdSenha_armz, bdSenhac);
    }
    
}
//login
else if(opc == 2){
    printf("Nome: ");
    gets(bdNomel);
    fflush(stdin);
    printf("Senha: ");
    gets(bdSenhal);
    fflush(stdin);
    if(strcmp(bdNomel, bdNome_armz) == 0){
        printf("Nome confirmado !");
    }
    else if(strcmp(bdSenha_armz, bdNomel) == 0){
        printf("Senha confirmada !\nLogin realizado com sucesso ");
    }
    else{
        printf("Nome e senha incorrectos !");
    }
}
//saida de erro no caso de entrada de opcao incorrecta
else{
    printf("Opcao invalida !"); return 0;
}

//fim do loop
printf("Introduza (1) se pretende continuar e (2) para parar: ");
scanf("%d", &loop);
fflush(stdin);
}while(loop == 1);
}

cedar vaporBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

warm hinge
#

!howto ask

cedar vaporBOT
# warm hinge !howto ask
How to Ask a Programming Question

Anyone can ask a question in our programming channels. Following the guide Writing The Perfect Question is recommended.

What to Post

State your problem clearly and provide all necessary details:

  • the relevant portion of your code, or all of it
  • the expected output
  • the actual output (or the full error)
    :trophy: Gold Standard: Minimal Reproducible Example
Where to Post

Provide the relevant code in the message, and format it nicely with a code block*. If it's too much for one message, you can upload it:

  • Compiler Explorer for most C and C++ snippets
  • OnlineGDB for interaction, debugging
    :no_entry: Do not post screenshots, let alone photos of your screen!
cedar vaporBOT
#
#include <stdio.h>
#include <string.h>

#define N 50

int main() {
  // variaveis
  char bdNomec[N];
  char bdSenhac[N];
  char bdNomel[N];
  char bdSenhal[N];
  char bdNome_armz[N];
  char bdSenha_armz[N];
  int opc;
  int loop;
  // loop
  do {
    // opcao login ou entrada
    printf(
        "Banco de dados\nSelecione a opcao de entrada\n(1)Login (2)Cadastro:");
    scanf("%d", &opc);
    // cadastro
    if (opc == 1) {
      printf("Introduza o seu nome: ");
      gets(bdNomec);
      fflush(stdin);
      printf("Introduza o sua senha: ");
      gets(bdSenhac);
      fflush(stdin);

      if (strcmp(bdNomec, bdNome_armz) == 0) {
        printf("Nome ja existente");
        continue;
      } else {
        printf("Cadastro realizado com sucesso !");
        strcpy(bdNome_armz, bdNomec);
        strcpy(bdSenha_armz, bdSenhac);
      }

    }
    // login
    else if (opc == 2) {
      printf("Nome: ");
      gets(bdNomel);
      fflush(stdin);
      printf("Senha: ");
      gets(bdSenhal);
      fflush(stdin);
      if (strcmp(bdNomel, bdNome_armz) == 0) {
        printf("Nome confirmado !");
      } else if (strcmp(bdSenha_armz, bdNomel) == 0) {
        printf("Senha confirmada !\nLogin realizado com sucesso ");
      } else {
        printf("Nome e senha incorrectos !");
      }
    }
    // saida de erro no caso de entrada de opcao incorrecta
    else {
      printf("Opcao invalida !");
      return 0;
    }

    // fim do loop
    printf("Introduza (1) se pretende continuar e (2) para parar: ");
    scanf("%d", &loop);
    fflush(stdin);
  } while (loop == 1);
}
PaoComManteiga
zenith pumice
#
  • fflush(stdin) - is wrong (and undefined behavior by the standard)
  • gets is deprecated and shouldn't be used
  • *gets & scanf don't play well together. unless you know the quirks of scanf i suggest you won't use them both in the same program. use either scanf or fgets throught the entire program
warm hinge
#

oh ok ty

#

but wait one question how i clean the buffer ?

#

and why i shouldnt use "gets" ?

zenith pumice
cedar vaporBOT
#
Handling Whitespace in scanf

When the user presses space or enter in the console, this puts ' ' and '\n' whitespace characters into stdio respectively. When reading an int* with %d or float* with %f, scanf skips any leading whitespace. However, %c for char* extracts whitespace characters.

Example Problem

Say the user enters " 70 f", and we want to read 70 and f into int and char respectively:

int x; char c;
scanf("%d", &x); // OK,  x = 70
scanf("%c", &c); // BAD, c = ' '

Instead of reading f, we read a space, because %c does not skip leading whitespace. Solution:

scanf(" %c", &c);

The leading space before %c matches any whitespace of any length.

zenith pumice
warm hinge
#

oh thx

cedar vaporBOT
#

@warm hinge Has your question been resolved? If so, type !solved :)

warm hinge
#

!solved