I need help about string

This is a discussion on I need help about string within the C Programming forums, part of the General Programming Boards category; hi,my program calculate the salary of salesperson,but gets(name) doesnt give the name? Code: #include<stdio.h> #include<stdlib.h> #include<ctype.h> int main() { char ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    7

    Exclamation I need help about string

    hi,my program calculate the salary of salesperson,but gets(name) doesnt give the name?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    int main()
    {
        char name[100];
        int sales[100],k=0,n,first=0,second=0,third=0,fourth=0,fifth=0;
        double salary[100];
        
        printf("how many salesperson in this company = ");
        scanf("%d",&n);
    //given the informations of salespersons...  
      
        for(int i=0; i<n;i++)
        {
         //printf("Name of salesperson  = ");
         gets(name);                           //here doesnt give the name
         printf("\nSales of salesperson = ");
        scanf("%d",&sales[i]);
          
         salary[i]=sales[i]*0.8+100;       
        }
       
    //make equal space ...
         for(int i=0;i<n;i++)
           for(int j=0;j<=40;j++)
              name[i]=' '+name[i];
              
    //draw first table ...
        for(int i=0;i<n;i++)
           {
              while(name[k] !='\0')
                 {
                  putchar(name[k]);
                  if(k==40)
                    { 
                  printf("$ %d  |$ %d",sales[i],salary[i]);
                  k++;     
                    }   
                 } 
                printf("\n");
           } 
              
    //the range of salary ...
        for(int i=0; i<n; i++)
           {
             if(100<= salary[i] && salary[i]<300)
                first++;
        else if(300<= salary[i] && salary[i]<500)   
                second++;
        else if(500<= salary[i] && salary[i]<700)
                third++;
        else if(700<= salary[i] && salary[i]<900)
                fourth++;
        else
                fifth++;   
           }  
    //draw second table ...
           
                 printf(" ----------------------------- \n");
                 printf("| range      |number of sales people |\n");
                 printf("|-----------------------------|\n");  
                     
        for(int i=0;i<5;i++)
          {
             switch(i)
              {
               case 0: printf("| 100$ - 299$    | %d          |\n",first);  break;
               case 1: printf("| 300$ - 499$    | %d          |\n",second); break;
               case 2: printf("| 500$ - 699$    | %d          |\n",third);  break;
               case 3: printf("| 700$ - 899$    | %d          |\n",fourth); break;
               case 4: printf("| 900$ and over  | %d          |\n",fifth);  break;
              } 
                       printf("|-----------------------------|\n");     
           }
        
        system("pause");
        return 0;
    }
    thank you for helping.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    gets() doesn't "give" the name. It "gets" the name (and you shouldn't actually be using it, anyway).

    To print the name, use printf() with the %s format

    As soon as you can, switch from using gets() to using fgets() -- gets() is WAY dangerous in a program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 22
    Last Post: 07-28-2011, 01:26 PM
  2. Replies: 7
    Last Post: 06-16-2011, 06:21 PM
  3. Replies: 5
    Last Post: 05-09-2010, 10:58 AM
  4. Replies: 1
    Last Post: 10-31-2005, 10:36 AM
  5. Problem comparing string from text file with string constant
    By XenoCodex Admin in forum C++ Programming
    Replies: 3
    Last Post: 07-25-2002, 10:17 AM

Tags for this Thread


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