Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My code isn\'t running correctly. I have to prompt the user for two positive num

ID: 672868 • Letter: M

Question

My code isn't running correctly. I have to prompt the user for two positive numbers, but when I do that, the program responds as if I entered a negative number? can anyone help? Also printf("Would you like to continue?(y/n)"); has the similar problem.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int finalPopulation(int size, int num_days){
   if (num_days <= 1){
       return size;
   }
   else{
       return finalPopulation(size, num_days - 1) + finalPopulation(size, num_days - 2);
   }
}

int main()
{
   int size, num_days;
   char str[100];
   while (1){
           printf("Please enter the initial size of the green curd: ");
           scanf("%d", &str);
           size = (char)str;
           if (size <= 0){
               printf("I'm sorry that value is unrecognized or is negative. Please enter the initial size of the green curd: ");
               scanf("%d", &str);
           }
           else
           {
               break;
           }
       }
       printf("Please enter the number of days: ");
       scanf("%d", &num_days);
       printf("With an initial population of %d pounds of crud growing for %d days. thefinal population would be %d pounds. ", size, num_days, finalPopulation(size, num_days / 5));
       while (1){
           printf("Would you like to continue?(y/n)");
           scanf("%c", &str);
           if (str[0] == 'n'){
               return 0;
           }
           else if (str[0] == 'y'){
               break;
           }
           else{
               printf("I am sorry that answer is not recognized. ");
           }
       }
       return 0;
   }

Explanation / Answer

In your program have some errors
you mentioned below code in your program
size = (char) str;

you took size as a string .Strings are basically an array of characters the size should be taken as an int since it is a number. it is Not possible convert a
string to integer

Please find below program

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int finalPopulation(int size, int num_days) {
    if (num_days <= 1) {
        return size;
    } else {
        return finalPopulation(size, num_days - 1) + finalPopulation(size, num_days - 2);
    }
}

int main() {
    int size, num_days;
    char str[100];
    while (1) {
        printf("Please enter the initial size of the green curd: ");
        scanf("%d", &size);

        if (size <= 0) {
            printf("I'm sorry that value is unrecognized or is negative. Please enter the initial size of the green curd: ");
            scanf("%d", &size);
        } else {
            break;
        }
    }
    printf("Please enter the number of days: ");
    scanf("%d", &num_days);
    printf("With an initial population of %d pounds of crud growing for %d days. thefinal population would be %d pounds. ", size, num_days, finalPopulation(size, num_days / 5));
    while (1) {
        printf("Would you like to continue?(y/n)");
        scanf("%s", &str);
        if (str[0] == 'n') {
            return 0;
        } else if (str[0] == 'y') {
            break;
        } else {
            printf("I am sorry that answer is not recognized. ");
        }
    }
    return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote