开发者

Error on char array declaration in GCC 3.3.4 [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.
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.


How about:

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

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


#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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜