Home > front end >  If else function constantly outputting 0 instead of a number conditioned
If else function constantly outputting 0 instead of a number conditioned

Time:12-23

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;
}

CodePudding user response:

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"

  •  Tags:  
  • c
  • Related