Thread: Problems with array of strings

  1. #31
    Registered User
    Join Date
    Mar 2008
    Posts
    21
    Hello,

    I would like to ask how could I check if the input is right:
    the program asks user to input a float from a keybord, but user inputs a char. Is there any funcion which can check that?

    Thanks in advance.

  2. #32
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The most common way is to read a string with fgets, then convert to float using strtod, and check the end pointer argument to see where the function stopped parsing. If it stopped parsing right away (the start is the same as the end), then it means it's an invalid input.
    You can see documentation for strtod.
    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.

  3. #33
    Registered User
    Join Date
    Mar 2008
    Posts
    21
    Thanks for an idea, but therefore I've faced another problem. I decided to use isdigit to check if the inputed number is not a char and if it isn't-then convert it with atof or atoi.

    But then i started to use these funcions, the for loops began working incorrectly: they are being repeated more times than the num value is, for example if num=3, each loop is repeated10 times and so on.

    Do you have an idea how could I correct this to work properly?

    Code:
       char t;
    
                   for(i=0;i<num;i++)
                        {
    		char* p;
    		printf("Enter string nr.&#37;d\n",i+1);
    		fgets(string[i], sizeof string[i], stdin);
    		p = strchr(string[i],'\n');
    		if(p) *p = '\0';
    	     }
    
                    for(i=0;i<num;i++)
    	{ 
                           printf("Enter float nr.%d \n",i+1);
    	               fgets(&t, sizeof &t, stdin);
    	               if (isdigit(t)) floatt[i]=atof(&t);
    	               else {printf("Error");return 0;}
                     }
    
     
                   for(i=0;i<num;i++)
                     {	
                         printf("Enter integer nr.%d \n",i+1);
    	             fgets(&t, sizeof &t, stdin);
    	             if (isdigit(t)) integer[i]=atoi(&t);
    	             else {printf("Error");return 0;}
    	}
    Last edited by CactusC; 04-07-2008 at 02:53 AM.

  4. #34
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are aware that there are valid floating point numbers that do not start with a digit:
    Code:
    +1.2
    -1.2
    .01
    Above are three examples of such numbers.

    By using strtod(), you can make a better check if the number is valid (and completely accepted - you then can decide if "123.4A" is a valid number or not).

    --
    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.

  5. #35
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't know how string is defined, but this is absolutely wrong:
    Code:
    fgets(&t, sizeof &t, stdin);
    if (isdigit(t)) floatt[i]=atof(&t);
    When working with C-strings, you need arrays, not a single char. Never. All C-strings contains one byte for each character + one character for NULL. So when reading that char, fgets write the char + NULL, thus buffer overrun. The same for atoi - it also expects a NULL char, and does a buffer overrun looking for it.
    http://cpwiki.sf.net/Buffer_overrun

    But you seriously need some indenting lessons too. Here are some reasources:
    http://cpwiki.sf.net/Indentation
    http://cpwiki.sf.net/User:Elysia/Indentation
    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. #36
    Registered User
    Join Date
    Mar 2008
    Posts
    21
    Than you. I've changed
    Code:
    char t;
    to
    Code:
    char t[100];
    and nows everything is working in a right way

  7. #37
    Registered User
    Join Date
    Mar 2008
    Posts
    21
    I would like to make a program, which does smth when one of the arrows (of keyboard) is pressed. So I would like to ask which funcion should I use (scanf, getch...?) to get the pressed arrow.

  8. #38
    Registered User
    Join Date
    Mar 2008
    Posts
    21
    I did it.

  9. #39
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    How? What compiler are you using? If you are using Dev-C++, did you use getche()? (Just out of curiosity.)

    By the way, that smiley that you keep using ( ) is not used when asking for help. Actually, it indicates sarcasm, which usually means you're annoyed or something -- which I don't think is what you want to convey.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #40
    Registered User
    Join Date
    Mar 2008
    Posts
    21
    I'm using lcc-win32.

    http://support.microsoft.com/kb/57888

    P.S. Sorry for the smiley, it has a different meaning in our country

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Worksafe Array of Strings
    By Hawkin in forum C Programming
    Replies: 4
    Last Post: 03-28-2008, 11:00 PM
  2. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Passing an Array of Strings from VB to a C DLL
    By mr_nice! in forum Windows Programming
    Replies: 9
    Last Post: 03-08-2005, 06:16 AM
  5. Array of strings in C
    By szill in forum C Programming
    Replies: 10
    Last Post: 02-22-2005, 05:03 PM