lundi 7 novembre 2022

How to print random imported strings from user in C?

The assignment is as follows: Write a program that takes in courses from a user, saves them in an array, and then prints out a random course for user to complete first.

For example:

How many courses do you have homework in? 3
Course: Math
Course: English
Course: Computer Science
Math, I choose you!
// Helps a user decide which homework to do first
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
    // Prompt the user for the number of courses that they have homework in
    int n = get_int("How many courses do you have homework in? ");

    // TODO: Declare an array of courses with the correct number of elements

    // TODO: Prompt the user for their course names and store it in the array
    string courses[n];
    for (int i = 0; i < n; i++)
    {
        courses[i] = get_string("Course: ");
    }
    return(0);
    // Initialize random number generator
    // (found info on https://www.tutorialspoint.com/c_standard_library/c_function_rand.htm)
    time_t t;
    srand((unsigned) time(&t));

    // Find a random number
    int r = rand() % n;

    // TODO: Print out a random course number with index r
    printf("%s/n", courses[r]);
}

This is my code above. How do I fix the print statement to print a random string that the user imported?




Aucun commentaire:

Enregistrer un commentaire