#C beginner help

98 messages · Page 1 of 1 (latest)

abstract kite
#

My code:

    int numLines;
    char data[200] = {};
    char *Data = data;
    printf("Enter the number of lines: ");
    scanf("%d", &numLines);

    printf("Enter the integers:\n");
    getchar();
    for (int i = 0; i < numLines; i++) {
        fgets(data, 20, stdin);

        int numbers = strtol(Data, &Data, 10);

        printf("Line %d: has values of: %d ", i + 1, numbers);

}
    return 0;

}

what i want: if I input 4 lines and then when it says enter the integers I want to copy and paste something like this (4 lines of random numbers)

1 2 3 4
25346436 346433 6346436
4363433 346 346 346 ```


I want it to print: Line1 contains values of: 1 2 3 4, line 2 contains values of 1 2 3 4 etc etc

please let me know I am new trying to learn
lime archBOT
#

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 run !howto ask.

real crown
#

what's different between that and what you have

abstract kite
#

uh it doesn't work

#

xd

#

when inputting this

#

"1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
"

#

I get

real crown
#

what's different between that and what you want

abstract kite
#

it should do

#

from this

#

"1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

#

Line: 1 1 2 3 4

#

Line 2 5 6 7 8

#

Line 3

#

9 10 11 12

#

but u can see what it does

#

its weird

real crown
abstract kite
real crown
#

use your words

abstract kite
#

it prints 1 5 6 7 8

#

which is not even what i put

real crown
#

what do you want it to do?

abstract kite
#

print the correct numbers

real crown
#

what does "correct" mean?

abstract kite
#

the actual nuumbers

#

that i input

real crown
#

you're only strtoling one number per line

#

it's doing what you want

abstract kite
#

well obviously i didn't mean to do that

#

thats what im asking

#

how do i make it strtol the whole line

real crown
#

you need to call strtol multiple times, one per number

#

that's what the endptr argument for strtol is for - it'll point to the end of what it just parsed, so you can start there for the next number

#

it's reading the 1 at the beginning of the first line you pasted in, then printing Line 1: has values of 1, and then the 5 6 7 8 from the second line of what you pasted gets in after that

abstract kite
#

so what should i do

#

to make it do one line at a time

real crown
#

it is

abstract kite
#

but the first number

#

in the print

real crown
#

yes

abstract kite
#

is always wrong

real crown
#

no, it's correct

abstract kite
#

by wrong i meant not what i want

real crown
#

then what do you want?

abstract kite
#

bro

real crown
#

we aren't mindreaders

abstract kite
#

i said it like 10 times

#

wdym u aren't a mind reader

#

if i input "9 10 11 12
13 14 15 " etc

#

it should print

#

line 1: 9 10 11 12

#

line2: 13 14 15

real crown
#

then you should keep parsing numbers until you reach the end of the line

abstract kite
#

okay how do i do that

real crown
#

by calling strtol multiple times

abstract kite
#

so make a for loop inside the loop?

real crown
#

until endptr is at the end of the line

abstract kite
#

how do i do that

real crown
#

a loop

#

until endptr reaches the end of the line

abstract kite
#

by end pointer you are referring to &Data

#

in this case

#

right

real crown
#

yes, the argument to strtol named endptr

abstract kite
#

for(int x =0; i< ?

#

what would i be less than

#

not numlines

#

wouldn't it be the total amount of numbers per line

abstract kite
#

yea

#

im asking how

#

im new

real crown
#

and I'm not going to hand you everything on a silver platter

abstract kite
#

so help me understand

#

because you are repeating urself

real crown
#

how do you think you'd compare endptr to the end of the line

abstract kite
#

idek what ur asking me

real crown
#

do you know what the endptr value is for?

abstract kite
#

yes

#

for the char

#

data

#

&Data

#

is the second pointer

real crown
#

but do you know what it's actually for

abstract kite
#

all that i know

#

is whhen using strtol

#

it takes the first pointer, second pointer, and base

lime archBOT
#

strtol, strtoll, strtoq - convert a string to a long integer

Synopsis
#include <stdlib.h>

long strtol(const char *restrict nptr,
            char **restrict endptr, int base);
long long strtoll(const char *restrict nptr,
            char **restrict endptr, int base);

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

strtoll():
    _ISOC99_SOURCE
        || /* Glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE

real crown
#

I reccomend reading the description of the function, specifically the part that starts with "if endptr is not NULL..."

abstract kite
#

well then ig i dont fully understand how strtol works

#

because i still dont know

#

i thought it just takes a string and converts it into integers

real crown
#

the char* you pass a reference to as endptr gets set as a pointer to the first char in the input string that couldn't be converted, aka the first character after the number it parsed

#

if that first character is the end of the line, then you're done parsing numbers for that line

tawdry tendon
# abstract kite i thought it just takes a string and converts it into integers

It just goes through the string, ignoring all leading whitespace, and then "grabs" digits until any other character, including whitespaces, is encountered.
It then returns you the integer value of the number these digits represent.

If you decided to pass endptr, then strtol will set this pointer to point to the first character after the digits of the number it just converted.