Results 1 to 3 of 3

Thread: Homework related...

  1. #1
    Join Date
    May 2008
    Beans
    2

    Homework related...

    Hey guys, I'm currently doing a graduate certificate in IT and as part of that course i'm doing a unit on UNIX/LINUX programming.

    I am trying to come to grips with not only the linux programming environment itself, but also some specific commands. My assignment asks me to write a program which takes input from a file and outputs only the lower case letters from that file. I think I know how to do it, it's just that the syntax is driving me mad.

    Every example i find, i cant get to work. I'm using the getchar command to read the character then an if loop to determine the case of teh character and a simple printf to output the result.

    My problem is, that i cant get the getchar command to work at all, and im not even sure that im executing the command the right way.

    My course is by distance and my lecturer doesn't give much information away, which is why i'm trolling the internet for help.

    This is the code i have written to test the getchar command which i have written in a text document and saved as test.c

    #include <stdio.h>

    int main (void)
    {
    int c;

    printf("EOF has value: %d\n", EOF);

    while((c = getchar()) != EOF)
    putchar(c);

    return 0;
    }


    When I type source test.c into the terminal i get

    bash: test.c: line 3: syntax error near unexpected token `('
    bash: test.c: line 3: `int main (void)

    My question is, should this program run in this way from the terminal or is there something else i need to do? Once i can get this to work, i'll be able to answer the question, i just can't figure out the getchar syntax and how it is supposed to be run.

    Thanks,

    Craig

  2. #2
    Join Date
    Apr 2006
    Location
    Hamilton, New Zealand
    Beans
    198
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Homework related...

    Programs written in C need to be compiled before you can run them to compile something you need gcc installed which you can do by running the following command with sudo

    Code:
    apt-get install build-essential
    To compile code you need to run a command similar to this

    Code:
    gcc -o <output_name> <input_files>
    where <output_name> is the name of the program you wish to produce and <input_files> is the one or more c source files.

  3. #3
    Join Date
    Feb 2007
    Beans
    24,961
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: Homework related...

    From the Code of Conduct
    While we are happy to serve as a resource for hints and for asking questions when you get stuck and need a little help, the Ubuntu Forums should not be thought of as a homework service. Please do not post your homework assignments expecting someone else to do it for you. Any such threads will be taken offline and warnings or infractions may be issued.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •