d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Small Problem In C
12Next
Add Reply New Topic New Poll
Member
Posts: 1,753
Joined: Apr 12 2011
Gold: 0.00
Nov 24 2012 09:16pm
Code
main()
{ char ch;
 int opnd;

 write_message("Operand Test");

       while ((ch = getchar()) != 'q')
       {
               if (ch == '\n')
               {
                       write_char(ch);
                       break;
               }

                       opnd = get_opnd(ch);
                       write_debug(opnd);
       }
}



for every valid input, i pass the input to a function, print the results, and grab a new input. if input is a newline character, i want to pass it to another function and break from the loop. for whatever reason, the newline character isn't getting recognized by the if statement and it's still getting passed to the second half of the loop. even with "!= '\n'" parameters on the second part of the loop, the newline character is still going through

i put debugging statements in the function and throughout the driver but i have no idea why it's acting like that. if i take out the if statement and leave the "write_char()" function in the loop, inputing a newline will give me the correct output for the function so i don't see why it's not getting recognized in the if statement parameter. any idea on where my problem is or what else i could do to check?
Member
Posts: 5,641
Joined: Apr 13 2006
Gold: 2.00
Nov 24 2012 09:50pm
'\r\n' is probably what you need to look for.

This post was edited by SilverMice on Nov 24 2012 09:51pm
Member
Posts: 1,753
Joined: Apr 12 2011
Gold: 0.00
Nov 24 2012 10:38pm
Quote (SilverMice @ Nov 24 2012 05:50pm)
'\r\n' is probably what you need to look for.


thanks. \r works....actually never encountered that before

what's the problem with \n in this scenario though?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 24 2012 10:47pm
Quote (Reverence @ Nov 25 2012 12:38am)
thanks.  \r works....actually never encountered that before

what's the problem with \n in this scenario though?


im not sure if you're describing the problem accurately or not.

i assume you're on a windows machine where the newline is \r\n, so your function first gets called for \r (which goes through get_opnd) and you mistakenly think it's \n. then after this executes, it reaches \n and breaks. if you use unix then \n is the newline character and your code should work as intended

i dont know what c debuggers are available since i'm not a C guy, but im sure there's one out there that lets you line-by-line debug and hover over your variable to let you know it's \r instead of \n. you might wanna look into it.
Member
Posts: 1,753
Joined: Apr 12 2011
Gold: 0.00
Nov 24 2012 11:13pm
Quote (carteblanche @ Nov 24 2012 06:47pm)
im not sure if you're describing the problem accurately or not.

i assume you're on a windows machine where the newline is \r\n, so your function first gets called for \r (which goes through get_opnd) and you mistakenly think it's \n. then after this executes, it reaches \n and breaks. if you use unix then \n is the newline character and your code should work as intended

i dont know what c debuggers are available since i'm not a C guy, but im sure there's one out there that lets you line-by-line debug and hover over your variable to let you know it's \r instead of \n. you might wanna look into it.


unix. like i said \n doesn't get recognized in the parameter for some reason. if anyone knows why i'd appreciate an explanation

i've never seen/used \r before but it works like how i expected \n to work... \r\n was recognized as multiple characters and gave me an error which is because i'm on unix?

This post was edited by Reverence on Nov 24 2012 11:15pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 24 2012 11:17pm
Quote (Reverence @ Nov 25 2012 01:13am)
unix.  like i said \n doesn't get recognized in the parameter for some reason.  if anyone knows why i'd appreciate an explanation

i've never seen/used \r before but it works like how i expected \n to work... \r\n just gives me a compiler error but i guess that's because i'm on unix?


\r\n are two characters, \r and \n so that's why you have a compile error. just check for \r followed by \n

how did you verify that it's \n getting ignored and it's not actually \r followed by \n? did you check the unicode value or what?

This post was edited by carteblanche on Nov 24 2012 11:18pm
Member
Posts: 1,753
Joined: Apr 12 2011
Gold: 0.00
Nov 24 2012 11:31pm
Quote (carteblanche @ Nov 24 2012 07:17pm)
\r\n are two characters, \r and \n so that's why you have a compile error. just check for \r followed by \n

how did you verify that it's \n getting ignored and it's not actually \r followed by \n? did you check the unicode value or what?


i think i understand what you're saying....and i guess that's exactly what's happening lol


i'm still a bit confused about it though. in other code i've been using \n perfectly fine. what is the programming seeing with ch=getchar() when i hit enter?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 24 2012 11:38pm
Quote (Reverence @ Nov 25 2012 01:31am)
i think i understand what you're saying....and i guess that's exactly what's happening lol


i'm still a bit confused about it though.  in other code i've been using \n perfectly fine.  what is the programming seeing with ch=getchar() when i hit enter?


assuming it's \r and \n together, your code should still pick up \n just a bit later. so if you look for \n to terminate or something, you probably won't notice the effect from \r and your code will seem to work fine. im guessing you only noticed it here because get_opnd('\r') does something

for debugging purposes, cast the char to an int then print it so that you can see the unicode value. then compare it to (int)'\r' and (int)'\n' and see which it is.
Member
Posts: 1,753
Joined: Apr 12 2011
Gold: 0.00
Nov 25 2012 12:00am
Quote (carteblanche @ Nov 24 2012 07:38pm)
assuming it's \r and \n together, your code should still pick up \n just a bit later. so if you look for \n to terminate or something, you probably won't notice the effect from \r and your code will seem to work fine. im guessing you only noticed it here because get_opnd('\r') does something

for debugging purposes, cast the char to an int then print it so that you can see the unicode value. then compare it to (int)'\r' and (int)'\n' and see which it is.


made a short program and i was getting 10 as expected. added it to this program and i was getting 13. what the
Member
Posts: 2,303
Joined: Oct 11 2007
Gold: 0.00
Nov 27 2012 07:52pm
Try to use fflush() before calling getchar !

Got me out of trouble a couple times (using visual studio 2010)

This post was edited by Eleven11 on Nov 27 2012 07:55pm
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll