can anybody help me with this problem, the function nextprev() runs twice for some reason and becuase of this there's no way to break out of the look. can someone tell me why it runs twice or how to make it run only once at a time?
Code:
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
struct input {
char sentence[300];
};
void print(struct input line[], int *previouspos, int *currentpos)
{
//system("cls");
}
void nextprev(struct input line[], int *currentpos, int *previouspos, int *tmppos, int *keeprunning)
{
char choice;
int i;
printf("N for more, P for previous, any other key to exit. . .\n");
scanf("%c", &choice);
switch(choice)
{
case 'n':
case 'N':
*previouspos = *currentpos;
*currentpos += 20;
if(*previouspos < 0 || *currentpos == 0)
{
*previouspos = 0;
*currentpos += 20;
}
for(i = *previouspos; i < *currentpos; i++)
printf("%s", line[i].sentence);
break;
case 'p':
case 'P':
*tmppos = *previouspos;
*currentpos -= 40;
*previouspos = *currentpos;
*currentpos = *tmppos;
if(*previouspos < 0 || *currentpos == 0)
{
*previouspos = 0;
*currentpos += 20;
}
for(i = *previouspos; i < *currentpos; i++)
printf("%s", line[i].sentence);
break;
//default: *keeprunning = 0;
}
}
void printLines(struct input line[])
{
int i, previouspos = 0, currentpos = 0, tmppos = 0;;
int temp = 1, keeprunning = 1;
for(i = 0; i < 20; i++)
printf("%s", line[i].sentence);
currentpos += 20;
while(keeprunning == 1)
nextprev(line, ¤tpos, &previouspos, &tmppos, &keeprunning);
printf("keeprunning = %d\n" , keeprunning);
printf("REACHES END");
}
int num_lines(char name[]) // this function counts the number of lines in file
{
int ch, prev = '\n', lines = 0;
FILE *in_file;
in_file = fopen(name, "r");
while ( (ch = fgetc(in_file)) != EOF )
{ // read chars in file
if ( ch == '\n' )
++lines; // counter
prev = ch;
}
fclose(in_file);
if ( prev != '\n' ) // if last line did not end in new line
++lines;
return lines;
}
int main(int argc, char *argv[])
{
struct input line[500];
int numline, x;
numline = num_lines("example.txt");
FILE *in_file;
in_file = fopen("example.txt", "r");
if (in_file == NULL)
{
fprintf(stderr, "Unable to locate the file!\n");
exit(1);
}
for(x = 0; x < numline; x++)
fgets(line[x].sentence,299,in_file);
printLines(line);
fclose(in_file);
getch();
return 0;
}
output
Code:
Extension List Fix files should be directed to McAfee
Technical Support.
- About This Extension List Fix
N for more, P for previous, any other key to exit. . .
N for more, P for previous, any other key to exit. . .