proble with string

This is a discussion on proble with string within the C Programming forums, part of the General Programming Boards category; Hello everyone. I am having a trouble with storing first and last names into the string, the program stops after ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    92

    proble with string

    Hello everyone. I am having a trouble with storing first and last names into the string, the program stops after it asks to type the first name in.

    here is the function
    Code:
    struct student get_student_number_and_name(struct student X[MAX], int y)
    {
    	int i, valid, num;
    			
    	for(i=1; i<=y; ++i)
    	{
    	
    		do{
    			valid=0;
    			printf("\n\nEnter student %i - #  ", i);
    			scanf_s("%i", &num);
    
    				if(X[i].student_num==0)
    				{
    					X[i].student_num=num;
    
    					printf("First name:  ");
    					scanf_s("%s", X[i].first_name); //it stops here.
    					printf("Last name:   ");
    					scanf_s("%s", X[i].last_name);
    				}
    				else
    				{
    					printf("Student Number has been entered! Try agin!");	
    					valid=1;
    				}
    		}while(valid!=0);
    
    	
    	
    	}
    	return X[MAX];
    }
    I do no see why it stops. I will be very ppriciated if anyone can point where I am wrong.

    Thank you.

  2. #2
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    Code:
        (struct student X[MAX], int y)
    is equal to

    Code:
        (struct student *X, int y)
    write struct student here

    and
    Code:
        return X[MAX];
    change to
    Code:
        return X;
    X - will be pointer, so define the function as struct student *get_student_number_and_name(

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't need to return anything, since you aren't changing the pointer in any way. Since you're passing a pointer, you're already affecting the passed variables, and since their location doesn't in any way change, you don't need to return anything.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    by returning he can control the function (know how it is work), if he return a pointer - it's ok, if he return NULL - something wrong, so he can check the validity of the array or scanf result (there are many moments)

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    > I do no see why it stops. I will be very ppriciated if anyone can point where I am wrong.

    > scanf_s("%s", X[i].first_name); //it stops here.
    Well you're wrong in that you didn't read the manual for scanf_s before using it.
    scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l (CRT)
    %s needs a buffer size, and you're just passing garbage with the non-existant parameter.

    And Microsoft was wrong in believing that anyone who used the non-portable and proprietary _s versions of standard library functions would be able to write better code. Especially since they didn't bother to make the compiler diagnose printf/scanf screwups.

    scanf_s is no more safe than scanf, if you DON'T read the manual on how to use it properly.

    > for(i=1; i<=y; ++i)
    Lemme guess, you want to fill an array.
    Arrays start at 0, not 1.
    The typical array subscripting loop is
    for( i = 0 ; i < y ; ++i)
    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.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by c.user View Post
    by returning he can control the function (know how it is work), if he return a pointer - it's ok, if he return NULL - something wrong, so he can check the validity of the array or scanf result (there are many moments)
    Sure he could, but that is not what is being done. It's always returning the exact same thing no matter what.

    In which case, as I've already stated, there's no point in returning anything.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    Thanks, finaly it works .

    Quote Originally Posted by Salem View Post
    > I do no see why it stops. I will be very ppriciated if anyone can point where I am wrong.

    > scanf_s("%s", X[i].first_name); //it stops here.
    Well you're wrong in that you didn't read the manual for scanf_s before using it.
    scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l (CRT)
    %s needs a buffer size, and you're just passing garbage with the non-existant parameter.

    And Microsoft was wrong in believing that anyone who used the non-portable and proprietary _s versions of standard library functions would be able to write better code. Especially since they didn't bother to make the compiler diagnose printf/scanf screwups.

    scanf_s is no more safe than scanf, if you DON'T read the manual on how to use it properly.
    Salem I see that now, I used scanf_s because my compiler giving me a warnings all the time when I am using scanf, guess I just have to ignore it.

    > for(i=1; i<=y; ++i)
    Lemme guess, you want to fill an array.
    Arrays start at 0, not 1.
    The typical array subscripting loop is
    for( i = 0 ; i < y ; ++i)
    That's the way my teacher'd like us to do, and I do not agree with it also, as on my thought everything has to be done in a right way from a very beginning of our learning process, but for now I have to follow her rule.

    Thanks for all your replies.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by nynicue View Post
    Salem I see that now, I used scanf_s because my compiler giving me a warnings all the time when I am using scanf, guess I just have to ignore it.
    If your compiler is giving warnings, the chances are you're doing something wrong.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    > I used scanf_s because my compiler giving me a warnings all the time when I am using scanf
    More manual reading perhaps?
    Security Enhancements in the CRT

    #define _CRT_SECURE_NO_WARNINGS
    right at the start of your program, or better yet in the project settings when you're into multi-file projects.

    > That's the way my teacher'd like us to do
    Are you saying that your "teacher" doesn't actually know that arrays begin at zero?
    If you are, then this is a new level of bone-headedness.
    Do they also spout void main nonsense as well.
    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.

  10. #10
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    quzah,
    Code:
        scanf_s("%s", X[i].first_name);
    if he will repair it to right form, what he will get if the function will not save the entered data ?
    (it shouldn't return void)
    maybe int or pointer to start array, but not void, because if he will lose data he will must check all array for this state (lost or not) after every call of the function

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Gah! Why are you still arguing this?
    Code:
    #include<stdio.h>
    void foo( int bar[], size_t baz )
    {
        size_t x = 0;
        for( x = 0; x < baz; x++ )
        {
            printf( "Enter a number: " );
            fflush( stdout );
            scanf( "%d", &bar[ x ] );
        }
    }
    
    int main( void )
    {
        int array[ 5 ] = {0};
        size_t x;
    
        foo( array, sizeof array / sizeof( array[0] ) );
    
        printf( "OMG full array!\n" );
        for( x = 0; x < sizeof array / sizeof( array[ 0 ]); x++ )
        {
            printf( "%d\n", array[ x ] );
        }
    
        return 0;
    }
    It's going to save always, because he's editing the array directly. I don't need to return anything, and neither does he.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    92

    Smile

    Quote Originally Posted by Salem View Post
    > I used scanf_s because my compiler giving me a warnings all the time when I am using scanf
    More manual reading perhaps?
    Security Enhancements in the CRT
    Your sarcasm drove me to read that( mannual ) and think now I do understand those kind of things better , thank you.

  13. #13
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    quzah,
    Code:
            printf( "Enter a number: " );
            fflush( stdout );
            scanf( "%d", &bar[ x ] );
    no, push ctrl+z at first element (or send a file to the input of the program with three numbers by command "type file.txt | prog.exe")
    you will get output like 1 2 3 at new line every
    how do you know how many numbers in the file ?
    how do you know there are 1 2 3 and not 1 2 3 0 0 ?

    if I would write this function, I would have not only a check for scanf() == 1 (and returning EOF if it is not save the data), but else function would return number of writed elements to the array (and I would know how many elements it has saved)

    in your example you can't know what was there (input with zeroes or incorrect input)

    why you use fflush( stdout ); I don't know, what wil be without it ? (it is only flushes the buffer, when stdout use buferization of characters before output)

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by c.user View Post
    quzah,

    no, push ctrl+z at first element (or send a file to the input of the program with three numbers by command "type file.txt | prog.exe")
    you will get output like 1 2 3 at new line every
    how do you know how many numbers in the file ?
    how do you know there are 1 2 3 and not 1 2 3 0 0 ?
    None of this has anything to do with the issue you're trying to prove. Sending EOF to the function is completely irrelevant with regards to the function's return value. Accessing via pointer means you don't have to return it. It's not a hard concept. They're not repositioning the pointer, they're not reassigning it to something else. They're changing the value of what it's pointing at. Therefore, your absurd insistence that they must return a value is wrong. They don't gain anything by returning anything, and as I've said, they were returning the same thing every time anyway, so your point is moot.

    You're wrong, I'm right, that's all there is to it. You trying to make the issue into something completely different ("well what if they break your error checking?!") isn't going to make you any more right.

    You're wrong. Returning something isn't going to make his function any better than if he doesn't. Furthermore, sending EOF to break his error checking still isn't going to make returning something any different. It doesn't matter. Nothing you can possibly do is going to change the effect of returning or not returning something in the scope of this argument.

    You're wrong. I'm not.
    Quote Originally Posted by c.user View Post
    in your example you can't know what was there (input with zeroes or incorrect input)
    My example wasn't to have a flawless input method. It was to illustrate the point that the array is going to be changed by my function (or not, I could write it to not change it), and that returning the array would be completely irrelevant.

    The only way it could possibly matter would be if you were cloning the array, returning a new copy of it, and then comparing the before-array to the after-array outside of the function. This discussion isn't about doing that, so, as I've said, you're wrong. Stop trying to convolute the issue into something it's not.

    You're wrong. I'm not.
    Quote Originally Posted by c.user View Post
    why you use fflush( stdout ); I don't know, what wil be without it ? (it is only flushes the buffer, when stdout use buferization of characters before output)
    There is no guarantee that you will ever see the output of printf if you aren't outputting a newline. Therefore, you flush the buffer to ensure it's being seen.

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    Quote Originally Posted by quzah
    There is no guarantee that you will ever see the output of printf if you aren't outputting a newline.
    newline is a character, where have you known that ?

    Quote Originally Posted by quzah
    It was to illustrate the point that the array is going to be changed by my function (or not, I could write it to not change it), and that returning the array would be completely irrelevant.
    If I will input letters your array will not change and function will not return info about such situation (and your loop after it will print array)
    I would write a function which when get a letter return and loop wouldn't run or it run for only entered data (without printing of empty values)

    Quote Originally Posted by quzah
    Furthermore, sending EOF to break his error checking still isn't going to make returning something any different. It doesn't matter.
    it has very much matter if you have over 10 functions and more (because you can control every step of your program), and if you don't know what happen at every step, you may seed the bug which will be hided for months

    Quote Originally Posted by quzah
    Sending EOF to the function is completely irrelevant with regards to the function's return value.
    Sending EOF to scanf means only when the input is end, your function continues save values, but it must return an info about this situation (with continue or stop of entering)

    but your loop has such header
    Code:
        for( x = 0; x < baz; x++ )

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 03:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

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