Anonymous
i been using scanf but i am learning about fgets
Anonymous
but when input data into the string and use printf it create a newline in the middle of the statement
Anonymous
how do i not do that
C.
In the end of the file input must have
So his string reads a '\n' char
Anonymous
fgets used to input data , stored in a string
Anonymous
when i try to print it using printf it creates a new line after the string
Anonymous
this is not what i intend
Anonymous
#include <stdio.h> int main(){ char someString[10]; puts("what is your favorite color"); fgets(someString,sizeof(someString), stdin); printf("%s is my favorite color\n", someString); return 0; }
Anonymous
this is C
C.
so remove \n from printf
Did you read what he said?
C.
middle not end of the sentence
Anonymous
so lets say i input blue
C.
lol
Show us an output please
Anonymous
Humbles-MacBook-Pro:ChapterTwo humblerise$ gcc fgetit.c -o Fgets && ./Fgets what is your favorite color Blue Blue is my favorite color
Anonymous
but it should be “Blue is my favorite color” all in one line
C.
for(size_t i = 0; i < 10; i++) { if (color[i] == '\n') { color[i] = '\0'; break; } }
Harsh
Oops
I_Interface
but it should be “Blue is my favorite color” all in one line
remove puts, switch it into an usual printf for ur text
Anonymous
lol
C.
In files?
Anonymous
wow it really requires a for loop
Anonymous
lol
C.
Well
Harsh
does that work in C
Use fflush(stdin) after input
C.
I didn't want to use it
C.
Use fflush(stdin) after input
It's not stdin goddamnit
C.
wow it really requires a for loop
The loop is to take the line break character in the string you got from the file.
Anonymous
this worked , but is that the simplest way of doing it?
C.
But I need to think to make a simpler.
I_Interface
Change the 10 to the size of the array
ah, didn't relize what it's a char array, prefer to use strings :)
Anonymous
well the for loops does show me that there is a \n added by the fgets , so its helpful
C.
string.h ?
It doesn't add string type
Anonymous
i only on chapter 2 btw lol
Anonymous
Idk if fgets added one or if it was in the file already
if you use scanf , it doesnt have this issue
C.
if you use scanf , it doesnt have this issue
Scanf is just for stdin. Fgets is for file manipulation
C.
fgets = file get string.
Anonymous
fgets = file get string.
i see , i guessing i will get to file manipulation within the next few pages
C.
You'll have pointers before.
C.
I think
Anonymous
You'll have pointers before.
i learnt about pointers, they store memory address, and are used to mutate data stored in that memory address
C.
So. Yeah, file manip is near XD
Anonymous
and array variables are treated like pointers for the first character of an array
C.
And files are pointers too
Anonymous
Anonymous
this should be fun
C.
It'll make you cry
C.
:D
Anonymous
It'll make you cry
i enjoy self inflicted pain, or i would learn python
Anonymous
or even c++ lol
I_Interface
or even c++ lol
C++ - is a language of Gods
Anonymous
C++ - is a language of Gods
what about that Rust tho
C.
Lol
Anonymous
facebook is using rust for Libra dev
C.
what about that Rust tho
What about brainfuck?
Dominic
Guy's can someone teach me Algorithm, Pseudo code and flow chart
Anonymous
i am learning C because i had to choice between C or java for this certification i am studying for
Anonymous
i figure learning a manual memory management language will help me on the long run
C.
Guy's can someone teach me Algorithm, Pseudo code and flow chart
Well, it depends on how much you're willing to pay
Dominic
Oh bro ... Why