printing error

This is a discussion on printing error within the C Programming forums, part of the General Programming Boards category; hi there my prog prompts for the number of students in a class and then for a mark for each ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    13

    Angry printing error

    hi there
    my prog prompts for the number of students in a class and then for a mark for each of them.then it is supose to display a histogram of how many student have the same mark, as a bar chart.am having problems on the displaying parts. i think is probably the way i declared the variables but am not sure anymore.please any idea
    Code:
     #include <stdio.h>
    
    
    
    void main(){
    
     int num;
     int i;
     int students[20];
     int marks [5];
    
     printf("How many students are in the class?  :");
     scanf("%d", &num);
    
    
     for(i=0;i<=num-1;i++){
    
     printf("Enter the marks for the student %d :" ,i+1);
     scanf("%d",&students[i]);
     }
    
     for(i=0;i<=4;i++){
     marks[i]=0;
     }
    
     for(i=0;i<num-1;i++){
    
    if (students[i]==0){  marks[0]++;}
      else{ if (students[i]==1)   marks[1]++;
              else{  if (students[i]==2)   marks[2]++;
                        else {if (students[i]==3)   marks[3]++;
                                  else{ if (students[i]==4)   marks[4]++;
                                      }
                              }
                   }
           }
     }
     for(i=0;i<=4;i++){
     printf("%d\t   %d\n", i, asterix(marks[i]));
     }
       scanf("%d",&students[i]);
    }
    
    asterix(char n)
       {
        int i;
        char c=' ';
        char ch='*';
    
        if(n==0){
        printf("%c", c );
                }
        else{
            for(i=0;i<=n;i++)
                {
                 printf("%c", ch);
                }
             }
    
    
             return;
       }
    the output now is this:
    How many students are in the class? :5
    Enter the marks for the student 1 :1
    Enter the marks for the student 2 :1
    Enter the marks for the student 3 :1
    Enter the marks for the student 4 :1
    Enter the marks for the student 5 :1
    0 1
    *****1 1
    2 1
    3 1
    4 1

    It is supposed to be like this:

    How many students are in the class? :5
    Enter the marks for the student 1 :1
    Enter the marks for the student 2 :1
    Enter the marks for the student 3 :1
    Enter the marks for the student 4 :1
    Enter the marks for the student 5 :1
    0
    1 *****
    2
    3
    4

  2. #2
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77
    hello. i really didn't solve any of your problems.....I got real bored and I thought that your idea was neat so I made my own program very similar to yours....just because i'm real new to programming and wanted to know if i could repeat a similar effect without looking at your code...i did.....mine prints out properly....its structure alot different than yours and probably a bit simpler....here it is.

    Code:
    #include <stdio.h>
    
    void main()
    {
    
    	int a=0,b=0,c=0,d=0,e=0;
    	int students;
    	int x=1;
    	char answer;
    	
    	printf("\n\t\t\t**************************");
    	printf("\n\t\t\t*    The Clicker Poll    *");
    	printf("\n\t\t\t**************************");
    	printf("\n\n\n\t\t\t    Only A,B,C,D, or E    ");
    
    	
    	printf("\n\n\n\tNumber of Students that Clicked in :  ");
    	scanf("%i",&students);
    		
    		while (students>0)  {
    			
    			printf("\tStudent #%i Answered: ",x);
    			scanf("%s",&answer);
    			if ((answer=='A') || (answer=='a')) {  
    				a++;  }
    			if ((answer=='B') || (answer=='b')) {
    				b++;  }
    			if ((answer=='C') || (answer=='c')) {
    				c++;  }
    			if ((answer=='D') || (answer=='d')) {
    				d++;  }
    			if ((answer=='E') || (answer=='e')) { 
    				e++;  }
    								
    			students--;
    			x++;
    	}
    	
    	
    	printf("\n\n\t\t\t ** GRAPHED **");
    	printf("\n\t\t");
    
    	printf("  Clicked A (%d Responses): ",a);
    	while (a>0)   {
    		printf("*");
    		a--;      }
    		printf("\n\t\t");
    		printf("  Clicked B (%d Responses): ",b);
    	while (b>0)   {
    		printf("*");
    		b--;      }
    		printf("\n\t\t");
    		printf("  Clicked C (%d Responses): ",c);
    	while (c>0)   {
    		printf("*");
    		c--;      }
    		printf("\n\t\t");
    		printf("  Clicked D (%d Responses): ",d);
    	while (d>0)   {
    		printf("*");
    		d--;      }
    		printf("\n\t\t");
    		printf("  Clicked E (%d Responses): ",e);
    	while (e>0)   {
    		printf("*");
    		e--;      }
    
    	printf("\n\n\t\t...press any key to continue...");
    }
    The output looks like:

    Code:
                      **************************
                      *    The Clicker Poll    *
                      **************************
    
    
                          Only A,B,C,D, or E
    
    
      Number of Students that Clicked in :  6
      Student #1 Answered: A
      Student #2 Answered: C
      Student #3 Answered: D
      Student #4 Answered: D
      Student #5 Answered: E
      Student #6 Answered: A
    
    
                       ** GRAPHED **
                Clicked A (2 Responses): **
                Clicked B (0 Responses):
                Clicked C (1 Responses): *
                Clicked D (2 Responses): **
                Clicked E (1 Responses): *
    you get the idea...hope you gets yours to work. sorry i couldnt offer much assistance i could never get yours to compile right for me...

  3. #3
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77

    Lightbulb

    ....come to think about it........i added a new special effect to mine....something like:

    Code:
    Sleep(100):
    printf("*\a");
    using the #include <windows.h> to access the Sleep function and then adding a beep....so the program beeps when the graph is made, and the graph stats kind quickly astriek themselves across each one...makes it look kind of neat... just a thought.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  4. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 11:17 AM
  5. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM

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