-2
int main () {

   char b[100];
   for (int i = 1; i <= 10; i++ )
       scanf ("%c%*c", b[i]);
}

but am getting the error 'Format arguemnt is not a pointer'

How can i declare an array to get all values form the user?

EDIT :

#include <cstdio>
#include <stdlib.h>

using namespace std;


int p[100], b, bc;
char bb[100];

int main () {


        printf("Enter Count : ");
        scanf ("%d", &bc);
        for (b = 1; b <= bc; b++ ) {
            printf("Enter a char and integer: ");
            scanf ("%c%*c %d", &bb[b-1], &p[b-1]);
            printf ("\n Your Entries =>  %c,  %d", bb[b-1], p[b-1]);
        }


    return 0;
}

This is my source code.

coderex
  • 27,225
  • 45
  • 116
  • 170

3 Answers3

3

How about:

scanf("%c", &b[i]);

scanf() needs to know the address of the variable, so that it can modify it.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
2
#include <cstdio>

Apparently, you're coding in C++

#include <stdlib.h>

Oh! Wait. It is C after all.
@coderex: make up your mind what language you're using

using namespace std;

Oh! It is C++. My answer does not consider the C++ specifics which I don't know.

int p[100], b, bc;
char bb[100];

If you can avoid using global variables, your code will be easier to deal with.

int main () {


        printf("Enter Count : ");
        scanf ("%d", &bc);
        for (b = 1; b <= bc; b++ ) {

The idiomatic way is for (b = 0; b < bc; b++). Using the idiomatic way, you won't need to subtract 1 inside the loop to access the array indexes.

            printf("Enter a char and integer: ");
            // scanf ("%c%*c %d", &bb[b-1], &p[b-1]);

scanf() is notoriously difficult to use correctly.
Anyway the "%d" conversion specifier already discards whitespace so you can remove the strange stuff; also there's an ENTER pending from the last scanf call. Using a space in the format string gets rid of it.

            scanf (" %c%d", &bb[b-1], &p[b-1]);
            printf ("\n Your Entries =>  %c,  %d", bb[b-1], p[b-1]);
        }


    return 0;
}

Have fun!

pmg
  • 106,608
  • 13
  • 126
  • 198
0

Error was, its reading the "enter key" as newline char after an integer value enter.

to avoid this I used %*c scanf ("%c%*c %d%*c", &bb[b-1], &p[b-1]);

thank you everybody.

coderex
  • 27,225
  • 45
  • 116
  • 170
  • See my answer ... but `scanf(" %c%d", &bb[b - 1], &p[b - 1]);` "works" and is easier on the eyes and brain. – pmg May 07 '11 at 14:59