Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

int main()
{
    ifstream dataFile;
    string filename;
    char characterDrawing , symbol;

    cout<<"Please input filename: ";
    getline(cin, filename);

    dataFile.open(filename);
    if (dataFile)
    {
        dataFile >> characterDrawing;
        dataFile.ignore(20,'\n');
        if (isdigit(characterDrawing) || isspace(characterDrawing))
        {
            symbol = '#';
            cout<<symbol;
        }
        else
        {
            symbol = characterDrawing;
            cout<<symbol;
        }

        dataFile.close();

    }
   else
    {
       cout<<"Error opening the file.\n";
    }
    return 0;
}


What I have tried:

All the input from text file is correctly pass into program but when i add character testing code that is line
if (isdigit(characterDrawing) || isspace(characterDrawing))
 {
     symbol = '#';
     cout<<symbol;
 }
 else
 {
     symbol = characterDrawing;
     cout<<symbol;
 }

the input from text file is different from what it actually need to input. How to solve it?
Posted
Updated 19-Sep-19 21:55pm

We can't tell - we have no idea what you expect the data file to produce, and we have no access to your data to find out what it actually is doing!

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
I don't know what your program is supposed to do, but what it does, now, is try opening a file, and, if successful, read the very first character of that file. The only thing that could go wrong, is that the input file can't be found, or opened. You could use the debugger (see solution 1) to find out if that's what's happening.

Your call to datafile.ignore() has no effect on the output of your program. Maybe you forgot to read what is past that position in your file? Or did you mean to read that character afterwards, instead of before calling ignore()?
 
Share this answer
 
Comments
Member 14598404 20-Sep-19 13:07pm    
I solved the problem. However, new problem comes and debugger do not have error. The output is error opening file.
Stefan_Lang 23-Sep-19 2:57am    
If this a call for help, you should provide more info. Since you obviously changed the code to resolve your problem, you should show the current code, at least the part immediately before and up to the line that causes the error. Then it would be helpful to know the exact wording of the error you got.

Remember, we're not in your head, we don't see what you see, and on top of that we may not interpret that which you see in the same manner. therefore you need to provide us with hard data.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900