I/O Code

Jul 28, 2012 at 11:40am
Hello, I have created the following program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <stdafx.h>

int main() 
{ FILE * pFile; 
char mystring [100];

pFile = fopen ("encoded.txt" , "r"); 
if (pFile == NULL) perror ("Error opening file"); 
else { 
if ( fgets (mystring , 100 , pFile) != NULL )
puts (mystring); 
fclose (pFile); 
} 
return 0; 


I need to extend the program to incorporate the translate() function and displays the decoded output onto the screen.

Can anyone please help me finish this program and thank you soo much for your help.
Jul 28, 2012 at 11:43am

1
2
3
4
for (int i=0; i<100; i++)
{
  mystring[i] = translate(mystring[i]);
}
Jul 28, 2012 at 10:27pm
hi thank you for replying but it keeps failing when I try debugging it. Ive tried trouble shooting it in many ways its not working. can you please help me with this.
Jul 29, 2012 at 12:03am
Your use of indentation scares me.

Assuming you are trying to take a whole text document into a string, I've taken the liberty of making a prototype program that does this for you.
Mind the extra parenthesis at document declaration, as it's needed for proper parsing.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string>
#include <fstream>

typedef std::string doc_t;
typedef std::fstream fstream_t;
typedef std::istreambuf_iterator<char> fstream_iterator;

void translate_document( doc_t document_copy);
int main()
{
	doc_t document( (fstream_iterator( fstream_t("document.txt"))), fstream_iterator() );
	return 0;
}
Jul 29, 2012 at 3:12am
Thanx Nexius, but the code you wrote is doing nothing more then the one I wrote which is only displaying all the integers in the .txt file on the screen.

I need this program to display the decoded output using the char translate (int i) function.
Jul 29, 2012 at 9:18pm
is there no one that can please help me with this...
Jul 29, 2012 at 9:27pm
$ whatis translate
translate: nothing appropriate.

¿What's the problem with Moschops's code?
Jul 29, 2012 at 9:29pm
I keep getting this error when debugging, causing it to fail:

error C3861: 'translate': identifier not found
Jul 29, 2012 at 10:13pm
... that's a compilation error.
¿have you ever defined translate?
Jul 29, 2012 at 10:56pm
no i have never defined translate, how do you do that?
Jul 29, 2012 at 10:58pm
Time to learn about functions:

http://www.cplusplus.com/doc/tutorial/functions/

The key is that if you want to use a function called translate, then that function must have been written by someone.
Jul 29, 2012 at 11:53pm
Im sorry I dont mean to run in circles but I was on the link and I'm still a little confused. is this how you define translate():

1
2
3
4
char translate (int i)
{
return i;
}
Last edited on Jul 29, 2012 at 11:54pm
Jul 30, 2012 at 8:25am
no.

1
2
3
4
<type> functionname (<input type> local variable name)
{
    return variable of <type>
}


1
2
3
4
int doubler (int i) // this function has to return an int, and has to be given an int
{
    return i * 2; // return double of int given
}


1
2
3
4
5
int main()
{
    std::cout << doubler(2) << std::endl; // will output 4
    return 0; //0 is success, anything else is failure, main always returns an int, although your compiler will normally add this if you don't
}
Last edited on Jul 30, 2012 at 8:26am
Jul 30, 2012 at 11:42am
¿So what's his mistake? (apart that the function does nothing)
Jul 31, 2012 at 8:04am
he returned an int instead of a char, the type before the function name, although I'm not sure if that would error considering an int can be represented as a char, still unexpected.
Topic archived. No new replies allowed.