Read in data file!
Need Help Reading in this data file! My output is a black screen!
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
#include <iostream>
#include <cctype>
#include<stdio.h>
#include<iterator>
#include<fstream>
using namespace std;
bool NUM;
int main()
{
std::ifstream myReadFile;
myReadFile.open("C:/Users/marqu_000/Documents/txt.data");
char ch[80];
if (myReadFile.is_open());
while (!myReadFile.eof());
int i, numbers = 0, other = 0, character = 0, letters = 0, spaces = 0, punct = 0, digits = 0, special = 0, upper = 0, lower = 0;
cout << "\nEnter The String:\n " << endl;
gets (ch);
bool NUM = false ; //initialises to false
for (i =0; ch[i] !='\0';i++)
{
if (ch[i] != ' ')
character++;
if(isalpha(ch[i]))
++letters;
else if(isspace(ch[i]))
++spaces;
//else if(ispunct(ch[i]))
// ++punct;
if (ch[i] ==',')
++punct;
else if (ch[i] ==';')
++punct;
else if (ch[i] =='.')
++punct;
else if (ch[i] ==':')
++punct;
else if (ch[i] =='!')
++punct;
else if (ch[i] =='?')
++punct;
else if (ch[i] =='"')
++punct;
else if (ch[i] =='/')
++punct;
else if (ch[i] =='-')
++punct;
else if (ch[i] =='+')
++punct;
else if (ispunct(ch[i]))
++special;
else if(isdigit(ch[i]))
++digits;
++ch[i];
if (ch[i] >= 'A' && ch[i] <= 'Z')
upper++;
if (ch[i] >= 'a' && ch[i] <= 'z')
lower++;
if (isdigit(ch[i]) == true)
NUM++;
if (ch[i] == ' ')
++other;
}
cout << "Number of characters: " << character << endl;
cout << "Number of letters: " << letters << endl;
cout << "Number of lower cases : " << lower << endl;
cout << "Number of upper cases : " << upper << endl;
cout << "Number of digits: " << digits << endl;
cout << "Number of punctuation characters: " << punct << endl;
cout << "Number of special characters: " << special <<endl;
cout << "Number of spaces: " << spaces << endl;
cout << "Number of words: " << spaces + 1 << endl;
cout << "Number of numbers " << NUM;
cout << "\nNumber of others " << other;
myReadFile.close();
return 0;
}
|
Last edited on
Line 17:
|
if (myReadFile.is_open());
|
The body of the if statement is empty (just a semicolon).
Line 18:
|
while (!myReadFile.eof());
|
Another null statement represented by the semicolon. Hence this line gives an infinite loop, execution of the program never gets past this point.
It is equivalent to this:
1 2 3 4
|
while (!myReadFile.eof())
{
; // a single empty statement which does nothing.
}
|
In addition, later in the program there are no statements to read input from the file.
Here's how to check the file is open, and read it line by line:
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
|
#include <iostream>
#include <cctype>
#include <cstdio>
#include <iterator>
#include <fstream>
using namespace std;
int main()
{
ifstream myReadFile("C:/Users/marqu_000/Documents/txt.data");
if (!myReadFile)
{
cout << "Input file is not open\n";
return 1;
}
char ch[80];
while ( myReadFile.getline(ch, sizeof(ch) ))
{
cout << "Line = " << ch << '\n';
}
}
|
By the way, don't use eof() as the condition of a loop, it doesn't work as you expect.
Topic archived. No new replies allowed.