Simple question (for you not for me!)

This is a discussion on Simple question (for you not for me!) within the C Programming forums, part of the General Programming Boards category; l == "b" ?? Would help if we could see the whole code....

  1. #16
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    l == "b" ??

    Would help if we could see the whole code.

  2. #17
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    520
    Quote Originally Posted by antonis
    Code:
            else if (l=='b'){
                    /*Dialog to insert a new line before current line.*/
                    printf("Insert the new line:\nTEEDI>");
                    fgets(newline, MAXLINE,stdin);
                    InsertBefore(buffer, newline);
            }
    here there is the code in my programm. I don't get a prompt to write the line! Why???? How can I fix it?
    your printf has no '\n', so the output buffer is not being flushed to the screen because it wants to wait until more calls have been made as to be more efficient....

    put a fflush(stdout) after your "printf(" call.

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'll help you all here, because you all need it. Let's go post by post, it'll be faster that way.

    Code:
    #include <stdio.h>
    main()
    {
    char line;
    scanf("%s",line);
    
    /*NOW USE THE TEXT LINE WHILE CALLING A FUNCTION*/
    }
    1) In this first post, line is a single character.
    2) If it were an array, the call to scanf would be correct. But it's not.
    3) If it were "%c", the call to scanf would be correct for reading a single character. But it's not.

    Second post, this wouldn't ever be correct:
    Quote Originally Posted by Axel
    Code:
    scanf("%s",line);
    change line to &line
    1) If line were an array, it would be wrong because you don't use the address-of operator here with arrays.
    2) If it were a single character, it'd be wrong because the format specificer is for reading strings.

    Same song, third verse, couldn't get better so it's gotta get worse:
    Quote Originally Posted by antonis
    Code:
    #include <stdio.h>
    main()
    {
    char *line;
    scanf("%s",&line);
    
    /*NOW USE THE TEXT LINE WHILE CALLING A FUNCTION*/
    printf("%s",&line);
    Here,
    but I still get just the first word of the text.
    1) line now is a pointer, but it doesn't point to anything.
    2) scanf wouldn't use the address-of operator here, because you've already got a poitner, so you don't need the &.
    3) printf never uses the address-of unless specifically trying to print an address.

    Quote Originally Posted by antonis
    I use the assigment operator at printf because else I get segmentation.
    You're getting a segfault because you don't have anything allocated for your pointer. It's pure luck that it works when you add the printf. There's no reason it should.

    "I see bad code."
    "In your dreams?"
    *shakes head no*
    "How often do you see them?"
    "All the time."
    Quote Originally Posted by Quantum1024
    If you want the whole line you can use gets()
    Code:
    #include <stdio.h>
    int main()
    {
    	char line[256];
    	gets(line);
    
    	/*NOW USE THE TEXT LINE WHILE CALLING A FUNCTION*/
    	printf("%s",line);
    	return 0;
    }
    There's a FAQ on why gets is so bad. Then again, there's a FAQ on reading a line from the user.

    From the "I knew I should have taken that left turn at Albequerque" department, comes this guy:
    Quote Originally Posted by durban
    try using std::cin.getline(line, 256); That should get you the whole line instead of just the first word. The reason for this in scanf is because it registers a space as an EOF delimiter or '\0', it's mainly made for parsing files. std::cin.getline() will get you the text until the enter key is pressed.
    Nice try, wrong forum. Say "Hi" to Bugs Bunny next time you see him.

    Next we get one more of the usual suspects:
    Quote Originally Posted by antonis
    Code:
            else if (l=='b'){
                    /*Dialog to insert a new line before current line.*/
                    printf("Insert the new line:\nTEEDI>");
                    fgets(newline, MAXLINE,stdin);
                    InsertBefore(buffer, newline);
            }
    here there is the code in my programm. I don't get a prompt to write the line! Why???? How can I fix it?
    This one is our friend "Hey, here's a completely unrelated piece of code, which is incomplete and impossible to troubleshoot!"

    This guy recognizes our familiar suspect, but is waylayed by either the C++ guy, or the absence of strcmp:
    Quote Originally Posted by Barnzey
    l == "b" ??

    Would help if we could see the whole code.
    The check would have been correct, with the single quotes, if it were a single character. This above code however, will never be correct, because you can't test string equality this way in C. (Well, I can't say never, because it's entirely possible that you wanted to know if this string literal shared the same memory position as the string l. But I doubt it.)


    "That about wraps this show up, folks. Tune in tomorrow, or heck, even later today, for more fun commentary by yours truely, on 'C Code Catastrophes!'"


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

  4. #19
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by TactX
    2. Problem: You will not get more than the first word of your input when using scanf().
    Code:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
            char buf[201];
            printf("Enter a line: ");
            fflush(stdout);
            scanf("%200[^\n]", buf);
            printf("Entered: \"%s\"\n", buf);
            return 0;
    }
    Output:
    Code:
    Enter a line: one two three
    Entered: "one two three"

  5. #20
    C_newbie
    Join Date
    Oct 2005
    Location
    Greece
    Posts
    32
    @cwr: This code is OK but here:
    Code:
       else if (l=='b'){
                    /*Dialog to insert a new line before current line.*/
                    printf("Insert the new line:\nTEEDI>");
                    fgets(newline, MAXLINE,stdin);
                    InsertBefore(buffer, newline);
            }
    I get 2 prompts again and can not insert any code....
    -----------------------------------------------------------------------
    @Quzah:
    I'll help you all here, because you all need it. Let's go post by post, it'll be faster that way.
    I dont think you help a lot you just laugh about my problem and the other persons posts, and you tell us why we don't know anything and why this and the other is false, but you don't tell us the right thing. Maybe you don't know either, who knows. Maybe the others that have answered need help too as you say but at least they try to help me and do not laugh about me.

    OK about the first 2 posts I suppose you're right, but I have already corrected
    this codes with the help of other forum members, even if they are not as good as you.
    Quote:
    Originally Posted by antonis
    Code:

    #include <stdio.h>
    main()
    {
    char *line;
    scanf("%s",&line);

    /*NOW USE THE TEXT LINE WHILE CALLING A FUNCTION*/
    printf("%s",&line);


    Here,
    but I still get just the first word of the text.

    1) line now is a pointer, but it doesn't point to anything.
    2) scanf wouldn't use the address-of operator here, because you've already got a poitner, so you don't need the &.
    3) printf never uses the address-of unless specifically trying to print an address.

    Quote:
    Originally Posted by antonis
    I use the assigment operator at printf because else I get segmentation.
    You're getting a segfault because you don't have anything allocated for your pointer. It's pure luck that it works when you add the printf. There's no reason it should.
    The segfault goes away when I use the ASSIGNMENT OPERATOR (&) in printf f not when using printf. But its more funny writing about LUCK! Isn't it quzah?
    "I see bad code."
    "In your dreams?"
    *shakes head no*
    "How often do you see them?"
    "All the time."
    I hate irony, so I won't answer here.
    Quote:
    Originally Posted by Quantum1024
    If you want the whole line you can use gets()
    Code:

    #include <stdio.h>
    int main()
    {
    char line[256];
    gets(line);

    /*NOW USE THE TEXT LINE WHILE CALLING A FUNCTION*/
    printf("%s",line);
    return 0;
    }


    There's a FAQ on why gets is so bad. Then again, there's a FAQ on reading a line from the user.

    From the "I knew I should have taken that left turn at Albequerque" department, comes this guy:
    Here there is Irony again- which is normally used when we don't have arguments about our opinion.
    Quote:
    Originally Posted by durban
    try using std::cin.getline(line, 256); That should get you the whole line instead of just the first word. The reason for this in scanf is because it registers a space as an EOF delimiter or '\0', it's mainly made for parsing files. std::cin.getline() will get you the text until the enter key is pressed.
    Nice try, wrong forum. Say "Hi" to Bugs Bunny next time you see him.
    One more time, ironical, quzah is 1000 levels above all of us.
    [quote]
    Next we get one more of the usual suspects:
    Quote:
    Originally Posted by antonis
    Code:

    else if (l=='b'){
    /*Dialog to insert a new line before current line.*/
    printf("Insert the new line:\nTEEDI>");
    fgets(newline, MAXLINE,stdin);
    InsertBefore(buffer, newline);
    }


    here there is the code in my programm. I don't get a prompt to write the line! Why???? How can I fix it?
    This one is our friend "Hey, here's a completely unrelated piece of code, which is incomplete and impossible to troubleshoot!"[/quota]
    I dont think its unrelated, here the problem started and I decided to make a smaller programm to read the line, whith your (plural of course) help. When it finally worked I tryed to put the code in my previous programm but there I had a problem. why incomplete I have the fgets and then the programm goes directly to InserBefore whithout waiting for me to insert the line. I think it's all there what you need, but if you like smthing more you can ask for it. If you want me to copy in every post 350lines of code for a problem that needs 5 lines then I can do it, but I think its easier so.
    This guy recognizes our familiar suspect, but is waylayed by either the C++ guy, or the absence of strcmp:
    Quote:
    Originally Posted by Barnzey
    l == "b" ??

    Would help if we could see the whole code.
    The check would have been correct, with the single quotes, if it were a single character. This above code however, will never be correct, because you can't test string equality this way in C. (Well, I can't say never, because it's entirely possible that you wanted to know if this string literal shared the same memory position as the string l. But I doubt it.)
    In my code there are single quotes. But its again not funny to see the post of the code but to see a reply with " " which is a question why "b" and of course if you read Barnzeys whole post you will understand (?) that he means 'b'. he doesnot add some code, but sees from my if that there must be more post.

    "That about wraps this show up, folks. Tune in tomorrow, or heck, even later today, for more fun commentary by yours truely, on 'C Code Catastrophes!'"


    Quzah.
    No thanks I think this was enough.
    Now if you want to answer me, dont do it on this post I wont reply. We are out of topic, and it has no sense. Just pm me.
    Antonis
    AC/DC: Highway to Hell!
    No speed limit, no stop signs, nobody 's gonna slow me down!

  6. #21
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by antonis
    The segfault goes away when I use the ASSIGNMENT OPERATOR (&) in printf f not when using printf. But its more funny writing about LUCK! Isn't it quzah?
    Well, quzah's comment on it being luck that it works was just 100% correct and informative. I'll try to provide an informative explanation, since you seem to be still confused.

    The unary & operator means "address of", not "assignment" so when you do:
    Code:
    char *line;
    scanf("%s", &line);
    printf("%s", &line);
    Or some other odd combination, you are doing crazy things on both counts.

    The scanf line, will write the result of the %s read into the space where the line pointer resides in memory, but the line pointer is like to only take up about 4 bytes of memory, so beyond that, you are just trashing other bits of your stack. Other declared variables nearby, perhaps, the return address of the function, maybe, all sorts of goodies.

    The printf line then coincidentally "works" because you are then telling printf to look for a string starting at the address of the line pointer, so it just merrily reads whatever scanf put there.

    Slightly more correct code would be:
    Code:
    char *line;
    scanf("%s", line);
    printf("%s", line);
    Now we're cooking, the format of the scanf argument and printf argument are now correct, because line is a pointer to char, so scanf will now write the string where line points to.

    But there's still an enormous problem. We've declared char *line; but haven't initialised it, so line is pointing to some "random" address. You have to assign line to some malloc'd space, or an array:
    Code:
    char *line;
    line = malloc(500);
    if (line == NULL)
    {
        /* oh no */
        exit(1);
    }
    scanf("%s", line);
    printf("%s", line);
    Now that's more like it, line now points to a freshly allocated 500 byte chunk of memory, so you can now tell scanf to go put stuff there.

    Finally, there's still an error, because if someone decides to type a really long line, they will overflow the buffer. At best, this will crash your program. Second best, it will corrupt the data leading to undefined results. Third best, it will allow a malicious person to execute arbitrary code.

    So we have to limit scanf's input length:
    Code:
    scanf("%499s", line);
    Hope this cleared at least something up.

  7. #22
    C_newbie
    Join Date
    Oct 2005
    Location
    Greece
    Posts
    32
    Yes you helped....
    But why doesn't it stay at the prompt and goes directly to the function? Should I change ti to scnaf again?
    AA
    AC/DC: Highway to Hell!
    No speed limit, no stop signs, nobody 's gonna slow me down!

  8. #23
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Are you saying the fgets doesn't occur, it doesn't wait for a line of input? If you are calling fgets and that happens, then you already have something on stdin.

    If you want to make sure everything is printed beforehand, make sure you put a fflush(stdout) after your printf.

  9. #24
    C_newbie
    Join Date
    Oct 2005
    Location
    Greece
    Posts
    32
    I have:
    Code:
    else if (l=='b'){
                    /*Dialog to insert a new line before current line.*/
                    printf("Insert the new line:\nTEEDI>");
                    fflush(stdout);
                    fgets(newline, MAXLINE,stdin);
                    InsertBefore(buffer, newline);
            }
    but look:
    Code:
    TEEDI>b
    Insert the new line:
    TEEDI>TEEDI>
    actually Between the two prompts, I schould insert the text. Now it wants a new command again like 'b' and if i insert a text I get that there is no command like this.
    AC/DC: Highway to Hell!
    No speed limit, no stop signs, nobody 's gonna slow me down!

  10. #25
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by antonis
    @Quzah:
    I dont think you help a lot you just laugh about my problem and the other persons posts,
    There's a saying by a good friend of mine, Daffy Duck: "It is to laugh."

    Quote Originally Posted by antonis
    and you tell us why we don't know anything and why this and the other is false, but you don't tell us the right thing.
    Have you ever taken a test in school, and the teacher hands it back with big red marks on all the wrong answers? But they never really tell you the right one? Yeah, I hate that. Think of me as the guy with the red pen.

    Ah, but I did tell you what was right, by telling you what was wrong.
    Quote Originally Posted by antonis
    Maybe you don't know either, who knows. Maybe the others that have answered need help too as you say but at least they try to help me and do not laugh about me.
    There's another good quote, a few in fact:

    "Laughter is the best medicine."

    "Learn to laugh at your problems; everybody else will."

    Quote Originally Posted by antonis
    OK about the first 2 posts I suppose you're right, but I have already corrected this codes with the help of other forum members, even if they are not as good as you.
    I did help. You just didn't like the way I helped. I pointed out the errors in both cases, giving exactly what was wrong. The other replies before mine were incomplete, or flat out wrong, in most cases.

    Quote Originally Posted by antonis
    The segfault goes away when I use the ASSIGNMENT OPERATOR (&) in printf f not when using printf. But its more funny writing about LUCK! Isn't it quzah?
    The difference between you and I is that I actually know what I'm talking about. See, you think you're brilliant here, and that somehow I'm wrong. Sadly for you, that just isn't the case. Sorry.

    Quote Originally Posted by antonis
    I hate irony, so I won't answer here.

    Here there is Irony again- which is normally used when we don't have arguments about our opinion.

    One more time, ironical, quzah is 1000 levels above all of us.
    Here's one for you:

    "You keep using that word. I do not think it means what you think it means." - Inigo Montoya
    Quote Originally Posted by antonis
    I dont think its unrelated, here the problem started and I decided to make a smaller programm to read the line, whith your (plural of course) help. When it finally worked I tryed to put the code in my previous programm but there I had a problem. why incomplete I have the fgets and then the programm goes directly to InserBefore whithout waiting for me to insert the line. I think it's all there what you need, but if you like smthing more you can ask for it. If you want me to copy in every post 350lines of code for a problem that needs 5 lines then I can do it, but I think its easier so.
    See, had you included this fine little paragraph, explaining what that seemingly random chunk of code was in reference to, and had you actually included the variable declaration of l, then it would have had context. Had it any context at all, you'd be right. Since it didn't, you aren't.
    Quote Originally Posted by antonis
    In my code there are single quotes. But its again not funny to see the post of the code but to see a reply with " " which is a question why "b" and of course if you read Barnzeys whole post you will understand (?) that he means 'b'. he doesnot add some code, but sees from my if that there must be more post.
    No he didn't. He meant "" and used "". He meant, or at least we'll assume it, since he didn't actually form a sentence, "Do you mean 'l == "b"?'

    You see, if you hang out here long enough, as I have, you'll commonly see people try to compare strings with the equality operator. Had you actually read my reply, instead of pretending your feelings were hurt, you'd have seen my illustration of said point. Furthermore, since I wasn't in fact talking about your post there, you can stop pretending to be upset about it.
    Quote Originally Posted by antonis
    No thanks I think this was enough.
    Now if you want to answer me, dont do it on this post I wont reply. We are out of topic, and it has no sense. Just pm me.
    Antonis
    In other words, "I'm going to whine in public, but don't call me on anything."


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

  11. #26
    C_newbie
    Join Date
    Oct 2005
    Location
    Greece
    Posts
    32
    Of course I don't think I'm brilliant, I am absolutely a newbie.
    The teacher first tells you the right thing and then corrects your mistakes.
    I don't have anything against laughing but against laughing at others because they know less or are stupid.
    If you look I am from greece. Irony is a greek word and I don't think you know better what it means. Open a wordbook-irony has nothing to do with humor or whatever you think.
    I think you miss the point.
    Last edited by antonis; 10-27-2005 at 03:58 AM.
    AC/DC: Highway to Hell!
    No speed limit, no stop signs, nobody 's gonna slow me down!

  12. #27
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by antonis
    If you look I am from greece. Irony is a greek word and I don't think you know better what it means. Open a wordbook-irony has nothing to do with humor or whatever you think.
    I think you miss the point.
    Irony. Ironically, it has a number of meanings.


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

  13. #28
    C_newbie
    Join Date
    Oct 2005
    Location
    Greece
    Posts
    32
    for you maybe! But I won't play this game any longer.
    AC/DC: Highway to Hell!
    No speed limit, no stop signs, nobody 's gonna slow me down!

  14. #29
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    Sorry for interupting this nice little argument, but did you fix your program?

  15. #30
    C_newbie
    Join Date
    Oct 2005
    Location
    Greece
    Posts
    32
    No I used flush too, but the 2 prompts are still there. I had to send it before 1 hour, so I put a comment explaining what doesn't work. It's minor to the whole programm but I have spent so many hours that I wanted it to be perfect. Next week the professor will give us his version, and then maybe I can fix mine...
    Who ever likes to see the programm, I can post it... It's a very very simple text editor. Thanks to all of you!
    AA
    AC/DC: Highway to Hell!
    No speed limit, no stop signs, nobody 's gonna slow me down!

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 09:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-21-2005, 12:41 AM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM

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