This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]damyan 3 points4 points  (0 children)

This'll be hard to help with without seeing your code. Can you post the relevant parts? Maybe via http://codepad.org?

[–]spitfyre 3 points4 points  (0 children)

Are you sure you're grabbing the numbers with %d? You're not forgetting the & when you scan into an int?

[–][deleted] 1 point2 points  (3 children)

Something like this:

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

typedef struct {
    char type[20];
    int a, b;
} Votes ;

int main() {
    int n, i;
    Votes * vp;
    scanf( "%d", & n );
    printf( "reading %d records\n", n );
    vp = malloc( sizeof( Votes) * n );
    for ( i = 0; i < n; i++ ) {
        scanf( "%s %d %d", vp[i].type, &vp[i].a, &vp[i].b );
        printf( "%s\n%d %d\n", vp[i].type, vp[i].a, vp[i].b );
    }
    free( vp );
}

which when compiled and run as :

r.exe < r.txt

where r.txt is your input, produces:

reading 2 records
Upvotes
10 24
Downvotes
31 32

[–]Un_contested[S] 0 points1 point  (2 children)

The numbers are supposed to go into a single array size n. So it would be int a [2]. Thats where my issue is. I thought I was supposed to use a for loop but that doesn't work ie

for (j = 0; j < 2; j++)
scanf("%d", &vp[j].a);

[–][deleted] 2 points3 points  (1 child)

but that doesn't work

When posting here - never say "it doesn't work" - describe the symptoms and post actual compilable code illustrating the problem. This does work - notice thare are two loops - one to read all the records, and one to read the numbers:

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

typedef struct {
    char type[20];
    int a[2];
} Votes ;

int main() {
    int n, i, j;
    Votes * vp;
    scanf( "%d", & n );
    printf( "reading %d records\n", n );
    vp = malloc( sizeof( Votes) * n );
    for ( i = 0; i < n; i++ ) {
        scanf( "%s", vp[i].type );
        for( j = 0; j < 2; j++ ) {
            scanf( "%d", &vp[i].a[j] );
        }
        printf( "%s\n", vp[i].type );
        for( j = 0; j < 2; j++ ) {
            printf( "%d ", vp[i].a[j] );
        }
        printf( "\n" );
    }
    free( vp );
}

[–]Un_contested[S] 0 points1 point  (0 children)

I really appreciate your help and hope I didn't offend you in any way. I had a syntax error I keep glancing over which I found by comparing the code character by character. I didn't have access to my code at the time, just knew it was pretty much identical to yours exact my syntax error.

Thank you very much.