read string

This is a discussion on read string within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include "listas.h" no *username(no *ptr); int password(no *ptr); int main(void) { no *fst ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Location
    Felgueiras Municipality, Portugal
    Posts
    1

    read string

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "listas.h"
    
    
    no *username(no *ptr);
    int password(no *ptr);
    
    
    
    
    
    
    
    
    int main(void) {
    
    
        no *fst = NULL;
    
    
        no *user1 = malloc(sizeof (no));
        user1->prox = NULL;
        user1->user = malloc(sizeof (user));
    
    
        user1->user->numec = 8080163;
        user1->user->pass = "password";
        user1->user->nome = "Fabio Teixeira";
        user1->user->tipouser = 3;
        fst = user1;
    
    
    
    
        no *user2 = malloc(sizeof (no));
        user2->prox = NULL;
        user2->user = malloc(sizeof (user));
    
    
        user2->user->numec = 8010672;
        user2->user->pass = "password";
        user2->user->nome = "Antonio Rocha";
        user2->user->tipouser = 3;
        user1->prox = user2;
    
    
    
    
        no *user3 = malloc(sizeof (no));
        user3->prox = NULL;
        user3->user = malloc(sizeof (user));
    
    
        user3->user->numec = 8020326;
        user3->user->pass = "password";
        user3->user->nome = "Tiago Gomes";
        user3->user->tipouser = 3;
        user2->prox = user3;
    
    
    
    
        no *user4 = malloc(sizeof (no));
        user4->prox = NULL;
        user4->user = malloc(sizeof (user));
    
    
        user4->user->numec = 8030934;
        user4->user->pass = "password";
        user4->user->nome = "Barbara Goncalves";
        user4->user->tipouser = 3;
        user3->prox = user4;
    
    
    
    
        no *user5 = malloc(sizeof (no));
        user5->prox = NULL;
        user5->user = malloc(sizeof (user));
    
    
        user5->user->numec = 8040397;
        user5->user->pass = "password";
        user5->user->nome = "Rosanette Cruz";
        user5->user->tipouser = 3;
        user4->prox = user5;
    
    
    
    
        no *user6 = malloc(sizeof (no));
        user6->prox = NULL;
        user6->user = malloc(sizeof (user));
    
    
        user6->user->numec = 8051234;
        user6->user->pass = "password";
        user6->user->nome = "Arnaldo Freitas";
        user6->user->tipouser = 3;
        user5->prox = user6;
    
    
    
    
        no *user7 = malloc(sizeof (no));
        user7->prox = NULL;
        user7->user = malloc(sizeof (user));
    
    
        user7->user->numec = 8060672;
        user7->user->pass = "password";
        user7->user->nome = "Diogo Neves";
        user7->user->tipouser = 1;
        user6->prox = user7;
    
    
    
    
        no *user8 = malloc(sizeof (no));
        user8->prox = NULL;
        user8->user = malloc(sizeof (user));
    
    
        user8->user->numec = 8070427;
        user8->user->pass = "password";
        user8->user->nome = "Marcio Coelho";
        user8->user->tipouser = 3;
        user7->prox = user8;
    
    
    
    
        no *user9 = malloc(sizeof (no));
        user9->prox = NULL;
        user9->user = malloc(sizeof (user));
    
    
        user9->user->numec = 8080007;
        user9->user->pass = "password";
        user9->user->nome = "Luis Branco";
        user9->user->tipouser = 3;
        user8->prox = user9;
    
    
    
    
        no *user10 = malloc(sizeof (no));
        user10->prox = NULL;
        user10->user = malloc(sizeof (user));
    
    
        user10->user->numec = 8090023;
        user10->user->pass = "password";
        user10->user->nome = "Pedro Lobo";
        user10->user->tipouser = 3;
        user9->prox = user10;
    
    
    
    
        no *user11 = malloc(sizeof (no));
        user11->prox = NULL;
        user11->user = malloc(sizeof (user));
    
    
        user11->user->numec = 8090713;
        user11->user->pass = "password";
        user11->user->nome = "Paulo Lopes";
        user11->user->tipouser = 3;
        user10->prox = user11;
    
    
    
    
        no *user12 = malloc(sizeof (no));
        user12->prox = NULL;
        user12->user = malloc(sizeof (user));
    
    
        user12->user->numec = 8010212;
        user12->user->pass = "password";
        user12->user->nome = "Elsa Lopes";
        user12->user->tipouser = 2;
        user11->prox = user12;
    
    
    
    
        no *user13 = malloc(sizeof (no));
        user13->prox = NULL;
        user13->user = malloc(sizeof (user));
    
    
        user13->user->numec = 8020001;
        user13->user->pass = "password";
        user13->user->nome = "Helder Pinto";
        user13->user->tipouser = 2;
        user12->prox = user13;
    
    
    
    
        no *user14 = malloc(sizeof (no));
        user14->prox = NULL;
        user14->user = malloc(sizeof (user));
    
    
        user14->user->numec = 8030028;
        user14->user->pass = "password";
        user14->user->nome = "Ricardo Santos";
        user14->user->tipouser = 2;
        user13->prox = user14;
    
    
    
    
        password(username(fst));
        
        
    }
    
    
    
    
    no *username(no *ptr)
    {
        int val;
        
            printf("Introduza o user: ");
            scanf("%d", &val);
        
                    while(ptr != NULL){
                            if(val==ptr->user->numec){
        
                                    
                                    return ptr;
               
                                                     } 
           ptr = ptr->prox;
                                      }
    }
    
    
    int password(no *ptr)
    {
        no* aux = malloc(sizeof(no));
        aux->prox = NULL;
        aux->user = malloc(sizeof(user));
        
        printf("Introduza a sua pass: ");
        scanf("%s", aux->user->pass);
        printf("A pass e: %s", aux->user->pass);
    }
    I have the struct of the lists in a header




    build netbeans:


    "/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
    "/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/tp
    mkdir -p build/Debug/GNU-MacOSX
    rm -f build/Debug/GNU-MacOSX/main.o.d
    gcc -c -g -MMD -MP -MF build/Debug/GNU-MacOSX/main.o.d -o build/Debug/GNU-MacOSX/main.o main.c
    mkdir -p dist/Debug/GNU-MacOSX
    gcc -o dist/Debug/GNU-MacOSX/tp build/Debug/GNU-MacOSX/main.o


    BUILD SUCCESSFUL (total time: 271ms)











    Cans someone tell me why it isn't reading the string I input?
    btw, I'm portuguese .
    cumps

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    Where did you allocate the space to store the password?

    You do realize without the data-types you code is not possible to verify that it is correct.

    Tim S.
    Last edited by stahta01; 01-07-2012 at 09:51 PM.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    Code:
        aux->user = malloc(sizeof(user));
         
        printf("Introduza a sua pass: ");
        scanf("%s", aux->user->pass);
    Where do you think this is pointing?

    In main(), you initialise pass with things like this
    user1->user->pass = "password";

    But here, you also need to initialise pass with some memory you can read a string into.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I read in this as a string?
    By scarlet00014 in forum C Programming
    Replies: 5
    Last Post: 09-14-2008, 11:39 AM
  2. How do I read in this as a string?
    By scarlet00014 in forum C Programming
    Replies: 5
    Last Post: 09-14-2008, 03:32 AM
  3. Read from string
    By rabbit in forum C++ Programming
    Replies: 6
    Last Post: 02-19-2006, 09:09 AM
  4. Using cin to read a name into 1 string
    By Slavakion in forum C++ Programming
    Replies: 18
    Last Post: 03-06-2004, 04:49 PM
  5. Replies: 1
    Last Post: 05-30-2003, 02:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21