#Need help 3:

99 messages · Page 1 of 1 (latest)

high girder
#

Im struggling with my program atm
Im trying to get this to work:

char *p = 'A';
char inputs[AR_SIZE];
char userChars[4];
double *userNums[3] = {0,0,0};

while (p != 'Z') {


    gets(inputs);

    p = inputs[0];

    if (!charEval(inputs)) {
        printf("%s", error1);
    }

    //Zero Terminates String
    inputs[strlen(inputs)] = "\0";

    //Checks to see if all fields are fufilled
    if (sscanf(inputs, "%3s %lf %lf %lf", userChars, &userNums, &userNums[1], &userNums[2]) != 4) {
        printf("%s", error1);

    }
    else {
        sscanf(inputs, "%3s %lf %lf %lf", userChars, userNums, &userNums[1], &userNums[2]);
        printf("%lf", userNums);
    }

-This program is intended to force users to follow a format string indicated in sscanf
-My issue is that it is not reading any numbers in on the else statment, it will always make each value zero
-I have no clue what im doing wrong, the eval string is "CCC Num Num Num", it should be working

idle cedarBOT
#

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.

idle cedarBOT
#

Im struggling with my program atm
Im trying to get this to work:

char* p = 'A';
char inputs[AR_SIZE];
char userChars[4];
double* userNums[3] = {0, 0, 0};

while (p != 'Z') {
  gets(inputs);

  p = inputs[0];

  if (!charEval(inputs)) {
    printf("%s", error1);
  }

  // Zero Terminates String
  inputs[strlen(inputs)] = "\0";

  // Checks to see if all fields are fufilled
  if (sscanf(inputs, "%3s %lf %lf %lf", userChars, &userNums, &userNums[1],
             &userNums[2]) != 4) {
    printf("%s", error1);

  } else {
    sscanf(inputs, "%3s %lf %lf %lf", userChars, userNums, &userNums[1],
           &userNums[2]);
    printf("%lf", userNums);
  }

-This program is intended to force users to follow a format string indicated in sscanf
-My issue is that it is not reading any numbers in on the else statment, it will always make each value zero
-I have no clue what im doing wrong, the eval string is "CCC Num Num Num", it should be working

Weeb
high girder
#

the getsline is used instead of scanf because i want to be able to avoid newlines

#

i have a hunch that because im not using scanf here, thats where the issue is occuring

#

because gets reads spaces as though they were characters

elfin sorrel
#

okay, first of all, char *p = 'a'; is not correct

high girder
#

ignore the poiner for userNums btw

#

it isn't?

elfin sorrel
#

you probably meant just char p = 'a'

high girder
#

im just trying to stop the program when z is inputted

#

oh

#

pointer variables confuse me

#

i have no idea why i would have to use them in most scenarios i see them being used

rapid dune
#

indirection will be useful when they are useful

elfin sorrel
#

in the first sscanf, &userNums should really be &userNums[0]

high girder
#

why?

#

dosent it assume your referring to the first address spot if none is provided

elfin sorrel
rapid dune
#

double* [] doesn't seem like what you want

high girder
#

yee

elfin sorrel
#

using & adds one

high girder
#

i was just testing stuff

#

wdym by adds one

#

like it adds an address spot>

#

*?

elfin sorrel
#

yeah, char* becomes char**

high girder
#

oh

elfin sorrel
#

sscanf just wants one level of indirection, you are giving it two

high girder
#

wdym by indirection

elfin sorrel
#

level of pointerness

high girder
#

oh

#

so how many addresses are being pointed too

elfin sorrel
#

with &userNums it creates a pointer pointing to another pointer that points to userNums

high girder
#

and without it it's just one pointer correct?

#

or zero

elfin sorrel
#

its just one

#
double* userNums[3] = {0, 0, 0};``` also yeah this should probably be `double userNums[3]`
high girder
#

whats the difference?

#

ik ones a pointer variable

#

and one isnt

#

but whats the primary difference between the two?

#

i mean dont variables point to addresses regardless

elfin sorrel
#

the first is an array of pointers to doubles, the second is an array of doubles

elfin sorrel
high girder
#

and pointers point to those addresses-right?

elfin sorrel
#

pointers are just variables that contain another address

#

yeah

high girder
#

so variables by themselves dont point to where they are located

#

right?

elfin sorrel
#

yeah

high girder
#

they need a variable that points to where they are located

#

that makes sense

#

okay so how would you rewrite this?

#

should i put &'s on everything

#

or just the userNums

elfin sorrel
#

just the userNums

high girder
#

so heres what i've got:

#

scanf(inputs, "%3s %lf %lf %lf", userChars, &userNums[0], &userNums[1], &userNums[2]);
printf("%lf", userNums);

#

its still not inputting the number into it

#

idk why

elfin sorrel
#

cant print arrays with printf

#

you need to iterate over it

high girder
#

not even array index spots?

elfin sorrel
high girder
#

okay so i've got it to work now

#

one last question

elfin sorrel
#

btw, inputs[strlen(inputs)] = '\0' will do nothing

high girder
#

it wont?

#

i was trying to zero terimnate it

elfin sorrel
#

strlen returns the index of the null terminator

high girder
#

oh

elfin sorrel
#

gets adds one

high girder
#

didnt know that

elfin sorrel
#

btw

#

dont use gets()

high girder
#

is there another way i can avoid newlines from being entered before all fields are provided?

elfin sorrel
#

its removed from newer standards, and is seriously frowned upon

high girder
#

didnt realize that

elfin sorrel
#

use fgets

high girder
#

fgets?

elfin sorrel
#

yes,

#
  fgets(inputs, AR_SIZE, stdin);
high girder
#

ooo okay

#

why is gets frowned upon?

#

i thought fgets was for files so i havent used it yet

elfin sorrel
#

if someone inputs a line longer than the buffer that you pass to gets, anything can happen

#

its UB

high girder
#

oh so you have to provide a massive buffer in order to avoid weird stuff from hapening

#

for example making a string have more index spots then what would normally be inputted

#

at least thats what im gathering

elfin sorrel
#

input might be different than you expected

#

no matter how big you make the buffer, it can still overflow with gets

high girder
#

oh danm

#

okay ill try that then thanks

#

thats it for now i think

#

thankyou

#

!solved