#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    On Earth!!
    Posts
    9
    Rep Power
    0

    Exclamation What's wrong with my Code


    Code:
     #include<stdio.h>
    #include<conio.h>
    main()
    {
      int i,j,car;
      static int frequency[5][5]={{0},{0},{0},{0},{0}};
      char city;
      clrscr();
      printf("For Each Person,enter the City Code \n");
      printf("followed by the car code \n");
      printf("Enter the Letter X to indicate end \n");
      for(i=1;i<10;i++);
      {
       scanf("%c",&city);
       if(city=='X')
         return 0;
       scanf("%d",&car);
       switch(city)
       {
         case 'B': frequency[1][car]++;
    	       break;
         case 'C': frequency[2][car]++;
    	       break;
         case 'D': frequency[3][car]++;
    	       break;
         case 'M': frequency[4][car]++;
    	       break;
        }
        }
         printf("\n\n");
         printf("    POPULARITY TABLE   \n\n");
         printf("_______________________________________________________ \n");
         printf("City     Ambassador     Fiat     Dolphin     Maruti \n");
         printf("_______________________________________________________ \n");
         for(i=1;i<=4;i++);
         {
           switch(i)
           {
           case 1: printf("Bombay    \n ");
    		 break;
           case 2: printf("Calcutta   \n");
    	       break;
           case 3: printf("Delhi     \n ");
    	       break;
           case 4: printf("Madras    \n ");
    	       break;
           }
    	for(j=1;j<=4;j++);
    	printf("%d",frequency[i][j]);
    	printf("\n");
    	}
          printf("_____________________________________________________ \n");
          getch();
     }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    On Earth!!
    Posts
    9
    Rep Power
    0
    How Can I break my input values in my code??? :eek:
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Originally Posted by Invalid Loop!
    How Can I break my input values in my code??? :eek:
    What is that supposed to mean? Are you asking us to provide you with a method for breaking your input values? Are you saying that there's nothing wrong with your code and you want us to tell you how to make it wrong? That makes absolutely no sense whatsoever.

    Do you believe that your code has a problem in it? Yes or no.

    If yes, then simply describe the problem. In cogent English. Give us a question that we can understand and that we can answer. No gibberish, please.


    BTW, this line:
    scanf("%c",&city);
    You're telling scanf to read in the very next character no matter what kind of character it is, even white space like a newline. The last call to scanf left at least a newline in the input buffer, so that is what that line will read in. Is that really what you want this scanf call to do that?

    Or do you want to tell it to skip all white space and read in the first non-white space (AKA "printable") character it finds? If so, then you need to tell it that, thus:
    scanf(" %c",&city);
    Note the blank before the percent sign. That tells scanf to expect and skip any white space that precedes a printable character.
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,367
    Rep Power
    1870
    > for(i=1;i<10;i++);
    All your for loops have a ; at the end, which makes the loop do NOTHING.

    Also, read how to indent code
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo