My code:
#include #include #include #include #include
using namespace std;
int main() { ifstream file; string filename, str, word; int Lcount=0, Wcount=0, Icount=0, BLcount=0, i=0; bool isInt = false;
cout > filename;
file.open(filename.c_str());
if(!file) { cerr
getline(file, str); while(!file.eof()) { Lcount++; if(str.length()==0 || str==" ") { BLcount ++; getline(file, str); continue; } str += " "; istringstream ss(str); ss >> word; while(ss.good()){ Wcount++; i=0; if(i==0 && (isdigit(word.at(0) || word[0]== '-' || word[0] == '+'))){ isInt = true; i++; } while(i>word; } getline(file, str); } cout return 0; }
The code is working but I have to upload it through self-check software and it is not giving me any credit because the output has to be exact. Can someone help with that, please?
RESULT: case3 [ 0 / 1] RUNNING case4 Enter Filename: Enter the name of a file to read from: Total Number of Lines: 6 Number of non-blank lines: 4 Number of Words: 5 Number of Integers: 3 1c1,2 Enter the name of a file to read from: > Total Number of Lines: 6 4c5 Number of Integers: 5 RESULT: case4 [ 0 1] RUNNING case5 Enter Filename: Enter the name of a file to read from: Total Number of Lines: 9 Number of non-blank lines: 6 Number of Words: 10 Number of Integers: 0 1c1,2 Enter the name of a file to read from: > Total Number of Lines: 9 RESULT: case5 ( 0 / 1] RUNNING case6 Enter Filename: Enter the name of a file to read from: Total Number of Lines: 11 Number of non-blank lines: 5 Number of Words: 16 Number of Integers: 5 1cl, 2 Enter the name of a file to read from: > Total Number of Lines: 11 4c5 Number of Integers: 7 RESULT: case6 [ 0 / 1 ] SUMMARY: RESULT: compiles [1/1] RESULT: casel [ 0 / 1] RESULT: case2 [ 0 / 1] RESULT: case3 [ 0 / 1 ] RESULT: case4 [ 0 / 1] RESULT: case5 [ 0 / 1 ] [ Executing 'RUN'...] Write a C++ program that reads lines from a file until the end of file. The program should prompt the user for the file name to read from. The program should open the file for reading, and if the file cannot be opened, print the message "File could not be opened", followed by a space and the filename, and exit. The program should keep track of the number of lines, the number of non-blank lines, the number of words, and the number of integers read from the file. An integer is defined as a one or more digits, with an optional sign preceding the number, with no spaces separating the sign and the number. For example, 17234, -354, and +639 are integers, but 17.234, -+23 are not. A word is defined as a sequence of one or more non-whitespace characters separated by whitespace. At the end of file, the program should print out the total number of lines, the number of non-blank lines, the number of words, and the number of integers. Note that a line having only whitespace characters is a non-blank line as well. For example, given the following file, staci james + George - +123 12.56 smith beth -4.75 Note: There are 9 lines in this input file. the generated output results after reading the file contents are as follows: Total Number of Lines: 9 Number of non-blank lines: 5 Number of Words: 10 Number of Integers: 0 Note: The screen pointer is at a new line after displaying the results. You have to apply the same format in order to have exact match. ccc_v1_w_e5429_17437@runweb1:-$ [ Executing 'RUN'... ] ccc_v1_w_e5429_17437@runweb1:~$ ccc_v1_w_e5429_17437@runwebl:-$ cd; VOC_SELECTED_FILE="RA2_Faizan_Alam.cpp" /bin/ SUCCESS! RESULT: compiles [1 / 1] RUNNING casel Enter Filename: File could not be opened infilel 1c1, 2 Enter the name of a file to read from: > File could not be opened infilel No newline at end of file RESULT: casel [ 0 1 1 ] RUNNING case2 Enter Filename: Enter the name of a file to read from: Total Number of Lines: 0 Number of non-blank lines: 0 Number of Words: 0 Number of Integers: 0 1c1,2 Enter the name of a file to read from: > Total Number of Lines: 0 RESULT: case2 [ 0 / 1] RUNNING case 3 Enter Filename: Enter the name of a file to read from: Total Number of Lines: 5 Number of non-blank lines: 3 Number of Words: 0 Number of Integers: 0 1c1,2 Enter the name of a file to read from: > Total Number of Lines: 5