Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5428
Number of posts: 16949

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
solve this..... Posted by nikku_smartie on 8 Feb 2012 at 2:05 PM
guys please check this code and tell me why the hell this is not working?
Question is-a test consisting of 10 multiple choice ques is administered to a batch of two students.first we input correct options on terminal like(abcdeabcde) and then students answers respectively.
and will got result for each ques.



but this code is not working....
/*............................STARTING OF CODE...................*/




#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define QUES 10
#define STUD 2
/* COADED BY NIKHIL SAGAR */
void main()
{
int i;
char answer[QUES],choice[QUES];
printf ("please enter answer keys\n\n");
for (i=1;i<=QUES;i++)
{
scanf ("%c",&answer[i]);
}

for (i=1;i<=STUD;i++)
{
printf ("Please enter Student No. %d's Choices \n\n",i);
for (i=1;i<=QUES;i++)
{
scanf ("%c",&choice[i]);
}
for (i=1;i<=QUES;i++)
{
if (answer[i]==choice[i])
{ printf ("Ques Number %d is CORRECT\n",i); }
else
printf ("Ques Number %d is WRONG\n",i);
}
}
getch();
system("cls");
/* COADED BY NIKHIL SAGAR */
}





/*........................END OF CODE.........................*/


i have a code for the same problem and that one is working very fine but that is huge.and also i would like to know the error of this code too.
Report
Re: solve this..... Posted by KenJackson on 13 Feb 2012 at 8:01 PM
If you have a variable answer[10], you need to index it from 0 to 9 (not 1 to 10).

Report
Re: solve this..... Posted by nikku_smartie on 14 Feb 2012 at 10:52 PM
: If you have a variable answer[10], you need to index it from
: 0 to 9 (not 1 to 10).

:
:
thanks for reply dude.....
what if i edit this.....

:char answer[QUES],choice[QUES];


to this....

:char answer[QUES+1],choice[QUES+1];

ithink it is ok by you.....if yes then run it and it is also giving runtime error......dude if you can write this code correctly i mean without runtime/compile time error then please post here as soon as possible....thanks in advance....
Report
Re: solve this..... Posted by KenJackson on 15 Feb 2012 at 10:12 AM

Yes, that looks like it will work. But you would be wasting array entry 0. Also, it would be an invitation for someone in the future to misunderstand the code and make an error when trying to modify it.

So instead of looping like this:
for (i=1; i<=QUES; i++)

you should do it the more standard and common way, like this:
for (i=0; i<QUES; i++)

Report
Re: solve this..... Posted by nikku_smartie on 17 Feb 2012 at 12:14 AM
:
: Yes, that looks like it will work. But you would be wasting array
: entry 0. Also, it would be an invitation for someone in the future
: to misunderstand the code and make an error when trying to modify it.
:
: So instead of looping like this:
: for (i=1; i<=QUES; i++)
:
: you should do it the more standard and common way, like this:
: for (i=0; i<QUES; i++)
:
:


yeah thx for suggestion dude but after this modification it is still not working and giving logical errors.....check it out on your machine....
i mean first input the correct answer keys like abcde , and then student no 1 answer keys (abcde) and after enter it will give u random results like some answers are correct and some are incorrect.
Report
Re: solve this..... Posted by nikku_smartie on 14 Feb 2012 at 10:55 PM
: If you have a variable answer[10], you need to index it from
: 0 to 9 (not 1 to 10).

:
:
thanks for reply dude.....
what if i edit this.....

:char answer[QUES],choice[QUES];


to this....

:char answer[QUES+1],choice[QUES+1];

ithink it is ok by you.....if yes then run it and it is also giving runtime error......dude if you can write this code correctly i mean without runtime/compile time error then please post here as soon as possible....thanks in advance....
Report
Re: solve this..... Posted by nikku_smartie on 14 Feb 2012 at 11:10 PM
: If you have a variable answer[10], you need to index it from
: 0 to 9 (not 1 to 10).

:
:
thanks for reply dude.....
what if i edit this.....

:char answer[QUES],choice[QUES];


to this....

:char answer[QUES+1],choice[QUES+1];

ithink it is ok by you.....if yes then run it and it is also giving runtime error......dude if you can write this code correctly i mean without runtime/compile time error then please post here as soon as possible....thanks in advance....



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.