Thread: Quite a stuffed bit of code actually.

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    7

    Quite a stuffed bit of code actually.

    Ok im making a program to help build my IRC client.
    The program is designed to break up a sentence, copy them to other variables, then let me print out the variables in a fashion i choose.

    I have a few problems though. Im storing the original sentence in an array, then copying them to other arrays, but i cant set up the code so that when they user has finished entering what they want (for example, hitting the enter key) it stops scanning in characters.

    Can you please take a look at this code and see how you can imporve it.

    I will add some documentation so it makes a bit more sense.

    Thanks to anyone who bothers.

    #include <iostream.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    #include <conio.h>
    #include <windows.h>

    void main()
    {
    int asciinum, *pointer;
    char spacechar;
    int index;
    char NULLchar, ch;
    int asciinum2, *pointer2;

    //Pointers that point at the ' ' char, so i can actually parse the line //
    asciinum = 32;
    pointer = &asciinum;
    spacechar = *pointer;

    char userentry[75];

    userinputscreen:

    printf ("Enter information in the following syntax...\n\n");
    printf ("![Name] [Ident@Hostname] [Command] [Parameter]#\n");
    printf ("[Name] = Less than 8 Characters\n");
    printf ("[Ident@Hostname] = Less than 30 Characters\n");
    printf ("[Command] = Less than 12 Characters\n");
    printf ("[Parameter] = Less than 20 Characters\n");
    printf ("End the statement with a #");
    printf (":");

    //This part of the code lacks something to stop it, because i couldnt set up pointers to do it. //
    for (index = 0; index < 75; index++){
    if (index > 74){
    system("CLS");
    printf ("You have exceeded the maximum ammount of Characters..\n");
    printf ("Please re-enter your details\n\n");
    main();
    }
    scanf ("%c",&userentry[index]);

    }



    printf ("\n\n");

    printf ("\n\n");

    index = 0;
    if (userentry[index] != '!') {
    system("CLS");
    printf ("Inappropriate Syntax. ! must be before the nickname.");
    main();
    }

    char username[9];

    for (index = 0; index <8; index++){
    if (userentry[index] = spacechar){
    printf ("Space Character Found, Terminating Loop...");
    }
    userentry[index] = username[index];
    }

    //Prints out the user name, it currently prints out nothing or some general ascii crap//
    printf("\n\n...:::");

    for (index = 1; index < 7; index++){
    printf ("%c", username[index]);
    }

    printf(":::...");

    printf ("\n\n");
    system("PAUSE");
    return 0;
    }

  2. #2
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    void main()
    change this to
    int main(void)
    or prepared to be flamed .
    As I read somewhere that when someone voids a main() he inspires a thousand flames.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Mmm, there's a lot to sort out here (including the void main already pointed out).

    1. Which Language is it written in
    #include <iostream.h> // This says C++
    #include <stdio.h> // This says C
    Looking through the code, I see inline variable declarations, so its C++

    2. Which operating system is it for
    #include <conio.h> // This says DOS
    #include <windows.h> // This says windows

    > int index;
    index is a reserved identifier - it is the name of a function in string.h. If you wanted to call that function from within this code, you'd have a problem.

    > //Pointers that point at the ' ' char, so i can actually parse the line //
    > asciinum = 32;
    > pointer = &asciinum;
    > spacechar = *pointer;
    You can remove two lines, and just say
    Code:
    spacechar = ' ';
    > userinputscreen:
    The appearance of a label is never a good sign

    > //This part of the code lacks something to stop it, because i couldnt set up pointers to do it. //
    Yeah, it can be replaced by one line.
    Code:
    fgets( userentry, sizeof(userentry), stdin );
    You seem to be trying to read in a word at a time. It's better to read the whole line in one go (using fgets()), then you can parse the char array as many times as you want in order to decide what it means.

    In addition, your code has no exit condition (other than filling up the buffer), at which point if prints an error and bumps into another no-no.

    > main();
    You should never call main recursively. A while loop is more appropriate here.

    > if (userentry[index] = spacechar){
    This is an assignment, not a comparison. Use == to compare values.
    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
    Registered User
    Join Date
    Oct 2001
    Posts
    7
    I fixed the code, but the problem is copying from one array to another, as in userentry[index] = username[index];

    but it wont work, so i need some suggestions on copying from one array to another array.

    Any Ideas?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    string arrays are copied using strcpy
    like
    strcpy( userentry, username );

    Read about it in your manual pages...
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit level permutation function
    By zxcv in forum C Programming
    Replies: 2
    Last Post: 07-27-2008, 01:26 PM
  2. Linked list - why this bit of code?
    By chris1985 in forum C Programming
    Replies: 2
    Last Post: 10-04-2005, 06:17 AM
  3. Replies: 1
    Last Post: 01-10-2003, 10:08 PM
  4. Replies: 4
    Last Post: 01-16-2002, 12:04 AM