HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

User input and reading contents of file

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
readingfileusercontentsinputand

Problem

For full disclosure: this is an assignment for my programming class and I just want tips or advice on some of the code.

Assignment details just for background information purposes:


Write a program that will read a number 1-100 from the user, and the
name of a data file, and will tell the user what word is in the file
and how many times the numbers shows up in the data file. Validate
input number (keep asking until valid) and validate the file was
successfully open.


text file contents: Darling 10 20 21 19 20

#include 
#include 
#include 
#include   // Needed for exit()
using namespace std;

int main()
{
    ifstream inputFile;
    string fileName;
    int value;

    cout > value;

    if (value >= 1 && value > value;
    }

    string word;
    int number;
    int count = 0;

    infile >> number;

    // Read a file until you've reached the end
    while (!inputFile.eof())
    {
        if (number == x)
        {
            sum1++
        }
        infile >> number;
    }
    cout > word;
        if (word == "input")
        {
            count++;
        }
    }

    // Close the file
    inputFile.close();
    return 0;
}


Any tips or advice would be greatly appreciated since I'm lost when it comes to the fstream portion of this assignment right now.

UPDATED

#include 
#include 
#include 
#include   // Needed for exit()
using namespace std;

int main()
{
    ifstream inputFile;
    string fileName;
    int value, x;

    cout > value;

    do
{
    cout > value;

} while (value  100);

    string word;
    int number;
    int count = 0;

    infile >> number;

    // Read a file until you've reached the end
    while (!inputFile.eof())
    {
        if (number == x)
        {
            sum1++
        }
        inputFile >> number;
    }
    cout > word;
        if (word == "input")
        {
            count++;
        }
    }

    // Close the file
    inputFile.close();
    return 0;
}

Solution

-
Despite what you may have been taught, you should not get into the habit of using using namespace std, especially when you start writing larger programs.

-
Since you're in main(), you should just return 1 instead of calling exit(1). The latter is only necessary in other places from which you will need to terminate early.

-
You don't need to display an output if the file was successfully opened. You'll know it failed if the program terminates after asking for a filename.

-
Do not use std::eof() for detecting the end of the file. It will look for the eof bit after reading the end of the stream (and will not even consider errors in reading (i.e. bad bit)). Read this for more information.

Instead, read from the file and check for the bad bit using std::getline():

while (std::getline(std::cin, line))
{
    // ...
}


-
You ask the user for input, but you only validate it once. If the user enters an invalid value a second time, the program will continue, causing problems. All of that should be in a loop that will terminate once the user provides valid input.

do
{
    std::cout > value;

} while (value  100);


-
You don't need return 0 at the end of main(). Reaching the end already implies a successful termination, so the compiler will do this return for you.

Code Snippets

while (std::getline(std::cin, line))
{
    // ...
}
do
{
    std::cout << "Pick a number between 1 through 100. ";
    std::cin >> value;

} while (value < 1 || value > 100);

Context

StackExchange Code Review Q#46946, answer score: 5

Revisions (0)

No revisions yet.