Thread: Its a simple program i know But??

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    13

    Red face Its a simple program i know But??

    I use Turbo C++.
    Please can you compile and put right this program.
    Not sure if it's me or my compiler thanks.

    #include <stdio.h>

    int main(int count, char *strings[]) {
    char *p1, *p2;
    int i = 0;

    if(count != 3) {
    printf("Usage: alpha <start char> <end char>\n");
    exit(-1);
    }

    if(*strings[1] < 65 || *strings[1] > 90 || *strings[2] < 65
    || *strings[2] > 90 || *strings[2] < *strings[1]) {
    printf("Please input uppercase characters from A-Z, with the ");
    printf("second one coming after the first one alphabetically.");
    printf("\n");
    exit(-1);
    }

    p1 = (char *)malloc(1);
    p2 = (char *)malloc(1);

    memcpy(p1, strings[1], sizeof(char));
    memcpy(p2, strings[2], sizeof(char));

    while((*p1 + i) <= *p2){
    printf("%c", (*p1 + i));
    i++;
    }

    printf("\n");
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    1

    Cool

    hmmz, I added these 3 header files & it "worked". It was a bit of trial & error
    (need to learn this again).


    #include <malloc.h>
    #include <memory.h>
    #include <stdlib.h> // for the exit function..
    #include <stdio.h>

    and I also added a "return 0;" before the last '}', so main now returns a value

    -->
    exit()
    The function exit() is prototyped in #include <stdlib> by:

    void exit(int status)
    [EDIT]using MSVC++ 6.0 btw[EEk!-EDIT]

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I only needed stdlib.h (it contains malloc as well as exit)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > still stops befoe it supposed to ask for input letters.
    Because you're supposed to type the letters on the command line when you run the program

    Eg.
    a.exe B F

    Produces this output
    BCDEF

    There is no input when the program is running - if it's not on the command line, the program just exits.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    13

    Talking Hmmm

    All i get when I run is inactive in viewer window, in otherwords I press run and the program halts with:

    Usage:alpha <Start Char> <End Char>.
    Can't do anything but watch it on screen.
    Am I cracking up

    Is there a way Someone could change it so the user inputs the letters??
    Can't get my head around it.
    Thanks
    Last edited by andy bee; 09-10-2001 at 06:49 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you're running this within some IDE, then there is usually some "Run options" menu which allows you to specify the command line parameters you want to pass to the program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    13

    Thumbs up Problem e.g errors solved

    Ok guys i have now got the code to work as it should.
    Thanks for your valuable help.
    (Learn sumut new every day).



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM