Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I want the user to input the day number as the value of the parameter of the called function using cin

So, as you see in the question I want to make the user input the value of the arguement daynum down when I call the function getday, and not me who enters it. However I can’t seem to get it right. I have tried cin << getday(); but it’s wrong I looked in the internet to get an idea I guess and I tried getday(cin); but still it’s wrong kinda clueless here about how to use cin with the function call.

#include <iostream>

using namespace std;

string getday(int daynum){
    string dayname;
switch(daynum){
case 0:
    dayname = "sunday";
    break;
case 1:
    dayname = "Monday";
    break;
case 2:
    dayname = "Tuesday";
    break;
default:
   dayname = "invalid day number";


}
return dayname;
}


int main()
{

cout << "Enter daynum" << endl;



    return 0;
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You have to create a temporary variable, fill it with user value using cin, and then pass it to your function:


string getday(int daynum)
{
    string dayname;
    switch (daynum)
    {
        case 0:
            dayname = "sunday";
            break;
        case 1:
            dayname = "Monday";
            break;
        case 2:
            dayname = "Tuesday";
            break;
        default:
            dayname = "invalid day number";
    }
    return dayname;
}

int main()
{
    cout << "Enter daynum" << endl;
    int daynum;
    cin >> daynum;
    cout << getday(daynum) << endl;

    return 0;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading