Need a little bit of help!

This is a discussion on Need a little bit of help! within the C++ Programming forums, part of the General Programming Boards category; The program is pretty much done. It's a program to split an input such as 20110102 into 01/02/2011. I already ...

  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
    21,744
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21