Will this work in Old Copliers scanf(" %[A-za-z. ]",Sting[counter]);

This is a discussion on Will this work in Old Copliers scanf(" %[A-za-z. ]",Sting[counter]); within the C Programming forums, part of the General Programming Boards category; In class my teacher grades our assignment with Turbo C++ the old version like the DOS one just wondering...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    20

    Will this work in Old Copliers scanf(" %[A-za-z. ]",Sting[counter]);

    In class my teacher grades our assignment with Turbo C++ the old version like the DOS one just wondering

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, it should work. However, the teacher may not understand it !

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,594
    It's not a good scanf, because it relies on ANSI or other charsets that are similar.
    It also lacks any type to read.
    Will it be used to read strings?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    20
    yes It will be?? I did so I could put space in the Word

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,594
    You failed to specify the "s", the buffer size and relied on typical ANSI charsets.
    More info and solutions here:
    https://apps.sourceforge.net/mediawi...tle=Scanf_woes
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    20
    Will it work tho???

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,594
    I'm tempted to say... "no".
    Do it right or not at all.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Demipimp View Post
    Will it work tho???
    Define "work"... It is unlikely that Turbo C will work with anything other than ANSI/ASCII characters in it's standard C library functions. Then it comes to "what happens if the user types in 1000 characters when you wanted 30", which is a "fault tolerance matter", and it may not be part of your assignment to make your code "resilient to abuse/misuse".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is an example from Turbo C/C++ version 1.01's help menu. Note the block of code in blue.

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void)
    {
       char label[20];
       char name[20];
       int entries = 0;
       int loop, age;
       double salary;
    
       struct Entry_struct
       {
          char  name[20];
          int   age;
          float salary;
       } entry[20];
    
    /* Input a label as a string of characters restricting to 20 characters */
       printf("\n\nPlease enter a label for the chart: ");
       scanf("&#37;20s", label);
       fflush(stdin);  /* flush the input stream in case of bad input */
    
    /* Input number of entries as an integer */
       printf("How many entries will there be? (less than 20) ");
       scanf("%d", &entries);
       fflush(stdin);   /* flush the input stream in case of bad input */
    
    /* input a name restricting input to only letters upper or lower case */
       for (loop=0;loop<entries;++loop)
       {
          printf("Entry %d\n", loop);
          printf("  Name   : ");
          scanf("%[A-Za-z]", entry[loop].name);
          fflush(stdin);  /* flush the input stream in case of bad input */
    
    /* input an age as an integer */
          printf("  Age    : ");
          scanf("%d", &entry[loop].age);
          fflush(stdin);  /* flush the input stream in case of bad input */
    
    /* input a salary as a float */
          printf("  Salary : ");
          scanf("%f", &entry[loop].salary);
          fflush(stdin); /* flush the input stream in case of bad input */
       }
    
    /* Input a name, age and salary as a string, integer, and double */
       printf("\nPlease enter your name, age and salary\n");
       scanf("%20s %d %lf", name, &age, &salary);
    
    
    /* Print out the data that was input */
       printf("\n\nTable %s\n",label);
       printf("Compiled by %s  age %d  $%15.2lf\n", name, age, salary);
       printf("-----------------------------------------------------\n");
       for (loop=0;loop<entries;++loop)
          printf("%4d | %-20s | %5d | %15.2lf\n",
             loop + 1,
             entry[loop].name,
             entry[loop].age,
             entry[loop].salary);
       printf("-----------------------------------------------------\n");
       return 0;
    }
    Note that flushing stdin has been deprecated, as has conio.h. Your first z in the scanf() call is lowercase, and should be uppercase.
    Last edited by Adak; 12-08-2008 at 04:50 AM.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by matsp View Post
    Then it comes to "what happens if the user types in 1000 characters when you wanted 30", which is a "fault tolerance matter", and it may not be part of your assignment to make your code "resilient to abuse/misuse".
    What matsp means is that you can specify a maximum length for the input in scanf, so that you don't get a buffer overflow:
    Code:
    char input[64];
    scanf("%63[A-Za-z. ]", input);
    This way, if the user enters more than 63 characters, that input will be truncated. 64 is for the null terminator \0.

    I don't understand what Elysia means by there not being a conversion specifier, I had thought [ ] is a conversion specifier (for strings). I don't know where you would stick an "s" in here to make it better...altho unless Adak has hatched some bizarre deception, the OP has nothing to worry about anyway.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,594
    Quote Originally Posted by MK27 View Post
    ...I had thought [ ] is a conversion specifier (for strings). I don't know where you would stick an "s" in here to make it better...
    Maybe it is. scanf is just too complex to know perfectly.
    Besides, I do not use scanf at all.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The Bludstayne Open Works License
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-26-2003, 10:05 AM
  2. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

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