Inputs from users...

This is a discussion on Inputs from users... within the C Programming forums, part of the General Programming Boards category; Consider the following situation: Code: printf("Please enter a number: "); scanf("%d",&num"); Problem with that ^^^.....The user (who is stupid) will ...

  1. #1
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129

    Inputs from users...

    Consider the following situation:

    Code:
    printf("Please enter a number: ");
    scanf("%d",&num");
    Problem with that ^^^.....The user (who is stupid) will and I knw, WILL, put in letters when it specifically asks for numbers.

    So my question...Is there anyway to force the user into only entering numbers besides using a loop until the user puts an int??

    Kinda like making all other keys on the keyboard useless except the numbers...and things like +,-, . ?

    Any ideas....a simple way that can use C standard libraries....

  2. #2
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129

    Questions,Question,Questions.....

    Is there a way to copy a whole structure into another structure of similar members...

    Code:
    typedef struct{
          char a[50];
          float b;
          char c[50];
          int d;
          char e[50];
    }mynotsocrazystructure;
    to

    Code:
    typedef struct{
          char a[50];
          float b;
          char c[50];
          int d;
          char e[50];
    }mycrazystructure;
    ???

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is there anyway to force the user into only entering numbers besides using a loop until the user puts an int??
    No, you have no control over the user. You do have control over what input you accept though, so something like this would be what you want:
    Code:
    while ( fgets ( s, sizeof s, stdin ) != NULL ) {
      if ( sscanf ( s, "%d", &num ) == 1 )
        break;
      fprintf ( stderr, "Invalid input\n" );
    }
    >Is there a way to copy a whole structure into another structure of similar members...
    Not safely. The two structures may have the same members, but that doesn't mean they have the same padding or alignment. As such, the only way to assign a structure variable of an equivalent type (as opposed to the same type) is to use a cast and hope for the best.
    My best code is written with the delete key.

  4. #4
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    So...

    This is my stucture...and the following is the array:
    Code:
    typedef struct{
          char name [50];
          float price;
          int noItems;
          char expiry[50];
          char type[50];
          char detail[50];
    }structure;
    .
    .
    .
    .
    structure inventory[100];
    WHat I am trying here is to copy each member of the last structure in the array to the first one in the array, considering both strucutres are populated with data.

    arrayL = last structure in array.
    num = first structre in array.
    Code:
        strcpy(inventory[num].name,inventory[arrayL].name);        
        (inventory[num].price) = (inventory[arrayL].price);
        (inventory[num].noItems) = (inventory[arrayL].noItems);
        strcpy(inventory[num].expiry,inventory[arrayL].expiry); 
        strcpy(inventory[num].expiry,inventory[arrayL].expiry); 
        strcpy(inventory[num].expiry,inventory[arrayL].expiry);
    I get a bunch of errors though:
    "89: invalid types `structure*[int*]' for array subscript"

    EDIT: I dont want the data in inventory[num]....

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >WHat I am trying here is to copy each member of the last structure in the array to the first one in the array
    This is entirely different. Arrays in C are homogeneous, so the first and last elements are guaranteed to be compatible types. This means your structure array idea is valid for assignment:
    Code:
    inventory[num] = inventory[arrayL];
    Of course, you will lose the contents of inventory[num] unless you save them first. For example, if you wanted to swap the two elements:
    Code:
    structure save = inventory[num];
    inventory[num] = inventory[arrayL];
    inventory[arrayL] = save;
    My best code is written with the delete key.

  6. #6
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    But when i try:

    >strcpy(inventory[num].expiry,inventory[arrayL].expiry);

    I get errors...
    "89: invalid types `structure*[int*]' for array subscript"

    edIT:
    Code:
     strcpy(inventory[num].name,inventory[arrayL].name);        
        (inventory[num].price) = (inventory[arrayL].price);
        (inventory[num].noItems) = (inventory[arrayL].noItems);
        strcpy(inventory[num].expiry,inventory[arrayL].expiry); 
        strcpy(inventory[num].expiry,inventory[arrayL].expiry); 
        strcpy(inventory[num].expiry,inventory[arrayL].expiry);
    ^^^^ gives me :

    "87: invalid types `structure*[int*]' for array subscript"
    "88: invalid types `structure*[int*]' for array subscript"
    "90: invalid types `structure*[int*]' for array subscript"
    "91: invalid types `structure*[int*]' for array subscript"
    "92: invalid types `structure*[int*]' for array subscript"
    "93: invalid types `structure*[int*]' for array subscript"

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I don't get any errors with my test code. Post a working program that exhibits the problem. Preferably something cut down to the bare bones so that the problem is easier to locate.

    [edit]
    Nevermind. If I recall correctly, arrayL is a pointer. Your error suggests this as well, so try dereferencing it.
    [/edit]
    My best code is written with the delete key.

  8. #8
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    Noted....

    I have been using system("pause")....but it prints a message with the pause....is there way to not have the message...

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >is there way to not have the message...
    Yes, write your own pause function that does not have a message. This steps outside of the C standard though as you need a way to get raw input without pressing enter.
    My best code is written with the delete key.

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by Prelude
    >is there way to not have the message...
    Yes, write your own pause function that does not have a message. This steps outside of the C standard though as you need a way to get raw input without pressing enter.
    In other words, use getchar();
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  11. #11
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    Originally posted by WaltP
    In other words, use getchar();
    getchar ??? ....i dont want the user to be able to type anything on screen whil ethe program is paused...

    EDIT: QUESTION. How long have you been guy programming in C for??

    5 months for me...

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >In other words, use getchar();
    No, in other words, use some form of getch. getchar allows the user to enter more than just a single keystroke. It can have the right effect if the restriction the user press enter to end the pause instead of any key at all and it doesn't matter that anything else is echoed to stdout.
    My best code is written with the delete key.

  15. #15
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    Let me work on that and i'll post if i'm succesful...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I validate these inputs?
    By Kyeong in forum C Programming
    Replies: 1
    Last Post: 10-19-2008, 02:20 PM
  2. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 07:18 AM
  3. Finding users on a network and messaging them.
    By Necrodeemer in forum C++ Programming
    Replies: 3
    Last Post: 05-04-2003, 02:04 PM
  4. Windows ntfs perms
    By wp_x in forum Tech Board
    Replies: 3
    Last Post: 03-04-2003, 05:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21