Reputation: 111
I am new in C programming. While I was trying to code, I came across this problem. Sorry, if the solution is obvious and I posted it here.
#include <stdio.h>
int main(){
int T = 2;
while(T--){
c = getc(stdin);
while( (c != '\n') && (c != EOF)){
// do some work
c = getc(stdin);
}
}
return 0;
}
what I want is like if I have multiple string inputs in multiple lines, e.g.,
Hithere
howareyou
Iamgood
And then, if after reading characters "hith", the while loop is broken then the next character to be read by fgetc(stdin) should be from the second line(howareyou) NOT the characters from "Hithere".
Actually, I will be given 3 input strings in 3 different lines. And I have to check the strings individually for some special character. And if the character is found then I will print that it is found and immediately stop checking any further characters in that string. Similarly, check for the next string, then next string and so on.
Many many thanks in advance. Please help me.
Upvotes: 4
Views: 3745
Reputation: 206717
The logic in this line is incorrect.
while( (c != '\n')|| (c != EOF)){
Change it to:
while( (c != '\n') && (c != EOF)){
Upvotes: 2
Reputation: 5661
It really depends on the operating system you are running this program. For example, in Windows a line ends with \r\n
while on Unix, it ends in \n
.
Upvotes: 1
Reputation: 409442
For your second question (in the future, please only post one question per question!) you might want to use fgets
to read a line, and then get characters from the read line.
Upvotes: 1