Trying to learn how to use %[^:]:, having trouble.

This is a discussion on Trying to learn how to use %[^:]:, having trouble. within the C Programming forums, part of the General Programming Boards category; Hi! I have an exam tomorrow, i need to learn how to use %[^:] Sample that i made: Enter Name ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    11

    Post Trying to learn how to use %[^:]:, having trouble.

    Hi!

    I have an exam tomorrow, i need to learn how to use %[^:]

    Sample that i made:
    Enter Name and Rank
    Bill Gates: 2
    Enter Name and Rank
    Sergey Brin: 1
    Enter Name and Rank
    Mr Random: 3

    Name: Bill Gates has rank: 2
    Name: Sergey Brin has rank: 1
    Name: Mr Random has rank: 3
    Here is my code
    Code:
    #include<stdio.h>
    #define MAX 100
    int main(void)
    {
    
    
     int  i;
     int rank[MAX];
     char name[MAX];
    
    
     for(i=0; i<3; i++)
     {
        printf("Enter Name and Rank\n");
        scanf("%[^:]:%d", &name[i], &rank[i]);
     }
    
     for(i=0; i<3; i++)
     {
         printf("Name: %s has rank:%d\n", name[i], rank[i]);
     }
     return 0;
    }
    test.c: In function ‚main‚:
    test.c:14:5: warning: format ‚%s‚ expects argument of type ‚char *‚, but argument 2 has type ‚int‚ [-Wformat]

    Enter Name and Rank
    Bill Gates:2
    Enter Name and Rank
    Sergey Brin:1
    Enter Name and Rank
    Mr Random:3
    Segmentation fault
    What am I doing wrong?
    Last edited by shank09; 04-18-2013 at 09:30 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have a one dimension char array for name, so name[i] and &name[i] refer to ONE character, and ONE characters address. You either want name and &rank[i], or you want to make name[] a 2D char array name[][]. Either way, the &before "name" is incorect. "name" serves as the pointer to the base of the name array, and name[i] serves as the pointer to one name within a 2D array of names.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-20-2009, 06:22 PM
  2. Do you ever try to learn too much?
    By Stonehambey in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 06-17-2008, 08:55 AM
  3. If I Know C++ will it take much to learn C
    By indigo0086 in forum C++ Programming
    Replies: 12
    Last Post: 01-11-2007, 04:51 PM
  4. You have to learn C in order to learn C++
    By gandalf_bar in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 07-16-2004, 11:33 AM
  5. a 10 yr old kid trying to learn C++
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2002, 09:05 AM

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