Hi,

I have to write a program which makes changes and writes to a different text file. The point at which I am stuck is that I am unable to add the value 10 to ASCII value of each of the first 6 characters in the file.

Can someone tell em where I am going wrong?

//
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <string>
#include <cctype>
#include <fstream>


using namespace std;
void changes(ifstream& in_stream, ofstream& out_stream);

int main()

{


ifstream fin;

ofstream fout;

cout << "Begin editing files.\n";

fin.open("TextIn.txt");
if (fin.fail( ))
{
cout << "Input file opening failed.\n";
system ("pause");
exit(1);
}

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

changes(fin, fout);//Making changes to file


fin.close( );
fout.close( );

cout << "End of editing files.\n";
system("pause");

return 0;
}

// Program to change underscore to spaces, uppercase to lowercase

void changes(ifstream& in_stream, ofstream& out_stream)
{
char next;



in_stream.get(next);
while (! in_stream.eof( ))
{
//Changing to lowercase
if (next >= 'A' && next <= 'Z')
next += 'a' - 'A';

//Changing underscores to spaces
if (next == '_')
out_stream << " ";
else
out_stream.put(next);

// Adding the value 10 to each character

for(char next=0; next<=6; next++)
{
next=+10;
}

//Getting the next character
in_stream.get(next);

}

}

Recommended Answers

All 10 Replies

next=+10;

Notice something wrong with this operator?

yeah surely must be something wrong there...but can u clarify what...i already tried all different combinations....

It's supposed to be '+='.

I had tried that before. However, that still doesn't change the data in the file. I am actually trying to decrypt a data in the TextIn file to the TextOut file.

for(char next=0; next<=6; next++)
{
next=+10;
}

So you create a new char called next (presumably shadowing the one you already had called next, so you're not changing that one at all), and then add ten to it, at which point the value is greater than 6 so the for loop ends after one iteration only.

commented: Oh yeah. You're right. I didn't see the shadow. :) +13

Oh, I didn't see the shadow. Good catch.

Thanks I changed and defined a new character for that...but it still not adding 10 to the ASCII value of the characters in the TextIn file.

This code reads a text file a line at a time, increments each char by 10, and writes the line back to a new file. Line endings are preserved. It should be enough to demonstrate a working method for you. Your code fetches a char at a time, so it's not a perfect match.

#include <iostream>
#include <fstream>
#include <string>

int main()
{
  std::ifstream inputFile;
  inputFile.open("a.txt");
  std::ofstream outputFile("b.txt");
  
  
  std::string someString;

  while (inputFile.good())
    {
      getline(inputFile, someString);
      if (inputFile.good())
      {
        int length = someString.length();
        for (int i=0; i<length; i++)
	  { someString[i]=someString[i] + 10;}
	      outputFile << someString << std::endl;
	  }
      }

  return 0;
}

Sample input:
beans on toast
and spam!


Corresponding output:

lokx}*yx*~yk}~**
kxn*}zkw+

Hi,

Thanks A LOT for your help. I kinda understand it better now. I did include your way in my program by making a function called Decrypt. However I don't think I defined the function right as it is giving me a syntax error. Can anyone help me please!

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <string>
#include <cctype>
#include <fstream>


using namespace std;


void changes(ifstream& in_stream, ofstream& out_stream);
void Decrypt(ifstream& in_stream, ofstream& out_stream);

int main()

{


      ifstream fin;
	  
    ofstream fout;

    cout << "Begin editing files.\n";

	fin.open("TextIn.txt");
    if (fin.fail( ))
    {
        cout << "Input file opening failed.\n";
		system ("pause");
        exit(1);
    }

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

    changes(fin, fout);//Making changes function
	Decrypt(fin, fout);//Decrypting data

    fin.close( );
    fout.close( );

    cout << "End of editing files.\n";
	system("pause");
    
return 0;
}

// Program to change underscore to spaces, uppercase to lowercase

void changes(ifstream& in_stream, ofstream& out_stream)
{
    char next;

		

    in_stream.get(next);
	    while (! in_stream.eof( ))
    {
		
		// Changing digits to asterix
			if(isdigit(next))
				next= '*';

		//Changing to lowercase
		    if (next >= 'A' && next <= 'Z')
				next += 'a' - 'A';

		//Changing underscores to spaces
        if (next == '_')
            out_stream << " ";
        else
            out_stream.put(next);

		

		//Getting the next character
        in_stream.get(next);
		    }
}

// Adding the value 10 to each character
void Decrypt(ifstream& in_stream, ofstream& out_stream)
{
 
std::string TextInChar;
 
while (in_stream.good())
{
getline(in_stream, TextInChar);
if (in_stream.good())
{
int length = TextInChar.length();

for (int i=0; i<length; i++)
{ TextInChar[i]=TextInChar[i] + 10;}
out_stream << TextInChar << std::endl;
}
}

What is the syntax error you are getting?

I suspect it may have something to do with unexpected end of file in function definition. Take another look at your braces. I see 4 opening braces '{' in your Decrypt() function, but I only see 3 closing braces '}'.

This is why you have to pay attention to your formatting. If you had made proper use of whitespace, you would probably have seen it yourself.

// Adding the value 10 to each character
void Decrypt(ifstream& in_stream, ofstream& out_stream)
{
 
  std::string TextInChar;
 
  while (in_stream.good())
  {
    getline(in_stream, TextInChar);
    if (in_stream.good())
    {
      int length = TextInChar.length();
 
      for (int i=0; i<length; i++)
      { TextInChar[i]=TextInChar[i] + 10;}
      out_stream << TextInChar << std::endl;
    }
  }
        //<----- something is missing here...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.