l == "b" ??
Would help if we could see the whole code.
l == "b" ??
Would help if we could see the whole code.
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....Originally Posted by antonis
put a fflush(stdout) after your "printf(" call.
I'll help you all here, because you all need it. Let's go post by post, it'll be faster that way.
1) In this first post, line is a single character.Code:#include <stdio.h> main() { char line; scanf("%s",line); /*NOW USE THE TEXT LINE WHILE CALLING A FUNCTION*/ }
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:
1) If line were an array, it would be wrong because you don't use the address-of operator here with arrays.Originally Posted by Axel
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:
1) line now is a pointer, but it doesn't point to anything.Originally Posted by antonis
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.
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.Originally Posted by antonis
"I see bad code."
"In your dreams?"
*shakes head no*
"How often do you see them?"
"All the time."
There's a FAQ on why gets is so bad. Then again, there's a FAQ on reading a line from the user.Originally Posted by Quantum1024
From the "I knew I should have taken that left turn at Albequerque" department, comes this guy:
Nice try, wrong forum. Say "Hi" to Bugs Bunny next time you see him.Originally Posted by durban
Next we get one more of the usual suspects:This one is our friend "Hey, here's a completely unrelated piece of code, which is incomplete and impossible to troubleshoot!"Originally Posted by antonis
This guy recognizes our familiar suspect, but is waylayed by either the C++ guy, or the absence of strcmp:
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.)Originally Posted by Barnzey
"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.
Code:Originally Posted by TactX
Output: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; }
Code:Enter a line: one two three Entered: "one two three"
@cwr: This code is OK but here:I get 2 prompts again and can not insert any code....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); }
-----------------------------------------------------------------------
@Quzah: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.I'll help you all here, because you all need it. Let's go post by post, it'll be faster that way.
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.
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?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 hate irony, so I won't answer here."I see bad code."
"In your dreams?"
*shakes head no*
"How often do you see them?"
"All the time."
Here there is Irony again- which is normally used when we don't have arguments about our opinion.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:
One more time, ironical, quzah is 1000 levels above all of us.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.
[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.
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.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.)
No thanks I think this was enough."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.
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!
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.Originally Posted by antonis
The unary & operator means "address of", not "assignment" so when you do:
Or some other odd combination, you are doing crazy things on both counts.Code:char *line; scanf("%s", &line); printf("%s", &line);
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:
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.Code:char *line; scanf("%s", line); printf("%s", line);
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:
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.Code:char *line; line = malloc(500); if (line == NULL) { /* oh no */ exit(1); } scanf("%s", line); printf("%s", line);
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:
Hope this cleared at least something up.Code:scanf("%499s", line);
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!
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.
I have:
but look: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); }
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.Code:TEEDI>b Insert the new line: TEEDI>TEEDI>
AC/DC: Highway to Hell!No speed limit, no stop signs, nobody 's gonna slow me down!
There's a saying by a good friend of mine, Daffy Duck: "It is to laugh."Originally Posted by antonis
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.Originally Posted by antonis
Ah, but I did tell you what was right, by telling you what was wrong.
There's another good quote, a few in fact:Originally Posted by antonis
"Laughter is the best medicine."
"Learn to laugh at your problems; everybody else will."
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.Originally Posted by antonis
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.Originally Posted by antonis
Here's one for you:Originally Posted by antonis
"You keep using that word. I do not think it means what you think it means." - Inigo MontoyaSee, 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.Originally Posted by antonisNo 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"?'Originally Posted by antonis
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.In other words, "I'm going to whine in public, but don't call me on anything."Originally Posted by antonis
Quzah.
Hope is the first step on the road to disappointment.
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 02:58 AM.
AC/DC: Highway to Hell!No speed limit, no stop signs, nobody 's gonna slow me down!
Irony. Ironically, it has a number of meanings.Originally Posted by antonis
Quzah.
Hope is the first step on the road to disappointment.
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!
Sorry for interupting this nice little argument, but did you fix your program?
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!