-2

so the 3rd function calls on the read_integer(prompt) from the int read_integer(string prompt) above it, seen by the ***** marks, but I am not sure what to add so that this works. My attempt in the below code doesn't spit out the month after the user puts an integer from 1-12. The function is to utalise structs and enums to read out a knights name, age, armour, and birthmonth. It is the birthmonth that doesnt work so far. I am a beginner coder btw

#include "splashkit.h"
#include <string>

using namespace std;

string read_string(string prompt)
{
    string result;
    write(prompt);
    result = read_line();
    return result;
}

// **********
int read_integer(string prompt)
{
    string result;
    write(prompt);
    result = read_line();
    return convert_to_integer(result);
}
// ***********

int read_birthmonth(string prompt)
{
    string line;
    int monthno;
    monthno = read_integer(prompt);

    while (monthno > 12) // forces the user to only enter integers up to 12
    {
        write_line("Please enter a value from 1 to 12: ");
        monthno = read_integer(prompt);
    }
    return monthno;
}

int read_armour(string prompt)
{
    string line;
    string armour;
    int armourno;
    armourno = read_integer(prompt);

    while (armourno != 1 && armourno != 2) // while loop makes sure the user has to pick either 1 or 2
    {
        write_line("Please enter 1 for Chain Mail and 2 for  Plate Armour: ");
        armourno = read_integer(prompt);
    }

    return armourno;
}

string select_armour(int armourno) // function to return armour type, from 1 or 2 via prompt
{
    string armourtype;
    if (armourno == 1)
    {
        armourtype = "Chain Mail"; // if this chosen it returns chain mail
    }
    else
    {
        armourtype = "Plate Armour"; // else it returns plate armour
    }
    return armourtype;
}

string select_month(int monthno) // use a switch function so that a number from 1 to 12 can be picked
{
    string monthname;
    switch (monthno)
    {
    case 1:
        monthname = "January";
        break;

    case 2:
        monthname = "February";
        break;

    case 3:
        monthname = "March";
        break;

    case 4:
        monthname = "April";
        break;

    case 5:
        monthname = "May";
        break;

    case 6:
        monthname = "June";
        break;

    case 7:
        monthname = "July";
        break;

    case 8:
        monthname = "August";
        break;

    case 9:
        monthname = "September";
        break;

    case 10:
        monthname = "October";
        break;

    case 11:
        monthname = "November";
        break;

    case 12:
        monthname = "December";
        break;
    }
}

struct knight_data
{
    string name;
    int age;
    string birthmonth;
    string armour;
};

knight_data read_knight() // questions asked to user
{
    knight_data result;
    result.name = read_string("Enter your name, knight: ");
    result.age = read_integer("How old are you?: ");
    int birthmonthno = read_birthmonth("What month were you born in (month number 1-12)?: ");
    result.birthmonth = select_month(birthmonthno); // based of the  number from 1 to 12
    int armourno = read_armour("What armour do you have? 1. for hain Mail, or 2. for Plate Armour: ");
    result.armour = select_armour(armourno);

    return result;
}

void write_knight(const knight_data &knight) // outputing
{
    write_line("Greetings Knight: " + knight.name + "\nAged:  " + to_string(knight.age));
    write_line("Your birth month is: " + knight.birthmonth);
    write_line("Your armour type is: " + knight.armour);
}

enum knight_update_option // enumerator function
{
    UPDATE_NAME,
    UPDATE_AGE,
    UPDATE_BIRTH_MONTH,
    UPDATE_ARMOUR,
    FINISH_UPDATE

};

knight_update_option read_knight_data_option() // when choose a number it updates it via enumeralot
{
    int result;
    write_line("1: Update name");
    write_line("2: Update age");
    write_line("3: Update birth month");
    write_line("4: Update armour");
    write_line("5: Finish update");
    result = read_integer("Select option: ") - 1;

    return static_cast<knight_update_option>(result);
}

void update_knight(knight_data &knight) // actually updating values via switch
{
    int option;
    int birthmonthno;
    int armourno;

    do
    {
        write_line();
        write_line("***Update Knight***");
        write_knight(knight);
        write_line();
        option = read_knight_data_option(); // the 1-5 updates from lines 180 - 184
        switch (option)
        {
        case UPDATE_NAME:
            knight.name = read_string("Enter new name: ");
            break;

        case UPDATE_AGE:
            knight.age = read_integer("Enter new age: ");
            break;

        case UPDATE_BIRTH_MONTH:
            birthmonthno = read_birthmonth("What new month were you born in (month number 1-12)?: ");
            knight.birthmonth = select_month(birthmonthno);
            break;

        case UPDATE_ARMOUR:
            armourno = read_armour("What new armour do you have? 1. for Chain Mail, or 2. for Plate Armour: ");
            knight.armour = select_armour(armourno);
            break;

        case FINISH_UPDATE:
            break;
        }

    } while (option != FINISH_UPDATE);
}

int main() // main function - calls on read, write, and update knight functions
{
    knight_data k1, k2;
    k1 = read_knight();
    write_knight(k1);
    update_knight(k1);
    write_line("After the update:");
    write_knight(k1);
    return 0;
}
  • 1
    Enable all warnings and see [Why should I always enable compiler warnings?](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings). – Jason Aug 25 '22 at 06:25

1 Answers1

0

You are not returning anything from select_month. Add:

return monthname;