-4

Keep getting this error when running my program. If i remove either void function it works fine, but when i try to run the complete program it gives me the Expression: (unsigned)(c+1) <= 256 error. Tried a couple of threads in this site to figure out what's going on and still can't figure it out (Something, something unsigned character?). I'm a beginner so any help would be much appreciated. Here is what I have so far:

#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;

void countUpperLower(ifstream& instream, ofstream& outstream);
void Convert(ifstream& in_stream, ofstream& out_stream);


int main()
{
    ifstream fin;
    ofstream fout;

    fin.open("CAD.dat");
    if (fin.fail())
    {
        cout << "Input file opening failed.\n";
        exit(1);
    }


    fout.open("outputs.txt");
    if (fout.fail())
    {
        cout << "Output file opening failed.\n";
        exit(1);
    }

    countUpperLower(fin, fout);
    Convert(fin, fout);
    fin.close();
    fout.close();

    cout << endl << "End of editing files.\n";
    return 0;
}

void countUpperLower(ifstream& instream, ofstream& outstream)
{
    char caps;
    int uppercase = 0;
    int lowercase = 0;
    do
    {
        instream.get(caps);
        if (isupper(caps))
            uppercase++;
        else if (islower(caps))
            lowercase++;

    } while (caps != '.');

    cout << "There are " << uppercase << " uppercase letters in this file\n";
    cout << "There are " << lowercase << " lowercase letters in this file\n";

}

void Convert(ifstream& in_stream, ofstream& out_stream)
{
    char next;
    do
    {
        in_stream.get(next);
        if (isspace(next))
            cout << '-';
        else if (isdigit(next))
            cout << '#';
        else if (isupper(next))
            putchar (tolower(next));
        else if (islower(next))
            putchar (toupper(next));
        else
        cout << next;
    } while (next != '.');
}
Mohamad Shiralizadeh
  • 8,329
  • 6
  • 58
  • 93
Kramer
  • 19
  • 1
  • What is the *exact*, *complete* error message? There is no variable called `c` in your code. – Oliver Charlesworth Nov 01 '14 at 18:45
  • A dialog box pops up and says: Debug Assertion failed! Program:.../Homework 6.exe File: f:\dd\vctools\crt_bld\self_x86crt\src\isctpye.c Line:56 Expression:(unsigned)(c + 1) <=256 – Kramer Nov 01 '14 at 18:47
  • Also, when i run the program with CTRL+F5 there is no compiler error. – Kramer Nov 01 '14 at 18:59

3 Answers3

0

What is in your text data? When a German Umlaut, for instance, or another char >0x80 is encountered, it is interpreted as a negative number, which would account for your error.

barabaskid
  • 27
  • 6
0

The code looks ok, and the problem you refer, don't see any, as there is no variable called c, so how could you get the error you mentioned? However, within Convert function, before entering do-while loop, initialize next to some value other than '.', for example,

char next = 0;
Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
0

Are you wanting to run two different processes on the entire file? If so, you're not seeking to the beginning of the file in-between your functions. You need to do something like this:

countUpperLower(fin, fout);
fin.seekg(0);
Convert(fin, fout);

Also, your output file appears to be empty.. Not sure if that was your intent. Did you mean to do out_stream << next rather than cout << next? (And others, such as -, #, etc).

Ryan Pendleton
  • 2,566
  • 19
  • 29
  • What I'm trying to do is first count the number of upper and lower case letters in the original CAD.dat file and then take that file and change all the spaces to '-' characters, all the digits to '#' characters and swap all lowercase and uppercase letters and output it to the screen after converting. I may be wrong, but I'm assuming I would need 2 separate functions to do this. You are correct about the second part, however I am leaving it as cout << next just so I can see it on the screen. Once everything works I will change that to out_stream << next. – Kramer Nov 01 '14 at 18:58
  • So after reading your comment more cloely, the problem was I was not initializing to the beginning of the file before te secon void function. I re-opened the file before the second void function and now it works. Thank you for your help! – Kramer Nov 01 '14 at 19:27
  • @Kramer Rather than reopening it, you should be able to just use `fin.seekg(0)` like in my answer. Either way, I'm glad you got it working. – Ryan Pendleton Nov 02 '14 at 22:03