![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Mar 2005
Posts: 40
Rep Power: 0
![]() |
Table in C language???
I have to make a table in which a user can input values horizontally in a table row...
user should enter the number of students and then a table must be generated of that size like this ... Name ..........<space>........ Marks in Subject-1 ..........<space>........ Marks in Subject-2... But the confusion lies because I have to do it in C language without arrays and functions, only by the use of for loop, if condition and gotoxy statement. I'v tried gotoxy statement but it isnt working properly ... please help me for this program ... Last edited by Planet_EN; Mar 27th, 2005 at 12:29 AM. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Mar 2005
Posts: 40
Rep Power: 0
![]() |
bump ...
|
|
|
|
|
|
#3 |
|
Programmer
Join Date: Mar 2005
Posts: 40
Rep Power: 0
![]() |
The first problem is that i cant align the fields in the appropriate manner ...
|
|
|
|
|
|
#4 |
|
Programmer
Join Date: Mar 2005
Posts: 40
Rep Power: 0
![]() |
here the program
#include<stdio.h>
#include<conio.h>
void main()
{
int x,inputs,loop1,sum;
clrscr();
for(x=1;x<=80;x++)
{
gotoxy(x,1);
printf("\xCD");
}
printf("Name\tSubject-1\tSubject-2\tSubject-3\tSubject-4\tSubject-5\tTotal\tPer\tAverage");
for(x=1;x<=80;x++)
{
gotoxy(x,3);
printf("\xCD");
}
for(loop1=8;loop1<=88;loop1+=10)
{
gotoxy(loop1,4);
scanf("%d",&inputs);
printf("\t");
sum=sum+inputs;
inputs=0;
}
} |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Mar 2005
Posts: 40
Rep Power: 0
![]() |
Here's my final program ... But its still having some alignment problem ... can anyone help ????
#include<stdio.h>
#include<conio.h>
void main()
{
int x,inputs,ColumnHandler,sum,rows=1,nos;
char name[25],grade;
float per;
x=inputs=sum=ColumnHandler=per=0;
clrscr();
printf("Enter the number of students : ");
scanf("%d",&nos);
clrscr();
for(x=1;x<=80;x++)
{
gotoxy(x,1);
printf("\xCD");
}
printf("Name %c%c%c%c",32,32,32,32);
printf("Elect%c%c%c%c",32,32,32,32);
printf("Cal-1%c%c%c%c",32,32,32,32);
printf("CP-1 %c%c%c%c",32,32,32,32);
printf("DMath%c%c%c%c",32,32,32,32);
printf(" Acc. %c%c%c%c",32,32,32,32);
printf(" Total%c%c%c%c",32,32,32,32);
printf("Per %c%c%c%c",32,32,32,32);
printf("Grade");
for(x=1;x<=80;x++)
{
gotoxy(x,3);
printf("\xCD");
}
for(;rows<=nos;rows++)
{
gotoxy(1,4+rows);
scanf("%s",name);
for(ColumnHandler=10;ColumnHandler<=60;ColumnHandler+=10)
{
if(ColumnHandler<=50)
{
gotoxy(ColumnHandler,4+rows);
scanf("%d",&inputs);
sum=sum+inputs;
inputs=0;
}
else
{
gotoxy(ColumnHandler,4+rows);
per=sum/5;
printf("%d%c%c%c%c%c%.0f%c%c%c%c%c",sum,32,32,32,32,32,per,32,32,32,32,32);
if(per>90)
printf(" A+");
else if(per<=90&&per>80)
printf(" A");
else if(per<=80&&per>70)
printf(" B");
else if(per<=70&&per>60)
printf(" C");
else if(per<=60&&per>50)
printf(" D");
sum=per=0;
}
}
}
getch();
} |
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 314
Rep Power: 4
![]() |
No offence, but that's some pretty twisted code. In your printf() format strings, you have multiple %c (character) parts, followed by the constant 32. This is exactly the same as just including a space in the format string. I can't see any reason for doing it this way.
The problems with the layout will be because you output the same number of spaces to pad out columns no matter what the length of the actual content is. I haven't actually tried the program (I can only get online at the library at the moment and their machine doesn't even have a C compiler...I feel claustrophobic!) or looked at it in great detail (running it would help with that) but I notice that nowhere in your program does it seem to measure strings and calculate the amount of padding required. Not sure about this, but you always, in your program, count co-ords on the screen from 1 up to for example 80, not 0 to 79, which looks a little strange to me. Are you sure you shouldn't be counting from zero? Like I say I'm not sure if this is right. You might well know better than I do in this regard. Ah, I see you don't actually need to calculate padding because you're always using gotoxy() to go to where the fields ought to start; so probably disregard at least some of what I've said here. The only other thing that springs to mind that could be giving you layout issues is the fact that each time input is read, it's echoed, which will move the cursor. Are you accounting for that? Just one last thing; your choice of variable names is a bit bizarre. This makes it much harder to skim your program and see what's going on. It took me about five minutes of frowning to realise that ColumnHandler is just an int, and with that long name and those CapitalLetters in it, I couldn't help thinking of it as a class ![]() Hope I've said something helpful in this rambling post. ![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|