Jump to content

C help yet again..

WorldDominator

Confession: I don't really know how to code in C

 

I do c# and some java

Close enough tbh :D

i5 4670k | Sapphire 7950 | Kingston 120GB SSD | Seagate 1TB | G.Skill Ripjaw X Series 8GB

PB238Q | Steelseries Sensei | Ducky DK9087 | Qck Heavy

Build Log: http://linustechtips.com/main/topic/44902-from-imac-to-my-own-creation/

Link to comment
Share on other sites

Link to post
Share on other sites

I think using fgets() for console input is overkill. Have you tried :

scanf("%[^\n]",string_variable);

This will keep reading till a newline is found. IIRC %s will only read single words.

Link to comment
Share on other sites

Link to post
Share on other sites

I think using fgets() for console input is overkill. Have you tried :

scanf("%[^\n]",string_variable);

This will keep reading till a newline is found. IIRC %s will only read single words.

 

Huh, I guess I forgot scanf took that kind of regex as a format!

Link to comment
Share on other sites

Link to post
Share on other sites

I think using fgets() for console input is overkill. Have you tried :

scanf("%[^\n]",string_variable);

This will keep reading till a newline is found. IIRC %s will only read single words.

I haven't seen anything like that before O.o

I did find another solution though. Thanks guy!

i5 4670k | Sapphire 7950 | Kingston 120GB SSD | Seagate 1TB | G.Skill Ripjaw X Series 8GB

PB238Q | Steelseries Sensei | Ducky DK9087 | Qck Heavy

Build Log: http://linustechtips.com/main/topic/44902-from-imac-to-my-own-creation/

Link to comment
Share on other sites

Link to post
Share on other sites

The problem is that fgets() doesn't strip the newline from the input, you need to do it yourself (for example using strok(string_with_newline, "\n")) then you should be OK. 

 

Also:

  • I don't think the fflush()es are necessary (I haven't tested it though)
  • You can use the sizeof() operator in your fgets() statements (eg: fgets(list[n].string, sizeof(list[n].string), stdin)
Link to comment
Share on other sites

Link to post
Share on other sites

Yea, what @random314 said you have a new line character at the end of the string.

 

You could also do list[n].first[strlen(list[n].first)-1] = '\0'; assuming you check that first is also >= 1

 

One last point using fgets it is very important that you get the array amount right.  Your fgets for first can overflow, since you can write 21 characters to an 16 character array

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×