Thread: HELP with coding.....

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sure, and I wil give HINTS to solutions, but part of learning to program is to learn how to "think" in terms of "how to solve problems using a computer". And that's not always that easy.

    Giving you the right solution directly is not going to help you.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Registered User
    Join Date
    Aug 2007
    Posts
    39
    I have tried a few lines of coding...dunno if im right.....please anyone cheK it out...
    Code:
    void main()
    {
    	int count;
    	char ans[6];
    
    	printf("\n Welcome to the QuiZ Competition....R u READY????");
    
    
    	for (count=1; count<3; ++count)
    
    	{
    		printf("\n What is the date of birth of Gandhi?");
    		scanf("&#37;s",&ans);
    		if (ans=02.10.69)
    			break;
    	}
    
    	{
    		printf("\n The answer u stupid is 02.10.69");
    	}
    	}
    Last edited by shaheel; 11-08-2007 at 10:14 AM.

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Good start. I would probably start the loop with 0 - that's the traditional way to do loops.

    Also, you just loop around asking, not checking if the answer is right (and breaking the loop when this happens).

    Edit: And, if you expect the user to actually input "02.10.69", you may want to have a string longer than 6 chars - consider that:
    there are 6 positions for the date itself, then two dots, and all strings also need a "ending zero". So you are at least 3 short.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    you cannot simply put

    ans=02.10.69

    first because of the == vs = and second because 02.10.69 is a string "02.10.69"
    if you want to compare string:

    #include <string.h>
    ....

    if(strcmp(ans,"02.10.69")==0) break;

  5. #20
    Registered User
    Join Date
    Aug 2007
    Posts
    39
    ok...thanks..I'll try that..But still Im having an error to compile that..
    At:
    The "if" statement in the loop....

  6. #21
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    Quote Originally Posted by shaheel View Post
    ok...thanks..I'll try that..But still Im having an error to compile that..
    At:
    The "if" statement in the loop....
    can you repost the code?

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It also helps if you post the EXACT error message from the compiler. It can be hard to spot some subtle errors without the information of what the compiler says.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #23
    Registered User
    Join Date
    Aug 2007
    Posts
    39
    here is the new coding...no errors..no warnings...:
    Code:
    #include <string.h>
    #include <stdio.h>
    void main()
    {
    	int count;
    	char ans[9];
    
    	printf("\n Welcome to the QuiZ Competition....R u READY????");
    
    
    	for (count=1; count<3; ++count)
    
    	{
    		printf("\n What is the date of birth of Gandhi?");
    		scanf("&#37;s",&ans);
    		if(strcmp(ans,"02.10.69")==0) break;
    
    		else printf("\n The answer u stupid is 02.10.69");
    	}
    
    	{
    		printf("\n Fabulous worK..Ur Great...");
    	}
    	}

  9. #24
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    I know it is upsetting... but trying to rewrite the same program with "while loop" could be useful...

  10. #25
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That looks about right. What's not working is that you don't say "bravo" when the user gets it right - and perhaps you want to say "try again" when the user gets it wrong.
    And you don't really need the braces around the answer - but if you want to say "stupid" then you probably should only print it when the answer is wrong.

    You can solve most of the "do this" or "don't do this" by adding a variable to say "correct" or "not correct", e.g "correcAnswer = 0" to begin with, and if you get a correct answer, set "correctAnswer = 1". Then when the loop ends [for whatever cause] you check the variable "correctAnswer" to see if it was correct or not.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #26
    Registered User
    Join Date
    Aug 2007
    Posts
    39
    well..I'll try to put ur advices in practice.... I was about to ask this very much..if there could be a way to store the corect answerrs in a particular function and another function where the score of the user may be calculated.....

  12. #27
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    Quote Originally Posted by shaheel View Post
    here is the new coding...no errors..no warnings...:
    doesn't your compiler give you a warning about return type of 'main' ? It should be int...

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    To add on to what matsp advised: change void main() to int main(), then include a return 0; statement at the end of main() if you are not compiling with respect to the C99 standard.

    Also, I believe you should actually use:
    Code:
    scanf("&#37;s", ans);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #29
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by shaheel View Post
    well..I'll try to put ur advices in practice.... I was about to ask this very much..if there could be a way to store the corect answerrs in a particular function and another function where the score of the user may be calculated.....
    You could write a function to ask a question and compare it to a correct answer. And you could also either call this function from another function that sums up the number of right answers, or call a function that updates a score for the person when the answer is right. Or something like that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #30
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    Quote Originally Posted by shaheel View Post
    well..I'll try to put ur advices in practice.... I was about to ask this very much..if there could be a way to store the corect answerrs in a particular function and another function where the score of the user may be calculated.....
    the "score" of the user is the variable "count" (maybe +1 if loop start from zero.) what do you mean for store the corect answerrs in a particular function ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-20-2009, 05:22 PM
  2. Coding Guideline ....!!
    By imfeelingfortun in forum Tech Board
    Replies: 8
    Last Post: 10-08-2006, 07:09 AM
  3. Before Coding
    By cyberCLoWn in forum C++ Programming
    Replies: 16
    Last Post: 12-15-2003, 02:26 AM
  4. Coding Contest....
    By Koshare in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 10-14-2001, 04:32 PM