Thread: Whats wrong?

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by $l4xklynx
    I see, but why put fgets as an expression in the if condition?
    If fgets() returns a null pointer, then you would not want to replace the newline with a null character since either there were no characters read, or there was an I/O error.

    Quote Originally Posted by $l4xklynx
    yes they can but it's wrong in C, that's what I meant...
    It is not wrong in C.

    Quote Originally Posted by $l4xklynx
    But in his program I don't think that he needs to initialize the array.
    That is true.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Apr 2009
    Posts
    13
    Quote Originally Posted by $l4xklynx View Post
    I see, but why put fgets as an expression in the if condition?



    yes they can but it's wrong in C, that's what I meant... But in his program I don't think that he needs to initialize the array.

    No its not wrong in C, and I always initialize my string arrays just in case I forget to put a null character after I use some functions to manipulate strings. It is just a good programming practice not to leave the variables holding garbage.

    and

    you can also do

    Code:
    char mystring[MAX] = {};
    same thing, and totally valid. Just filling up my array with null characters.

  3. #18
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by creeping death View Post
    i woundn't be too worried about both of them...let the compiler perform common subexpression elimination and dead code elimination...which all standard compilers do by default.
    That's being overly optimistic to the point of actually being hazourdous. You're pretending amoung other things, that the compiler is able to prove that printf is a function that does not hold references to either of those strings and modify them.
    What's more, it's more a point about code duplication which is bad for maintenance.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #19
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    It wont let me! What it is doin is when i click start without debugging it lets me enter the first and second string. Then once I press enter it prints out the first string is larger than the second string infinite times. It wont stop printing that. There is nothin I guess in my program that tells it to stop so thats what I need help at to figure it out.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    jturner38, what is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User
    Join Date
    Apr 2009
    Posts
    13
    Quote Originally Posted by jturner38 View Post
    It wont let me! What it is doin is when i click start without debugging it lets me enter the first and second string. Then once I press enter it prints out the first string is larger than the second string infinite times. It wont stop printing that. There is nothin I guess in my program that tells it to stop so thats what I need help at to figure it out.
    did you use the code I posted?... we told you to get rid of the loop and other stuff, use the code I posted, its pretty much the same you had plus some recommendations already given on here.

  7. #22
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    i guess...i reluctantly agree...you have got a point there...(@iMalc and @laserlight)

    @vart...i dont know...i hav'nt really dug into this compiler stuff...i will though, soon ... for now all that i could comprehend was that the compiler "somehow"(maybe using DAG?) gets to know if the value of an expression changes or not...and if it does not change, the "common sub-expression elimination technique" is applied on the intermediate code generated by the compiler. so if the value changes,like by a thread(like you mentioned) , then this concept would not be applicable,(i guess)...

    ( but guess this probably shouldn't be applied to functions?) ...
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  8. #23
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    This is what I have. The program builds successfully from my original to everybody else's way. When u run the program it tells u to enet the first string then enter the second string. The problem is right after u press enter after u have entered the second string it says the first string is larger everytime and not onlyy that but it also prints it out infinte amout of times as in it keeps on running and it never stops. The only way u can stop it is if u exit out of it.

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<ctype.h>
    
    #define MAX 100
    
    int main() {
    
    	char s1[MAX] = {0};
    	char s2[MAX] = {0};
    	int x;
    	
    	printf("Input first string:\n");
    	fgets (s1 , MAX , stdin);
    
    	printf("Input second string:\n");
    	fgets (s2 , MAX , stdin);
    
    	x = strcmp(s1, s2);
    
    	if (x == 0)
    		printf("These strings are equal\n");
    	else if (x > 0)
    		printf("The first string is larger\n");
    	else
    		printf("The second string is larger\n");
    
    	return 0;
    }

  9. #24
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    this program does not even has a loop, how it can continue printing something?

    probably - you are not compiling it correctly, or running the old file and not the one build by the compilation.

    read carefully the output of the compiler and check where the output executable is located
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #25
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    what compiler are you using?
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM