-2

I'm learning C and one of my tasks is to make a program where you input your name, street name and id number and have it print out that information. Every time that the program is getting to the id number part it's "going crazy." For the record, there's an int and char together in the same function, perhaps that's the source of the problem?

#include<stdio.h>

char a,b,c,d;
char e,f,g,h,i;
int j,k,l,m,n,o,p,q;
int main()
{
    printf("\nwrite your name (4 letter's only) ");
    scanf("%c%c%c%c",&a,&b,&c,&d);
    printf("\nwrite your street name (5 letter's only')");
    scanf("%c%c%c%c%c",&e,&f,&g,&h,&i);
    printf("\nwrite your id number (8 number's only')");
    scanf("%d%d%d%d%d%d%d%d",&j,&k,&l,&m,&n,&o,&p);
    printf("your name is %c%c%c%c your street name is %c%c%c%c%c and your id number is %d%d%d%d%d%d%d%d ",a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q);
    return 0;
}
Azar
  • 1,086
  • 12
  • 27
  • 7
    Wow. You desperately need a book on C, dude. – ooga Apr 26 '14 at 16:52
  • i know its possible man but i dont know why my program doesnt work – user3543289 Apr 26 '14 at 16:54
  • It's riddled with errors at every level to such a degree that it's not worth anyone's time to explain it all to you. Get a book. Go through the exercises from the beginning. You'll understand then. – ooga Apr 26 '14 at 16:56
  • Holy cow, this looks like it came straight from TDWTF. – Azar Apr 26 '14 at 17:02
  • thank you for.......helping me , i told you im a starter so instead of saying how dumb i am can you tell me what is the problem – user3543289 Apr 26 '14 at 17:05
  • @user3543289 [This](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) might be of some use to you. – Azar Apr 26 '14 at 17:36

2 Answers2

4

%d reads a whole integer not a single digit. Thus the first %d will read the whole id number into j. You do not need all those int variables - use only one. Also you may consider using %s and char array to read the street name and the user's name.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • as i said alerady im a starter and i know only the most basic stuff. i did what you told be to but its still not working, its not letting me make an input in the id int/s – user3543289 Apr 26 '14 at 16:58
  • @user3543289 then my answer should guide you to some educational and useful reading. Before jumping to that, though try reading a single integer, say `j` and printing it to see what happens – Ivaylo Strandjev Apr 26 '14 at 16:59
  • @user3543289 get to know, the answer tell you enough how much you need to know. – Grijesh Chauhan Apr 26 '14 at 17:03
-1

As an example of how your program might be written.

#include <stdio.h>
#include <string.h>

int main() {
    char name[100];
    char street[100];
    int id;

    printf("Enter your name: ");
    fgets(name, sizeof name, stdin);
    strtok(name, "\n"); // remove newline from end of name

    printf("Enter your street name: ");
    fgets(street, sizeof street, stdin);
    strtok(street, "\n"); // remove newline from end of street

    printf("Enter your id number: ");
    scanf("%d", &id);

    printf("Your name is %s, your street name is %s "
           "and your id number is %d\n", name, street, id);

    return 0;
}
ooga
  • 15,423
  • 2
  • 20
  • 21