C Beginner Help Plz :D
Hi again,
I seem to be having a bit of trouble with some of the code I wrote. I am only new at C and this code has arrays and pointers to structures. I know what what part is wrong in it and it is the part where the user is supose to enter his/her name. I would like to store their name in an array, the reason behind the 2D array I have used is because later I will be adding more names; but I do not know what element to store the name in or even how to go about it. This code I have written is only for practic so if it doesn't make any sense to do some of the things I have done that's ok, it's just the run-time error I would like to remove, please :D Here is my code: Code:
#include<stdio.h> |
1: your code has typos, fix them first (posted code should compile)
2: you are using empP without initializing it 3: compile with -Wall and fix anything gcc complains of |
Quote:
|
Expanding on jlliagre's post, the reason why your program isn't working (I would guess that you're getting a segmentation fault / access violation) is that you haven't initialised empP, which is a pointer to a struct of type "employee".
You either need to change your declaration of empP so that it refers directly to an "employee" structure: Code:
struct employee empP; Code:
struct employee *empP = NULL; ~sind |
Hi again,
I wanted to use some code that basically did this: Code:
::EDIT:: I have now added this to see if it works but it still doesn't: Code:
if (isalpha(*hrs_worked_tdy) || ispunct(*hrs_worked_tdy)) Here is my entire code: Code:
#include<stdlib.h> |
Please indicate what the problem is. I guess it does not compile without errors / warnings.
2 things I see: check the man page for the is.... family (isalpha etc); these functions take a char and not a number your function get_user_input takes a pointer to hrs_worked_tdy; next, in the call to scanf you pass the address of this pointer (so pointer to pointer) |
It compiles and runs. All I want it to do now is check if the user entered a "grammatical character" or a "number". This is why it's like this:
Code:
if (isalpha(*hrs_worked_tdy) && ispunct(*hrs_worked_tdy)) |
If you want to check for a number, use isdigit:
Code:
if( isdigit(*hrs_worked_tdy) ) Code:
char *ptr; |
I tried using the "isdigit" function like this below:
Code:
#include<stdlib.h> Code:
if(isdigit(*hrs_wrked_tdy) == 1) |
Well your usage there is saying "iif the first character is a digit, then complain". I don't think that is what you meant.
You want to see if the whole number is valid (all digits or period). Your if should have a ! symbol to dinicate not digiti. |
If I compile using g++ -Wall testje.c -o testje,
it tells me: Code:
testje.c:17: warning: passing 'float' for argument passing 1 of 'int isalpha(int)' As stated in my earlier post, is.... functions check a character and not a float |
The problem is clear to me now. You are reading in a float and trying to see if it valid? There is no way because you just relied on scanf to do so.
However, if you read the value in as a string, validate the characters and finally convert it to a float or double, then you can do validation of non-number values. Most likely, scanf will work like strtod where invalid characters are ignored and considered the end of the value so entering "ha" = 0. |
Quote:
|
I just like using doubles (better precision) instead of float personally, but you can change it to float just as well.
Code:
#include <stdlib.h> |
Quote:
change it to : file:///home/user/ :]] J/King |
All times are GMT -5. The time now is 10:20 AM. |