Completed Challenge # 2, I think?

+2 Dakota Spencer · January 23, 2015
Well, here it is, it's a bit excessive I think. Please do criticize:


#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <time.h>

int main()
{
    srand ( time(NULL) );
    int i;
    int diceRoll;
    int roll1=0;
    int roll2=0;
    int roll3=0;
    int roll4=0;
    int roll5=0;
    int roll6=0;
    int finalRoll=0;
    int total=0;
    char decision;
    char rollOrNot;
    int valid;
    int valid2;

        printf("Would you like to roll the first 3 di? (y/n):");
        scanf(" %s", &rollOrNot);
        rollOrNot = tolower(rollOrNot);
            if(rollOrNot == 'y' || 'n'){
                valid2 = 1;
        } else {
                valid2 = 0;
        }

            while(valid2 == 0){
                printf("Sorry, that was an invalid response, try again: \n Would you like to roll the first 3 di? (y/n):");
                scanf(" %s", &rollOrNot);
        rollOrNot = tolower(rollOrNot);
                   if(rollOrNot == 'y' || 'n'){
                        valid2 = 1;
                    } else {
                        valid2 = 0;
                        }
                }
        if(rollOrNot == 'y'){


    for(i=1; (i<7); i++){
        diceRoll = (rand()%6)+1;
                switch(i){
        case 1 : roll1 = diceRoll;
                break;
        case 2 : roll2 = diceRoll;
                break;
        case 3 : roll3 = diceRoll;
                break;
        case 4 : roll4 = diceRoll;
                break;
        case 5 : roll5 = diceRoll;
                break;
        case 6 : roll6 = diceRoll;
                break;
        default : puts("Sorry, something went wrong.");
                break;
        }
    }
    total = (roll1 + roll2 + roll3);
    finalRoll = (roll4 + roll5 + roll6);
    printf("Your rolls were %d, %d, and %d, for a total of %d. Will your next three be higher, lower, or the same when added together? (h/l/s):", roll1, roll2, roll3, total);
    scanf(" %s", &decision);
        decision = tolower(decision);
        if(decision == 'h' || 'l' || 's'){
            valid = 1;
        } else {
            valid = 0;
        }

            while(valid == 0){
                printf("Sorry, that was an invalid response, try again: \n Will that be higher, lower, or the same? (h/l/s):");
                scanf(" %s", &decision);
                    decision = tolower(decision);
                   if(decision == 'h' || 'l' || 's'){
                        valid = 1;
                    } else {
                        valid = 0;
                        }
                }
    i = 0;
        if((finalRoll == total) && (decision == 's')){
            printf("Congratulations, you were correct, your rolls were %d, %d, and %d for a total of %d.", roll4, roll5, roll6, finalRoll);
        } else if((finalRoll > total) && (decision == 'l')){
            printf("Congratulations, you were correct, your rolls were %d, %d, and %d for a total of %d.", roll4, roll5, roll6, finalRoll);
        } else if((finalRoll < total) && (decision == 'h')){
            printf("Congratulations, you were correct, your rolls were %d, %d, and %d for a total of %d.", roll4, roll5, roll6, finalRoll);
        } else {printf("Sorry, thats incorrect, your rolls were %d, %d, and %d for a total of %d.", roll4, roll5, roll6, finalRoll);
        }
        } else {
            printf("We hope you come play later!");
        }
    return 0;
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Joe Long · January 25, 2015
Yeah I ran this code and the only problem I had with it was it was saying a smaller number was bigger than a larger number. I just swapped the > and < on the last few lines and it works like it should now
0 Dakota Spencer · January 24, 2015
No, after further inspection, the "checking" method I used to see if the inputted value was a valid one is incorrect.
0 c student · January 23, 2015
is this supposed to happen?

http://i.imgur.com/ckofsAX.png
  • 1

C

107,129 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator