Input/Output Program's Output file is blank

I'm currently taking an "introductory" C++ course for biology majors and my professor gave us a code to use for a homework assignment. It compiles without any errors but when I run the program the output file is blank. I emailed my professor and all he said was that it worked for him so he doesn't know why it's not working. I know I'm typing the input file name correctly and it's saved in the same folder as the program. Thank you in advance to anyone who is able to help me out.

I'll also post the input file I'm using for reference if that helps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<cmath>
#include <stdio.h>
using namespace std;

int main(){
   ifstream seqfile;
   int  seqLength=0;
   double count=0, NT_c[4][10]={{0},{0},{0},{0}}, I[10]={0}, H[10]={0};
   double sites[10];
   string line, filename;
   char filename2 [20];

   cout<<"Input file name: " << endl;
   getline(cin, filename);

   cout<<"Output file name: " << endl;
   cin >> filename2;




   // Read data from file and compute frequencies
   seqfile.open(filename.c_str());
   while (getline(seqfile, line)) {
        seqLength= line.length();
        for(int i=0; i < seqLength ; i++){
          if( line.substr(i,1) == "A" || line.substr(i,1) == "a")
                NT_c[0][i]++;
          else if (line.substr(i,1) == "G" || line.substr(i,1) == "g")
                NT_c[1][i]++;
          else if( line.substr(i,1) == "C" || line.substr(i,1) == "c")
                NT_c[2][i]++;
          else if (line.substr(i,1) == "U" || line.substr(i,1) == "u")
                NT_c[3][i]++;
          else if (line.substr(i,1) == "-")
                ;
          else
               cout << "Error in the input sequence" <<endl;
          }
        count++;    }

// Compute Entropy and Information metrics
    for (int j=0; j < seqLength; j++){
      for(int z=0; z < 4 ; z++){
        if(NT_c[z][j] > 0)
          H[j]-= (NT_c[z][j]/count)*log(NT_c[z][j]/count)/log(2);
      }
      if(NT_c[0][j] + NT_c[1][j] + NT_c[2][j] + NT_c[3][j] ==0)
        I[j]=0;
      else
        I[j] = 2 - H[j];
      }

FILE* outfile;

// Store Logo Information in text file
outfile = fopen(filename2,"w");

   for (int j=0; j < seqLength; j++){
     fprintf(outfile,"Information I[%d] = %f \n", j+1,I[j]);
     fprintf(outfile,"Height of base A[%d] = %f \n", j+1, I[j]*NT_c[0][j]/count);
     fprintf(outfile,"Height of base G[%d] = %f \n", j+1, I[j]*NT_c[1][j]/count);
     fprintf(outfile,"Height of base C[%d] = %f \n", j+1, I[j]*NT_c[2][j]/count);
     fprintf(outfile,"Height of base U[%d] = %f \n", j+1, I[j]*NT_c[3][j]/count);
     fprintf(outfile,"-----------------------------------\n");
  }
  fclose(outfile);
   return 0;
}
Here's the text file that I need to use as the input file.

CCAU-
AGAU-
CCGU-
AAGU-
ACGU-
GGGU-
CCUU-
CGGU-
GUGU-
CUGU-
GCAU-
GCGU-
AUAU-
GCGU-
UUUU-
CCAU-
CCAU-
UGAU-
AGGU-
UAUU-
CUGU-
UUAA-
CCUU-
UUGA-
AUGA-
AUGU-
UUAU-
CUAU-
UAUU-
GGAU-
GUAU-
AAAU-
CCGU-
CUGU-
GAGU-
GCGU-
CCGU-
GGCU-
CCGU-
AUAU-
UUAA-
ACAU-
ACGU-
CAAU-
CCGU-
UCGU-
CGUU-
CAGU-
UCGU-
UCGU-
GUAA-
UUGU-
AUAU-
GAAU-
GGUU-
UGGU-
UUGU-
UGAU-
GGAU-
GGAU-
GUGU-
GAUU-
GUGU-
AUGU-
AAGU-
GUGU-
CUUU-
GAAU-
GGGU-
CGGU-
UUGU-
CUGU-
CAGU-
AAGU-
CGUU-
AUGU-
CAUU-
GUAU-
UCAU-
GUGU-
UUCU-
CUAU-
GGGU-
GGAU-
CUGU-
AAUU-
UUCU-
CCAU-
GUUU-
CCGU-
GGGU-
CUGU-
AUUU-
UUGA-
GAAU-
CUGU-
GCGU-
UUUU-
CCAU-
CCAU-

What is your assignment all about?
The end product is supposed to be a "sequence conservation logo calculator". We need to find a way to combine the above code with the one below so that the program creates a graph that shows the average of A's, U's, C's, and G's in the input file. Like if there are a lot of A's in one row the A on the graph will be big whereas if there are only a few C's in another row the C on the graph will be small. The letters on the graph are supposed to be made up of little letters (like the big A will be made up of little A's). Sorry if my description is a little confusing, to be honest this assignment is confusing for me. Before this we were doing simple loops and switches and now all of a sudden we're doing this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stdio.h>
#include <curses.h>

int main ( )
{
  char resp;
  int i,j,y,x;
  int width,height;

  initscr();
  getmaxyx(stdscr,height,width);
  clear();

  box(stdscr,'*','-');
  move(1,2);  // goto row 1 column 2
  printw("Print BIG letters");
  move(1,width-10);
  printw("10/13/16");
  /* send stdscr to terminal  */
  refresh();

  cbreak(); // make characters immediately accessible
  noecho(); // don't echo inputs

  move(height-2,2);
  printw("Hit any key to see what happens next:  ");
  getyx(stdscr,y,x);
  move(y,x-2);

  refresh();
  resp = getch();
  // draw big H
  for (i=3; i < 22 ; i++ )
  {
    move(i,15);
    printw("Hello");
    move(i,35);
    printw("Hello");
    if ((10 < i) && (i < 14))
    {
      move(i,20);
      for (j=20; j<35; j+=5)
        printw("Hello");
    }
  }
  refresh();


  move(height-2,2);
  printw("Hit any key to see what happens next:  ");
  getyx(stdscr,y,x);
  move(y,x-2);
  refresh();
  resp = getch();

  // draw big i
  for (i=3; i < 22 ; i++ )
  {
    move(i,55);
    printw("Hello");
    if (((3 <= i) && (i <= 5)) || ((19 <= i) && (i <= 21)))
    {
      move(i,50);
      for (j=50; j<65; j+=5)
        printw("Hello");
    }
  }

  move(height-2,2);
  printw("Hit any key to exit:                   ");
  move(height-2,22);
  resp = getch();

  endwin();

  return 0;
}
We need to find a way to combine the above code with the one below so that the program creates a graph that shows the average of A's, U's, C's, and G's in the input file.

What do you mean? You need to count A's, U's, C's, and G's in the input file?
If you look up sequence conservation for biology it might give you a better explanation than I could.
The first thing to do is verify that the input file is opened, which you are not currently doing. If it is not being opened then seqLength will still be 0 when line 63 of the code in the OP is reached, which means nothing will be written to the output file.

That seems the most likely source of your problem.
How would I be able to check to see if the file is being opened? (I apologize if this is a stupid question)
How would I be able to check to see if the file is being opened?


Check out: http://en.cppreference.com/w/cpp/io/basic_ifstream/is_open
Topic archived. No new replies allowed.