Thread: Need a little bit of help!

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    7

    Need a little bit of help!

    The program is pretty much done. It's a program to split an input such as 20110102 into 01/02/2011. I already have the loop in place, but for some reason when I output the program it gives me huge numbers. Please tell me what's wrong in my void function. Thank you.

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    
    
    
    void date (int fulldate, int month, int day, int year)
    {
    
    
    year = (fulldate/100/100);
    month = (fulldate0);
    day = ((fulldate/100)0);
    
    
    }
    
    
    
    
    int main()
    {
    
    
    rerun:
    int fulldate, month, day, year;
    char a;
    
    
    cout << "Enter a long integer of the form yyymmdd: "<< endl;
    cin >> fulldate;
    
    
    cout << "The date is " << month << "/" << day << "/" << year <<endl;
    
    
    cout << "Would you like to try again? [y/n]" << endl;
    cin >> a;
        if (a == 'y' || a == 'Y')
            goto rerun;
            
        else
            return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A few things:
    • You should properly indent your code.
    • Replace your label and goto with a loop construct, e.g., a do while loop.
    • It probably makes more sense to read the input as a string, not as an integer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    spaghetticode
    Guest
    You actually never use your function. You just cout your uninitialised variables month, day and year.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    Oh edit, I actually solved it by putting & on the ints in the void function as well as adding the date(), ty.

    Code:
    
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    
    
    
    void date (int& month, int& day, int& year, int& fulldate)
    {
    
    
    year = (fulldate/10000);
    day = (fulldate0);
    month = ((fulldate/100)0);
    
    
    
    
    return;
    
    
    }
    
    
    
    
    int main()
    {
    
    
    rerun:
    int month, day, year, fulldate;
    char a;
    
    
    cout << "Enter a long integer of the form yyyymmdd: "<< endl;
    cin >> fulldate;
    
    
    date (month, day, year, fulldate);
    cout << "The date is " << month << "/" << day << "/" << year <<endl;
    
    
    cout << "Would you like to try again? [y/n]" << endl;
    cin >> a;
    	if (a == 'y' || a == 'Y')
    		goto rerun;
    		
    	else
    		return 0;
    }
    Last edited by Sephyx; 10-15-2011 at 05:41 PM.

Popular pages Recent additions subscribe to a feed