Thread: Scanf String

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

    Scanf String

    Alright I want to know how to scanf an sting so I can input A full name so that when I hit Space it does not skip threw any thing like this does (p.s code not finish just example)
    I would want to input Kory Welson as the first employee



    Code:
    
    #include <stdio.h>
    
    
    
    int main ( void )
    
    {
     int cou;
     int counet;
     int id [10];
     double hours [10];   
     double wage [10];
     double tax  [10]; 
     double gros [10];
     double net [10];
     char Employee1 [60];
     char Employee2 [60];  
     char Employee3 [60];
     char Employee4 [60];   
     char Employee5 [60]; 
     char Employee6 [60];
     char Employee7 [60];
     char Employee8 [60];
     char Employee9 [60];
     char Employee10 [60];
    
    printf(" Input Employee 1's name\n");
    
    scanf("%s", Employee1);
    
    printf("\nInput %s 's wage", Employee1);
    
    scanf("%f", &wage [1]);
    
    printf("\nThe amount of hours %s 's wored are", Employee1);
    
    scanf("%f", &hours [1]);
    
    printf("\nThe tax in %s 's area is", Employee1); 
    
    scanf("%f",  &tax [1]);      
           
    
    
    
    
    printf(" Input Employee 2's name\n");
    
    scanf("%s", Employee2);
    
    printf("\nInput %s 's wage", Employee2);
    
    scanf("%f", &wage [2]);
    
    printf("\nThe amount of hours %s 's wored are", Employee2);
    
    scanf("%f", &hours [2]);
    
    printf("\nThe tax in %s 's area is", Employee2); 
    
    scanf("%f",  &tax [2]);           
           
    
    printf(" Input Employee 3's name\n");
    
    scanf("%s", Employee3);
    
    printf("\nInput %s 's wage", Employee3);
    
    scanf("%f", &wage [3]);
    
    printf("\nThe amount of hours %s 's wored are", Employee3);
    
    scanf("%f", &hours [3]);
    
    printf("\nThe tax in %s 's area is", Employee3); 
    
    scanf("%f",  &tax [3]);    
           
    
    printf(" Input Employee 4's name\n");
    
    scanf("%s", Employee4);
    
    printf("\nInput %s 's wage", Employee4);
    
    scanf("%f", &wage [4]);
    
    printf("\nThe amount of hours %s 's wored are", Employee4);
    
    scanf("%f", &hours [4]);
    
    printf("\nThe tax in %s 's area is", Employee4); 
    
    scanf("%f",  &tax [4]);    
    
    
    printf(" Input Employee 5's name\n");
    
    scanf("%s", Employee5);
    
    printf("\nInput %s 's wage", Employee5);
    
    scanf("%f", &wage [5]);
    
    printf("\nThe amount of hours %s 's wored are", Employee5);
    
    scanf("%f", &hours [5]);
    
    printf("\nThe tax in %s 's area is", Employee5); 
    
    scanf("%f",  &tax [5]);    
           
    
    printf(" Input Employee 6's name\n");
    
    scanf("%s", Employee6);
    
    printf("\nInput %s 's wage", Employee6);
    
    scanf("%f", &wage [6]);
    
    printf("\nThe amount of hours %s 's wored are", Employee6);
    
    scanf("%f", &hours [6]);
    
    printf("\nThe tax in %s 's area is", Employee6); 
    
    scanf("%f",  &tax [6]);    
           
    
    printf(" Input Employee 7's name\n");
    
    scanf("%s", Employee7);
    
    printf("\nInput %s 's wage", Employee7);
    
    scanf("%f", &wage [7]);
    
    printf("\nThe amount of hours %s 's wored are", Employee7);
    
    scanf("%f", &hours [7]);
    
    printf("\nThe tax in %s 's area is", Employee7); 
    
    scanf("%f", &tax [7]);    
           
    
    printf(" Input Employee 8's name\n");
    
    scanf("%s", Employee8);
    
    printf("\nInput %s 's wage", Employee8);
    
    scanf("%f", &wage [8]);
    
    printf("\nThe amount of hours %s 's wored are", Employee8);
    
    scanf("%f", &hours [8]);
    
    printf("\nThe tax in %s 's area is", Employee8); 
    
    scanf("%f",  &tax [8]);    
           
    
    printf(" Input Employee 9's name\n");
    
    scanf("%s", Employee9);
    
    printf("\nInput %s 's wage", Employee9);
    
    scanf("%f", &wage [9]);
    
    printf("\nThe amount of hours %s 's wored are", Employee9);
    
    scanf("%f", &hours [9]);
    
    printf("\nThe tax in %s 's area is", Employee9); 
    
    scanf("%f",  &tax [9]);    
           
    
    printf(" Input Employee 10's name\n");
    
    scanf("%s", Employee10);
    
    printf("\nInput %s 's wage", Employee10);
    
    scanf("%f", &wage [10]);
    
    printf("\nThe amount of hours %s 's wored are", Employee10);
    
    scanf("%f", &hours [10]);
    
    printf("\nThe tax in %s 's area is", Employee10); 
    
    scanf("%f",  &tax [10]);    
           
    for (cou = 0;cou <= 10; cou++) {
    
    gros [cou] = wage [cou] * hours [cou] ;
    
    }        
    
    for ( ctax = 0; ctax <= 10; ctax)  {
    
    tax[ctax] /= 100;
    
    } 
    
    
    
    
    for (counet = 0; counet <= 10; counet++) {
    
    net[counet] = gros [ counet ] * tax [ counet ];
    
    }
    
    for ( coid = 0; coid <= 10; coid++) {
    
    printf("\nPlease input your 10 Empolyee's ID numbers" );
    
    
    scanf("%d", &id [ciod] );
    
    } 
     
    
    
    
    
    
    printf ("ID %d\n %d\n %d\n %d\n %d\n 
    %d\n %d\n %d\n %d\n %d\n", id [1], id [2],
    id [3], id [4], id [5], id [6], id [7], id [8],
    , id [9], id [10]);
    
          getchar ();   
            
            
            
            getchar ();
            
            
            
            return 0;
    }

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    1 Never use scanf() to read in a string.

    Use fgets() instead.

    Code:
    char theName[ 20 ];
    
    puts("Enter the name: ");
    fgets( theName, 20, stdin );
    // there is somthing here you need to do next too
    Also, please indent your code, it is very hard to read that.
    Double Helix STL

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by swgh View Post
    1 Never use scanf() to read in a string.
    It's okay to use scanf to read in a string if you are not worried the user has a malicous intent (like if you will always be the user, etc). The command does exist and perform it's function correctly.

    Anyway, all you needed to do was include your problem line and not the whole program. If that line is okay, then maybe more is needed. But I suspect that from the nature of your question the problem is that scanf does not include spaces in &#37;s strings so you will only get the first name if you do that. Instead try:

    Code:
    scanf("%[A-za-z .]",fullname);
    Notice this includes upper and lowercase letters, the space, and a period in case I someone writes "Mr. Earl"
    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

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can get rid of every extra line between your lines of code, as well. Many people find that... annoying at best.
    Oh and, swgh, better use sizeof(theName) instead.

    Quote Originally Posted by MK27 View Post
    It's okay to use scanf to read in a string if you are not worried the user has a malicous intent (like if you will always be the user, etc). The command does exist and perform it's function correctly.
    Never, ever assume that, unless you have a very good reason. 99.99&#37; of the times, you don't. So don't. It's equally bad as gets.

    Code:
    scanf("%[A-za-z .]",fullname);
    http://cboard.cprogramming.com/showp...37&postcount=9
    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.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    20
    Big thxs

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >It's okay to use scanf to read in a string if you are not worried the user has a malicous intent
    Legitimate input mistakes can be just as devastating as malicious input strings. Anyway, there's no point in trying to guess when scanf is perfectly safe if you provide a length modifier. Any time you use %s or %[, there should be an expected length protecting against buffer overflow:
    Code:
    /* Subtract 1 to make room for '\0' */
    scanf ( "%59s", Employee1 );
    Code:
    /* Uh oh, we don't have the size of fullname as a literal */
    {
      char fmt[20];
    
      /* Note that ranges (A-z, a-z) in the scanset aren't portable */
      sprintf ( fmt, "%%%d[A-za-z .]", n - 1 );
      scanf ( fmt, fullname );
    }
    If you use these conversion specifiers naively, then scanf can be as dangerous as gets, just the same as if you always pass INT_MAX to fgets. It's all about the quality of the programmer here, not the inherent safety of scanf (though I'll admit that these modifiers should be required by default rather than optional).
    My best code is written with the delete key.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Prelude View Post
    >It's okay to use scanf to read in a string if you are not worried the user has a malicous intent
    Legitimate input mistakes can be just as devastating as malicious input strings.
    Perhaps you do not know what a legimately devestating and intentionally malicious input line could be, because they are not generated "by mistake".

    Accidentally causing a buffer overflow just causes your program to crash, which is as bad as that accident can get, but nothing like having someone gain root access (no accident) or just plain causing a system crash (no accident) with a cleverly designed "input line" where they know an overflow is possible.

    You are right about the length qualifier.

    ps. google "nop sled" or "nop slide"
    Last edited by MK27; 12-05-2008 at 08:51 AM.
    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

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Perhaps you do not know what a legimately devestating and intentionally malicious input line could be
    Yes, in fact I do. Perhaps you don't realize the horrible problems that benign input mistakes can cause.

    >Accidentally causing a buffer overflow just causes your program to crash
    Bzzt! Wrong. As only one example of many, I've seen buffer overflow silently corrupt financial data. So is it that you don't consider a suddenly negative bank balance to be more devastating than a program crash? I'm leaning more toward ignorance of the potential problems of undefined behavior.
    My best code is written with the delete key.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Prelude View Post
    >Accidentally causing a buffer overflow just causes your program to crash

    Bzzt! Wrong. As only one example of many, I've seen buffer overflow silently corrupt financial data.
    Point. I hadn't thought of possible effects on saved data, perhaps because it seems to me that would be the result of poor programming above and beyond just the initial overflow. Like I first said, I wouldn't permit the possibilty any where I'm not the only user, because I wrote it and I know.
    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

  10. #10
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by MK27 View Post
    Point. I hadn't thought of possible effects on saved data, perhaps because it seems to me that would be the result of poor programming above and beyond just the initial overflow. Like I first said, I wouldn't permit the possibilty any where I'm not the only user, because I wrote it and I know.
    Lol, MK27 I've been enjoying reading some posts of yours man, it's like the others are all on ya, lol, but i get your point and Prelude also
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  4. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 02:54 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM