Thread: First time Post/First Time Question

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    5

    First time Post/First Time Question

    Code:
     #include<stdio.h>#include<stdlib.h>
    #include<time.h>
    main()
    {
        float choice=0, result=0, cnt=0;
        int ans;
        time_t t;
        do
        {
            srand(time(&t));
            printf("pick a number from 1 to 4:\n");
            scanf("%f", &choice);
            if (choice<=4 && choice>=0)
            {
                result=(rand()%3)+1;
            }
            else
            {
                printf("I'm sorry, Please pick a number within the stated range");
                continue;
            }
            if (choice==result)
            {
                printf("YAY!!!! You got it right!\n!");
                cnt= cnt + 1 ;
                printf("Your score is %d\n", cnt);
                printf("Would you like to try again?\n");
    
    
            }
            else
            {
                printf("OOOOHHHH!!!! Better luck next time.\n\n " );
                printf("Your score is %d\n", cnt);
                printf("Try again?\n\n");
            }
                printf("Please answer with yes=0 or no=1.\n\n\n");
                scanf(" %d", &ans);
    
    
        }while(ans= 1);
        return 0;
    }

    Above is my program for rolling a random number between 1 and 4. When I try to keep going by pressing '0', the program still ends.
    I've been self teaching myself C and have been doing well so far up to this point so perhaps it's something subtle.

    Also, could someone guide me on how I would introduce a string into the "While" statement. I initially tried to have the user type in "yes" or "no" but to no luck.


    Thank you for your time.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Call srand only once at the beginning of your program, don't call it repeatedly.

    Time doesn't necessarily need a valid pointer, you can give it NULL and it simply returns the time.

    Assignment(=) isn't the same as equality(==). I'm talking about line #41.
    Last edited by GReaper; 06-12-2017 at 03:05 PM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jun 2017
    Posts
    5
    I greatly appreciate your help GReaper. I have now tried to change the yes and no response from binary to strings. Attached is what I have got...

    Code:
    #include<stdio.h>#include<stdlib.h>
    #include<time.h>
    main()
    {
        float choice=0, result=0;
        int cnt=0;
        char ans[4];
        //time_t t;
        do
        {
            //srand(time(&t));
            printf("pick a number from 1 to 2:\n");
            scanf(" %f", &choice);
            if (choice<=2 && choice>=0)
            {
                result=(rand()%2)+1;
            }
            else
            {
                printf("I'm sorry, Please pick a number within the stated range");
                continue;
            }
            if (choice==result)
            {
                printf("YAY!!!! You got it right!\n!");
                cnt= cnt + 1 ;
                printf("Your score is %d\n", cnt);
                printf("Would you like to try again?\n");
    
    
            }
            else
            {
                printf("OOOOHHHH!!!! Better luck next time.\n\n " );
                printf("Your score is %d\n", cnt);
                printf("Try again?\n\n");
            }
                printf("Please answer with yes or no.\n\n\n");
                scanf(" %s", ans);
    
    
        }while(ans=="yes");
        return 0;
    }
    I'm still coming across the issue of having the program shutting down even if I type "yes". Will I need to use strcpy somewhere or is there something else I'm missing?

    Thanks Again.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I would propose a slightly different way of structuring your code:
    Code:
    do
        while true
            print prompt for number choice
            choice = user input
            if choice is not within range
                print message informing user choice is not within range
            else
                break
    
        result = random number within range
    
        if choice == result
            print congratulations
            increase score
            print score
            print prompt to try again
        else
            print message informing user that choice was not succesful
            print score
            print prompt to try again
    
        ans = user input
    while ans == "yes"
    Things to note:
    • Are you sure you want to deal with floating point numbers? It looks like you should be dealing with integers instead. If you really want floating point numbers, e.g., you want to allow the user to enter a choice such as 1.23, then you must consider that floating point numbers have a certain inaccuracy that may make your comparison for the result fail even when you think it should succeed.
    • String comparison in C is done with strcmp and similiar functions because if you use ans=="yes" as in your code, it would actually compare the address of the first element of ans with the address of the first character of the string literal "yes" rather than comparing the string values. Since ans is a different array from "yes", this is always result in 0 (i.e., false).
    • You commented it out, but the srand call should be placed earlier in main, before the loop.
    • main should be explicitly declared as returning int, and although unnecessary for a function definition, it is good to have void as the parameter list since the function takes no arguments. This would match the forward declaration, where leaving out the void would mean that the function takes an unknown number of arguments. Of course, you will not be forward declaring the main function, but it is good practice for when you write other functions.
    • Instead of hardcoding 2 or 4 etc, declare a constant and use it, e.g.,
      Code:
      const int range_upper_limit = 2;
      /* ... */
      printf("pick a number from 1 to %d:\n", range_upper_limit);
      /* ... */
      result = (rand() % range_upper_limit) + 1;
    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

  5. #5
    Registered User
    Join Date
    Jun 2017
    Posts
    5
    Thank you very much for your response laserlight,

    From what I've looked up regarding the use of strcmp, will I need to create another variable to compare with the variable "ans" ?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, you can compare ans directly with "yes" using strcmp. Note that strcmp does not return a boolean value.
    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

  7. #7
    Registered User
    Join Date
    Jun 2017
    Posts
    5
    Alright, I'm just about done with this little program of mine. Actually, it's already done.
    My program is still as posted above with the exception that I have created a char r[]="yes"; array.
    My "do..while" function now looks like this at its end:
    Code:
     while( !strcmp(r,ans));
    I'm trying to understand why it makes sense to use the "not" operator.
    Thank you.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by mikeman92 View Post
    Alright, I'm just about done with this little program of mine. Actually, it's already done.
    My program is still as posted above with the exception that I have created a char r[]="yes"; array.
    My "do..while" function now looks like this at its end:
    Code:
     while( !strcmp(r,ans));
    I'm trying to understand why it makes sense to use the "not" operator.
    Thank you.
    Lookup the value returned strcmp - C++ Reference

    Edit: I would use the below instead.
    Code:
     while( 0 == strcmp(r,ans));
    Tim S.
    Last edited by stahta01; 06-20-2017 at 05:36 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    Jun 2017
    Posts
    5
    Thank you for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how convert uint32_t time to human read form of time?
    By barracuda in forum C Programming
    Replies: 19
    Last Post: 03-21-2015, 12:29 AM
  2. Replies: 5
    Last Post: 04-17-2013, 11:32 PM
  3. Replies: 2
    Last Post: 04-17-2013, 12:25 AM
  4. Replies: 7
    Last Post: 11-21-2009, 01:02 AM
  5. 1st post by 1st time C coder
    By Servo Wizard in forum C Programming
    Replies: 22
    Last Post: 09-09-2007, 08:18 AM

Tags for this Thread