Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Need Help with C++

Name: Anonymous 2009-02-12 23:01

I need help with getting my code to read and output a file into ROT13.

here's what i have so far.

#include <stdio.h>
#include <ctype.h>
#include <fstream>

main(int argc, char *argv[])
{
    FILE *input = stdin;
    char *prog = argv[0];
    int ch;

    printf("ROT13 Encoder\n");

    if(argc == 2)
        if((input = fopen(*++argv, "r")) == NULL)
        {
        fprintf(stderr, "ERROR: %s can not open file %s.n", prog, *argv);
            return 1;
        }

        while((ch = fgetc(input)) != EOF)
        {
            if(isalpha(ch))
            {
                if(toupper(ch) < ('A' + 13))
                    putchar(ch+13);
                else
                    putchar(ch-13);
            }
            else
               putchar(ch);
        }
        fclose(input);

        return 0;
}

Name: Anonymous 2009-02-12 23:11

>>1
It's probably because you're putting newlines before your {'s.

Name: Anonymous 2009-02-12 23:38

tr a-zA-Z n-za-mN-ZA-M <text.file >rot13.file

Name: Anonymous 2009-02-12 23:40

>>2
Don't listen to him. Allman style is the standard.

Name: Anonymous 2009-02-12 23:42

That's fucking C, not C++.

Name: Anonymous 2009-02-12 23:50

IF YOU WANT HELP TELL US WHAT IT'S DOING WRONG FAGGOT.  I DON'T WANT TO READ YOUR PIG DISGUSTING CODE TO BEGIN WITH.

Name: Anonymous 2009-02-12 23:58

Well, first of all, you're missing your [code] tags.

Without those, I CAN'T HELP YOU!

Name: Anonymous 2009-02-13 0:03

I'm trying to get it to read a file and output a file but I don't know how to.

OP here btw.

Name: Anonymous 2009-02-13 0:06

>>8
What is the point in trying to do this when it's a one-liner with a program designed for the task?

>>3 here btw.

Name: Anonymous 2009-02-13 0:14

Assignment.

I hate c++ i'm used to python. I just want this over with. It has to read a file and rot13 it and output it.

Name: Anonymous 2009-02-13 0:20

Assignment.
do your own homework, faggot.

Name: Anonymous 2009-02-13 0:23

Is this what you need, OP?


#include <ctype.h>
#include <stdio.h>

#define OPEN_OR_FAIL(FILE,NAME,MODE) \
    if(!(FILE = fopen(NAME, MODE))) \
    { \
        fprintf(stderr, "ERROR: %s cannot open file %s\n", prog, NAME); \
        return 1;\
    }

int main(int argc, char *argv[])
{
    FILE *in   = stdin;
    FILE *out  = stdout;
    char *prog = argv[0];
    int ch;

    /* If an input file is specified, use it. */
    if(argc > 1)
        OPEN_OR_FAIL(in, argv[1], "rb");

    /* If an output file is specified, use it. */
    if(argc > 2)
        OPEN_OR_FAIL(out, argv[2], "wb");

    /* ROT-13 the input. */
    while((ch = fgetc(in)) != EOF)
    {
        if(isalpha(ch))
            ch += toupper(ch) < ('A' + 13) ? 13 : -13;
        fputc(ch, out);
    }

    fclose(in);
    fclose(out);

    return 0;
}

Name: Anonymous 2009-02-13 0:23

Assignment.
/prog/ is not here to write your homework for you, faggot.

Name: Anonymous 2009-02-13 0:41

you made my life.

Name: Anonymous 2009-03-06 5:56


argv int argc char.

Name: Anonymous 2011-02-04 19:53

Don't change these.
Name: Email:
Entire Thread Thread List