Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to input a file into C++ and comparing console input
    primarykey
    data
    text
    <p>Im working on my homework assignment and I stuck because in the assignment we have to ask the user to enter a file name but also to type in either wc cc or lc (word count, character count, and line count of a file. For example, wc filename.txt. Im suppose to check the file to see if its valid or not which i understand and I know how to compare the users input to determine the different kind of function to run, but I dont understand how you could do it together. Any ideas? This is what I have so far.</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;fstream&gt; using namespace std; int main() { string line; string file; ifstream input; //input file stream int i; cout &lt;&lt; "Enter a file name" &lt;&lt; endl; while(true){ cout &lt;&lt; "&gt;" ; getline(cin,file); input.open(file.c_str()); if (input.fail()) { cerr &lt;&lt; "ERROR: Failed to open file " &lt;&lt; file &lt;&lt; endl; input.clear(); } else { i = 0; while (getline(input, line)) if(line == "wc"){ cout &lt;&lt; "The word count is: " &lt;&lt; endl; } else if(line == "cc"){ cout &lt;&lt; "The character count is: " &lt;&lt; endl; } else if(line == "lc"){ cout &lt;&lt; "The line count is: " &lt;&lt; endl; } else if(line == "exit"){ return 0; } else{ cout &lt;&lt; "----NOTE----" &lt;&lt; endl; cout &lt;&lt; "Available Commands: " &lt;&lt; endl; cout &lt;&lt;"lc \"filename\"" &lt;&lt; endl; cout &lt;&lt;"cc \"filename\"" &lt;&lt; endl; cout &lt;&lt;"wc \"filename\"" &lt;&lt; endl; cout &lt;&lt;"exit" &lt;&lt; endl; } } } return 0; } void wordCount(){ //TBD } void characterCount(){ //TBD } void lineCount(){ //TBD } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload