-4

Can someone explain to me what I'm doing wrong with the syntax below? My textbook doesn't have any examples of user-defined string functions and I can't figure out what I should be doing. Thanks in advance!

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>

string dayOfWeek(int day);

using namespace std;    

int main()
{
    cout << "Please enter a day of week (0 for Sunday, 1 for Monday, etc): ";
    cin >> day;
    cout << "The name of the day of the week is: " dayOfWeek(day) << endl;
}

string dayOfWeek(int day)
{
    if (day == 0)
        return "SUNDAY";
    else if (day == 1)
        return "MONDAY";
    else if (day == 2)
        return "TUESDAY";
    else if (day == 3)
        return "WEDNESDAY";
    else if (day == 4)
        return "THURSDAY";
    else if (day == 5)
        return "FRIDAY";
    else if (day == 6)
        return "SATURDAY";
}
BrianW
  • 11
  • 4

2 Answers2

2
#include <string>
#include <iostream>

std::string dayOfWeek(int day)
{
    if (day == 0)
        return "SUNDAY";
    else if (day == 1)
        return "MONDAY";
    else if (day == 2)
        return "TUESDAY";
    else if (day == 3)
        return "WEDNESDAY";
    else if (day == 4)
        return "THURSDAY";
    else if (day == 5)
        return "FRIDAY";
    else if (day == 6)
        return "SATURDAY";

    return "Invalid input!"; // May be you should guarantee a return value.
 }

int main() {
    std::cout << dayOfWeek(5) << std::endl;
}

should work as expected.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
2

The crux of the problem is here:

string dayOfWeek(int day);

using namespace std;

The using is after code that depends on it, so the compiler goes looking for string and can't find it. It doesn't go looking for std::string because it hasn't been told to yet.

You could move using up a few lines, but for the many reasons covered in the answers to Why is "using namespace std" considered bad practice? and a few not covered, you're probably better off not putting

using namespace std;

in your code at all. Prefix the std:: explicitly and you can avoid a litany of nasty surprises.

I also recommend following @πάνταῥεῖ example with where the function was placed. Forward declaring and then declaring gives you two places to have to change code and one more place to have a bug. Declaring the function ahead where it will be used mean you only ever have to change one declaration.

Putting it all together with a few other small tweaks:

#include <string>
#include <iostream>

std::string dayOfWeek(int day)
{
    switch (day) // for stuff like this, I like a switch. I find it looks nicer.
    {
        case 0:
            return "SUNDAY";
        case 1:
            return "MONDAY";
        case 2:
            return "TUESDAY";
        case 3:
            return "WEDNESDAY";
        case 4:
            return "THURSDAY";
        case 5:
            return "FRIDAY";
        case 6:
            return "SATURDAY";
        default:
            return "No such day"; // functions must always return something. 
                                  // in this case what will cout print if you 
                                  // don't return a string? Don't know?
                                  // Don't feel bad. Neither do I.
                                  // Welcome to Undefined Behaviour

    }
 }

int main()
{
    int day; //this was missing
    std::cout << "Please enter a day of week (0 for Sunday, 1 for Monday, etc): ";
    std::cin >> day;
    std::cout << "The name of the day of the week is: " << dayOfWeek(day) << std::endl;
}

There's one other trick you can use to get rid of the if or switch entirely

#include <string>
#include <iostream>


// define an array of days of the week to print. const means dayOfWeek cannot be changed
const std::string dayOfWeek[] = 
{
    "SUNDAY",
    "MONDAY",
    "TUESDAY",
    "WEDNESDAY",
    "THURSDAY",
    "FRIDAY",
    "SATURDAY"
 };

int main()
{
    unsigned int day; 
    //Note unsigned int. Negative numbers are now disallowed

    std::cout << "Please enter a day of week (0 for Sunday, 1 for Monday, etc): ";

    // keep looping until user provides a good number
    while (!(std::cin >> day) // user MUST input a number
           || day > 6) // number is a usable number
    {
        std::cin.clear(); // clean up bad input
        std::cout << "Please enter a day of week (0 for Sunday, 1 for Monday, etc): ";
    }
    // since we know day is a usable (0..6) number we can read the day out of the array   
    std::cout << "The name of the day of the week is: " << dayOfWeek[day] << std::endl;
}
Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 1
    Other problems include no `<<` before the use of the function `dayOfWeek`, no definition of `day`. – Fantastic Mr Fox Jun 28 '16 at 22:57
  • Feel fre to include this example of compiling code: http://ideone.com/XMxc03 – Fantastic Mr Fox Jun 28 '16 at 22:58
  • Didn't see that. Missing `day` could ruin your whole week. – user4581301 Jun 28 '16 at 22:58
  • that was a silly mistake on my part. I was just copying code from my larger program and accidentally put the namespace in the wrong spot. I've heard about the issues "using namespace std" can cause but I'm taking a college class and my professor said to use it so I figure I better stick with it for now. – BrianW Jun 28 '16 at 22:59
  • @BrianW Rebel! Don't follow the institution. Also read your compiler errors next time, and certainly include them in all future questions. – Fantastic Mr Fox Jun 28 '16 at 23:00
  • @Ben where would I define day? I'm down from 16 errors to 3 so I'm getting there! Ha – BrianW Jun 28 '16 at 23:00
  • nevermind. I see what you mean. Another silly mistake. – BrianW Jun 28 '16 at 23:01
  • I like your suggestion. Why not go all the way and define the array as: `constexpr char const* week_days[] = { ... };` so that compile-time int-to-week-day transformations are possible? The only negative part is `char const*` is used instead of `std::string`. – user2296177 Jun 29 '16 at 01:07
  • @user2296177 because I didn't think of it. Since it's constant, most of the `char` array nasties are moot. I call that a good improvement unless you have a number of cases that convert `char` array to `string`. Huh. Just spotted a bug. Need brackets around `std::cin >> day`. – user4581301 Jun 29 '16 at 01:18