Console keeps shutting down
Mar 13, 2019 at 7:27pm UTC
Write your question here.
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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
void dictionary_read(ifstream &dict);
int main()
{
ifstream infile("dna2.txt" );
if (infile.fail()) {
cerr << "File cannot be read, opened, or does not exist.\n" ;
exit(1);
} //When I remove infile2 the console stays open. So
ifstream infile2("codon.txt" ); //I am assuming there is
if (infile2.fail()) { //something wrong here
cerr << "File cannot be read, opened, or does not exist.\n" ;
exit(1);
}
string s;
string newline = "" ;
while (getline(infile, s))
{
for (int i = 0 ;i < s.length();i++)
{
if (s[i] == 'a' || s[i] == 'A' )
{
s[i] = 'U' ;
}
else if (s[i] == 'T' || s[i] == 't' )
{
s[i] = 'A' ;
}
else if (s[i] == 'C' || s[i] == 'c' )
{
s[i] = 'G' ;
}
else if (s[i] == 'G' || s[i] == 'g' )
{
s[i] = 'C' ;
}
}
dictionary_read(infile2);
}
infile.close();
infile2.close();
system("pause" );
return 0;
}
void dictionary_read(ifstream &dict) {
string key, value;
dict.clear(); // reset error state
dict.seekg(0); // return file pointer to the beginning
while (dict >> key >> value) {
cout << "key: " << key << endl;
cout << "value: " << value << endl;
}
}
Mar 13, 2019 at 7:35pm UTC
//When I remove infile2 the console stays open.
That implies your program can't open "codon.txt". If the file is not present, the exit() statement is going to close the console and return to the operating system immediately. You will never see the message from line 19.
If you want to prove that is what is happening add
systen("pause" );
between lines 19 and 20. You might want to change the text of the message so you can distinguish between file1 and file2.
Mar 13, 2019 at 7:48pm UTC
THANK YOU FOR YOUR RESPONSE!
Because of you I found my error! Thank you so much. I misspelled the file name.
Topic archived. No new replies allowed.