Techiio-author
Started by Anik AdhikaryMay 6, 2022

Open
Please someone tell me why the code can't break out of the loop ?

5 VIEWES 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 5 VIEWES SHARE
#include<stdio.h>
int main()
int n;
int tie;
int smiles = 0,joy = 0;
char d;
scanf("%d",&n);
for (int i; i = n; i++)
{
scanf("%c",&d);
if (d == "S" )
{
smiles++;
}
else if (d == "J" )
{
joy++;
}
else
{
tie++;
}
}
if (smiles > joy)
{
printf("Mr.Smiles");
}
else if (joy > smiles){
printf("Mr.Joy");
}
else if (smiles == joy){
if (tie % 2 == 0 ){
printf("Mr.Smiles");
}
else {
printf("Mr.Joy");
}
}

}

1 Replies

Techiio-commentatorPOULAMI SARKAR replied 9 days ago0 likes0 dislikes

I have been delving into C and rather using scanf() I wanna try to build my own function to get an entire line. I wrote this function that returns a line to be compared in my main function, except it won't break out of the loop to print my result.

I don't know if it's the actual function or just how I implemented it in main().

Here's the code:

main() {
int len;
int max = 0; //Largest line so far..
char linetemp[MAXLINE];
char linemax[MAXLINE];
while((len = getLine(linetemp,MAXLINE))> 0){
if(len>max){
max = len;
cpy(linemax,linetemp);
}
}
if(max>0) { //If their was even a line
printf("Longest Line:\n %s \n", linemax);
} else {
printf("No lines found!");
}
}

int getLine(char l[], int lim) {
int c, i;
for(i=0; i< lim - 1 && (c=getchar())!= '\n' && c != EOF ;++i)
l[i]= c;
if(c =='\n') {
l[i]= c;
++i;
}
l[i]= '\0';
return i;
}

You must be Logged in to reply
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance