-1

Below is only a partial function I am trying to execute of a bigger function. I am trying to assign a case number that will convert an integer returned from this function into a string. However, I don't know what the issue is, but this is constantly outputting 0 for some reason. What am I doing wrong?

#include <iostream>
#include <string>

using namespace std;

int daysFromnow;
int caseNumber;

int caseCalc(int daysFromnow);

int main()
{
    cout << "Please enter how many days from now" << endl;
    cin >> daysFromnow;
    cout << caseCalc(daysFromnow);
}

int caseCalc(int daysFromnow)
{
    daysFromnow + 7;
 
    if (daysFromnow % 7 == 0)
    {
        caseNumber == 1;
    }
    else if (daysFromnow % 7 == 1)
    {
        caseNumber == 2;
    }
    else if (daysFromnow % 7 == 2)
    {
        caseNumber == 3;
    }
    else if (daysFromnow % 7 == 3)
    {
        caseNumber == 4;
    }
    else if (daysFromnow % 7 == 4)
    {
        caseNumber == 5;
    }
    else if (daysFromnow % 7 == 5)
        {
        caseNumber == 6;
    }
    else if (daysFromnow % 7 == 6)
    {
        caseNumber == 7;
    }

    return caseNumber;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
BluMinto
  • 1
  • 1
  • 1
    `daysFromnow + 7;` this statement does nothing. – Jeffrey Dec 22 '21 at 22:11
  • 3
    `caseNumber == 7;` this statement also does nothing. Turn on compiler warnings. If you don't know how, please find out. – JohnFilleau Dec 22 '21 at 22:13
  • You can use a `switch` statement instead of those if-else statements. – digito_evo Dec 22 '21 at 22:23
  • 1
    You don't need need a `switch`, just add `+ 1` to the result of `% 7`, eg: `int caseCalc(int daysFromnow) { return ((daysFromnow + 7) % 7) + 1; }` But what is the point of asking the user for "days from now" if you are not actually using "now" in your calculation? When manipulating dates/times, have a look at the [``](https://en.cppreference.com/w/cpp/header/chrono) library. For instance, add `daysfromnow` to [`std::chrono::system_clock::now()`](https://en.cppreference.com/w/cpp/chrono/system_clock/now) and then handle the result as needed. – Remy Lebeau Dec 22 '21 at 22:26
  • 1
    Note that `caseNumber == 7` is COMPARISON(`==`), change it to ASSIGNMENT(`=`): `caseNumber = 7;` You are not actually modifying caseNumber in your function. – Marc Stevens Dec 22 '21 at 22:28

1 Answers1

0

As suggested by @Remy Lebeau, you can simplify caseCalc a lot.
Here it is:

#include <iostream>

int daysFromNow { };
int caseNumber { };

int caseCalc(const int daysFromNow);

int main( )
{
    std::cout << "Please enter how many days from now: ";
    std::cin >> daysFromNow;
    std::cout << caseCalc(daysFromNow);
}

int caseCalc(const int daysFromNow)
{
    return ( daysFromNow + 7 ) % 7 + 1;
}

Also as a side note check this: "using namespace std;", considered bad practice?

Another side note: "std::endl" vs "\n"

digito_evo
  • 3,216
  • 2
  • 14
  • 42
  • 1
    Hi thanks for the response. The reason I was using namespace and didn't solve the calculation in that one function was because it was a question I remembered from an exam that was stated in that way. Thanks. – BluMinto Dec 22 '21 at 23:53