Thread: problem with program

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    44

    problem with program

    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, &currentpos, &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. . .
    Last edited by loso44x; 10-22-2005 at 03:26 PM.

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    The code you posted should be a continuos loop, not only twice, you never change the value of keeprunning, after you start the loop.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    44
    i took out the default: in the switch statement because the function in the program ran twice so if u were to put n, the program takes in n and a space and becuase u entered a space, u also enter the default in the switch statement and then ending the whole loop.

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Quote Originally Posted by loso44x
    i took out the default: in the switch statement because the function in the program ran twice so if u were to put n, the program takes in n and a space and becuase u entered a space, u also enter the default in the switch statement and then ending the whole loop.
    Well you might want to look at fgets() rather than using scanf.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    44
    the problem is that the function nextprev runs twice instead of once not the scanf problem. so when the program runs u see the print statements twice and it wants two inputs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 11:10 AM
  2. Program Termination Problem
    By dacbo in forum C Programming
    Replies: 3
    Last Post: 01-23-2006, 02:34 AM
  3. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  4. Replies: 20
    Last Post: 06-12-2005, 11:53 PM