Thread: Need help (maybe it's loops i need, dunno)

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    4

    Arrow Need help (maybe it's loops i need, dunno)

    Hi guys,
    I have run into a small problem. I am trying to make this "daily planner" thing and I have a question.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main (void)
    {
        int points;
        int score;
        points = 5;
        score = 0;
       
        const char *key1 = "Monday";
        const char *key2 = "Tuesday";
        const char *key3 = "Wednesday";
        const char *key4 = "Thursday";
        const char *key5 = "Friday";
        const char *key6 = "Saturday";
        const char *key7 = "Sunday";
        char day[10]; 
        
        printf("Your current score is: %d.\n", score);
        printf("What day is it today?\n");
        scanf("%s", day);
       
        if(strcmp (key1, day) ==0){
                 printf("Do this and this");
                 } else if (strcmp (key2, day) ==0) {
                        printf("Do this and this");
                       } else if (strcmp (key3, day) ==0) {
                              printf("Do this and this");
                           } else if (strcmp (key4, day) ==0) {
                                  printf("Do this and this");
                               } else if (strcmp (key5, day) ==0) {
                                      printf("Do this and this");
                                    } else if (strcmp (key6, day) ==0) {
                                           printf("Do this and this");
                                        } else if (strcmp (key7, day) ==0) {
                                               printf("Do this and this");
                                            } else {
                                                   printf("You mistyped! English mother........er, do you speak it?");
                                                   
                                                   }
                                                   
        printf("\nHave you done what you planned to accomplish?\n");
        const char *answer = "yes";
        char input[4];
        scanf ("%s", input);
        if(strcmp (input, answer)==0){
                  printf("Congratulations! You have scored 5 points!");
                  score=score+points;
                  }
                  else {
                       printf("\nWell, try harder then!");
                       }
        
        
    getchar();
    getchar();
    return 0;
    
    
    }

    Firstly: how do I make the loop so that at the mistype moment the program goes back to the question "what day is it?"? should I put the whole if and if else block into a loop or just some part of it?

    Secondly: I really dislike having to write out all of those if and if else statements :/ is there a way around it? I read up on it and some people said switch statements don't work but maybe there is something else?

    Thirdly: is there a simple way for the program to retain the memory of my score? So that when the program terminates the score is still stored somewhere?

    Thanks!
    Last edited by OmenOfDefeat; 02-07-2016 at 08:10 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,419
    Quote Originally Posted by OmenOfDefeat
    Firstly: how do I make the loop so that at the mistype moment the program goes back to the question "what day is it?"? should I put the whole if and if else block into a loop or just some part of it?
    I suggest that you do this part later. Assume correct input and get the program to work correctly for a single run first.

    Quote Originally Posted by OmenOfDefeat
    Secondly: I really dislike having to write out all of those if and if else statements :/ is there a way around it? I read up on it and some people said switch statements don't work but maybe there is something else?
    It does look like you can improve this using loops, but before you do that, you would likely need to change your numbered variable names into an array:
    Code:
    const char *keys[] = {"Monday",
                          "Tuesday",
                          "Wednesday",
                          "Thursday",
                          "Friday",
                          "Saturday",
                          "Sunday"};
    Now, you can loop from keys[0] to keys[6] by accessing keys[i] in the loop body, where i is the loop/array index.

    Likewise, your "Do this and this" text is probably going to be specific to each day, so you would need a corresponding array of texts. Alternatively, you can have a struct and combine the day names and the associated text, but if you have not learnt about structures, don't worry about this.

    Quote Originally Posted by OmenOfDefeat
    Thirdly: is there a simple way for the program to retain the memory of my score? So that when the program terminates the score is still stored somewhere?
    Yes, you could write the score to a file. Perhaps on the next run of the program, the user could enter the named of the file where the score was saved, or opt for a new game.
    Last edited by laserlight; 02-07-2016 at 10:07 AM.
    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

  3. #3
    Registered User
    Join Date
    Feb 2016
    Posts
    4
    Ok so I guess the problem now is that I am not really sure how to compare the string (user input) to the array. Do I have to make a loop to see if it == to anything in the array? Also then I wont be able to change the message for separate days right?

    Also how exactly do I save the score other than manually? I would like to incorporate that into the code itself so that it automatically updates any changes

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,419
    Quote Originally Posted by OmenOfDefeat
    Ok so I guess the problem now is that I am not really sure how to compare the string (user input) to the array. Do I have to make a loop to see if it == to anything in the array?
    Yes, the point of the array is so that you can use a loop. Your use of strcmp is applicable, except that instead of strcmp(key1, day) == 0 followed by strcmp(key2, day) == 0, etc, it will just be strcmp(keys[i], day) == 0 appearing once in the loop body.

    Quote Originally Posted by OmenOfDefeat
    Also then I wont be able to change the message for separate days right?
    I already described how you could do that: "you would need a corresponding array of texts".

    Also how exactly do I save the score other than manually? I would like to incorporate that into the code itself so that it automatically updates any changes
    I already described how you could do that: "you could write the score to a file".
    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
    Feb 2016
    Posts
    4
    Yay i think I got it ot work properly now!
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main (void)
    {
        int i = 0;
        int points;
        int score;
        points = 5;
        score = 0;
       
        char day[10]; 
        const char *key[7];
        const char *message[7];
        
        key[0] = "Monday";
        key[1] = "Tuesday";
        key[2] = "Wednesday";
        key[3] = "Thursday";
        key[4] = "Friday";
        key[5] = "Saturday";
        key[6] = "Sunday";
        
        message[0] = "Do this and thatM";
        message[1] = "Do this and thatT";
        message[2] = "Do this and thatW";
        message[3] = "Do this and thatT";
        message[4] = "Do this and thatF";
        message[5] = "Do this and thatS";
        message[6] = "Do this and thatS";
        
        printf("Your current score is: %d.\n", score);
        printf("What day is it today?\n");
        scanf("%s", day);
       
        for (i=0;i<7;i++) {
            if (strcmp(day, key[i])==0) {
                            printf("%s", message[i]);
                            break;
                            }
            i++;
            }
            
            printf("\nHave you done what you planned to accomplish?\n");
        const char *answer = "yes";
        char input[4];
        scanf ("%s", input);
        if(strcmp (input, answer)==0){
                  printf("Congratulations! You have scored 5 points!");
                  score=score+points;
                  }
                  else {
                       printf("Well, try harder then!");
                       }
            
    getchar();
    getchar();
    return 0;
    }
    Also, I am really dumb and when you say "you could write the score to a file" I don't know how exactly to do it. Is there a command I can use to get it to save? If so I don't know of any

    And maybe at last I'll stop pestering you but do you know what kind of problem does C have with spaces between words in strings that are in arrays?
    Code:
    
    
    Code:
    keys[0] = "is studying";
        keys[1] = "is doing";
        keys[2] = "play";
        keys[3] = "have just cut";
        keys[4] = "arrived";
    When I have it like this and the program is supposed to compare what was the input and what the answer was supposed to be it always then goes on to say that it's wrong even if I wrote it correctly. When I delete the spaces here and then type in the answers without them he interprets everything just fine. is there a symbol i should be putting between the words instead of spaces?


    And thank you man! This helped me a lot
    Last edited by OmenOfDefeat; 02-07-2016 at 03:32 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dunno what to do
    By Joemar P. Gava in forum C Programming
    Replies: 8
    Last Post: 09-19-2014, 02:53 AM
  2. Some error, dunno what
    By suvojit biswas in forum C Programming
    Replies: 1
    Last Post: 10-04-2012, 09:36 AM
  3. 2 for loops...dunno why its skipping loop 2
    By Axolotl in forum C Programming
    Replies: 3
    Last Post: 04-03-2003, 02:07 PM
  4. i dunno how to do loops and functions together
    By Noobie in forum C++ Programming
    Replies: 30
    Last Post: 02-03-2003, 06:05 PM
  5. I now have the DX SDK (and I dunno how to.......)
    By Stealth in forum C++ Programming
    Replies: 6
    Last Post: 11-06-2001, 08:09 PM

Tags for this Thread