why won't this work!??!?
Nov 20, 2008 at 10:20pm UTC
This program allows the user to input 9 month, days, and years then displays them in chronological order.
I am getting the following error in my code:
"Run-Time Check Failure #2 - Stack around the variable 'date' was corrupted."
Here is a snippet of my code...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include "stdafx.h"
using namespace std;
int convertdays(int month, int day, int year)
{
int date;
date = 0;
date = (year * 10000) + (month * 100) + day;
return date;
}
int _tmain(int argc, _TCHAR* argv[])
{
int month, day, year, date[9], x;
date[1] = 0;
date[2] = 0;
date[3] = 0;
date[4] = 0;
date[5] = 0;
date[6] = 0;
date[7] = 0;
date[8] = 0;
date[9] = 0;
for (x = 0; x < 9; x++)
{
cout << "Enter the month: " ;
cin >> month;
cout << "Enter the day: " ;
cin >> day;
cout << "Enter the year: " ;
cin >> year;
convertdays(month, day, year);
}
Last edited on Nov 21, 2008 at 12:04am UTC
Nov 20, 2008 at 10:31pm UTC
When you declare an array the first element will be [0]
1 2 3 4 5 6 7 8 9
date[0] = 0;
date[1] = 0;
date[2] = 0;
date[3] = 0;
date[4] = 0;
date[5] = 0;
date[6] = 0;
date[7] = 0;
date[8] = 0;
Nov 20, 2008 at 10:35pm UTC
Ok I tried that but I am still getting the same message.....
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include "stdafx.h"
using namespace std;
int convertdays(int month, int day, int year)
{
int date;
date = 0;
date = (year * 10000) + (month * 100) + day;
return date;
}
int _tmain(int argc, _TCHAR* argv[])
{
int month, day, year, date[8], x;
date[0] = 0;
date[1] = 0;
date[2] = 0;
date[3] = 0;
date[4] = 0;
date[5] = 0;
date[6] = 0;
date[7] = 0;
date[8] = 0;
for (x = 0; x < 9; x++)
{
cout << "Enter the month: " ;
cin >> month;
cout << "Enter the day: " ;
cin >> day;
cout << "Enter the year: " ;
cin >> year;
convertdays(month, day, year);
}
system("cls" );
cout << "\n" << date[0] << "\n\n" ;
cout << "\n" << date[1] << "\n\n" ;
cout << "\n" << date[2] << "\n\n" ;
cout << "\n" << date[3] << "\n\n" ;
cout << "\n" << date[4] << "\n\n" ;
cout << "\n" << date[5] << "\n\n" ;
cout << "\n" << date[6] << "\n\n" ;
cout << "\n" << date[7] << "\n\n" ;
cout << "\n" << date[8] << "\n\n" ;
system("pause" );
return 0;
}
Last edited on Nov 20, 2008 at 11:57pm UTC
Nov 20, 2008 at 10:39pm UTC
You reduced your date array from 9 elements to 8, so the valid indices are 0...7 inclusive and you are writing to element 8.
Nov 20, 2008 at 10:41pm UTC
???
Nov 20, 2008 at 10:54pm UTC
Last edited on Nov 20, 2008 at 10:55pm UTC
Nov 20, 2008 at 11:01pm UTC
Thank you
Nov 20, 2008 at 11:05pm UTC
One more question....any idea why I am getting an output of nine 0's??
I initialized all the date's as 0 but those values should change in the "convertdays" function...shouldn't they?
1 2 3 4 5 6 7 8 9
date[0] = 0;
date[1] = 0;
date[2] = 0;
date[3] = 0;
date[4] = 0;
date[5] = 0;
date[6] = 0;
date[7] = 0;
date[8] = 0;
then....
1 2 3 4 5 6 7 8
int convertdays(int month, int day, int year)
{
int date;
date = 0;
date = (year * 10000) + (month * 100) + day;
return date;
then...
1 2 3 4 5 6 7 8 9
cout << "\n" << date[0] << "\n\n" ;
cout << "\n" << date[1] << "\n\n" ;
cout << "\n" << date[2] << "\n\n" ;
cout << "\n" << date[3] << "\n\n" ;
cout << "\n" << date[4] << "\n\n" ;
cout << "\n" << date[5] << "\n\n" ;
cout << "\n" << date[6] << "\n\n" ;
cout << "\n" << date[7] << "\n\n" ;
cout << "\n" << date[8] << "\n\n" ;
the output is simply nine 0's....
Last edited on Nov 20, 2008 at 11:58pm UTC
Nov 20, 2008 at 11:43pm UTC
I think when I return the date value from the convertdays function to the main function, it is not being returned correctly....how do I return it to the main function WITHOUT using date as a reference parameter?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include "stdafx.h"
using namespace std;
int convertdays(int month, int day, int year)
{
int date;
date = 0;
date = (year * 10000) + (month * 100) + day;
return date;
}
int _tmain(int argc, _TCHAR* argv[])
{
int month, day, year, date[9], x;
date[0] = 0;
date[1] = 0;
date[2] = 0;
date[3] = 0;
date[4] = 0;
date[5] = 0;
date[6] = 0;
date[7] = 0;
date[8] = 0;
for (x = 0; x < 8; x++)
{
cout << "Enter the month: " ;
cin >> month;
cout << "Enter the day: " ;
cin >> day;
cout << "Enter the year: " ;
cin >> year;
convertdays(month, day, year);
}
system("cls" );
cout << "\n" << date[0] << "\n\n" ;
cout << "\n" << date[1] << "\n\n" ;
cout << "\n" << date[2] << "\n\n" ;
cout << "\n" << date[3] << "\n\n" ;
cout << "\n" << date[4] << "\n\n" ;
cout << "\n" << date[5] << "\n\n" ;
cout << "\n" << date[6] << "\n\n" ;
cout << "\n" << date[7] << "\n\n" ;
cout << "\n" << date[8] << "\n\n" ;
system("pause" );
return 0;
}
Last edited on Nov 20, 2008 at 11:57pm UTC
Nov 20, 2008 at 11:53pm UTC
I made the following change to the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#include "stdafx.h"
using namespace std;
int convertdays(int month, int day, int year)
{
int date;
date = 0;
date = (year * 10000) + (month * 100) + day;
return (date);
}
int _tmain(int argc, _TCHAR* argv[])
{
int month, day, year, date[9], x;
date[0] = 0;
date[1] = 0;
date[2] = 0;
date[3] = 0;
date[4] = 0;
date[5] = 0;
date[6] = 0;
date[7] = 0;
date[8] = 0;
for (x = 0; x < 8; x++)
{
cout << "Enter the month: " ;
cin >> month;
cout << "Enter the day: " ;
cin >> day;
cout << "Enter the year: " ;
cin >> year;
convertdays(month, day, year);
date[x] = date;
}
system("cls" );
cout << "\n" << date[0] << "\n\n" ;
cout << "\n" << date[1] << "\n\n" ;
cout << "\n" << date[2] << "\n\n" ;
cout << "\n" << date[3] << "\n\n" ;
cout << "\n" << date[4] << "\n\n" ;
cout << "\n" << date[5] << "\n\n" ;
cout << "\n" << date[6] << "\n\n" ;
cout << "\n" << date[7] << "\n\n" ;
cout << "\n" << date[8] << "\n\n" ;
system("pause" );
return 0;
}
........now I am getting the following error:
error C2440: '=' : cannot convert from 'int [9]' to 'int'
Last edited on Nov 20, 2008 at 11:56pm UTC
Nov 21, 2008 at 12:44am UTC
You can't make a specific element of the array be they whole array Oo...did you mean to put "day" instead of "date"?
Nov 21, 2008 at 12:52am UTC
no I dont think so....what line are you refering to?
i realized that my value from the convert days function is not returning to my main function...how can I make it return to my main function?
here is my current code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include "stdafx.h"
using namespace std;
int convertdays(int month, int day, int year)
{
int dat;
dat = 0;
dat = (year * 10000) + (month * 100) + day;
return (dat);
}
int _tmain(int argc, _TCHAR* argv[])
{
int month, day, year, date [9], x, dat;
date [0] = 1;
date [1] = 0;
date [2] = 0;
date [3] = 0;
date [4] = 0;
date [5] = 0;
date [6] = 0;
date [7] = 0;
date [8] = 0;
dat = 0;
for (x = 0; x < 9; x++)
{
cout << "Enter the month: " ;
cin >> month;
cout << "Enter the day: " ;
cin >> day;
cout << "Enter the year: " ;
cin >> year;
convertdays(month, day, year);
cout << dat << endl;
}
system("cls" );
cout << "\n" << date [0] << "\n\n" ;
cout << "\n" << date [1] << "\n\n" ;
cout << "\n" << date [2] << "\n\n" ;
cout << "\n" << date [3] << "\n\n" ;
cout << "\n" << date [4] << "\n\n" ;
cout << "\n" << date [5] << "\n\n" ;
cout << "\n" << date [6] << "\n\n" ;
cout << "\n" << date [7] << "\n\n" ;
cout << "\n" << date [8] << "\n\n" ;
system("pause" );
return 0;
}
Last edited on Nov 21, 2008 at 12:53am UTC
Nov 21, 2008 at 1:28am UTC
new code.....
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include "stdafx.h"
using namespace std;
int convertdays(int month, int day, int year)
{
int date;
date = 0;
date = ((year * 10000) + (month * 100) + day);
return date;
}
int _tmain(int argc, _TCHAR* argv[])
{
int month, day, year, date [9], x;
date [0] = 0;
date [1] = 0;
date [2] = 0;
date [3] = 0;
date [4] = 0;
date [5] = 0;
date [6] = 0;
date [7] = 0;
date [8] = 0;
for (x = 0; x < 9; x++)
{
cout << "Enter the month: " ;
cin >> month;
cout << "Enter the day: " ;
cin >> day;
cout << "Enter the year: " ;
cin >> year;
convertdays(month, day, year);
date[x] = date;
}
system("cls" );
cout << "\n" << date [0] << "\n\n" ;
cout << "\n" << date [1] << "\n\n" ;
cout << "\n" << date [2] << "\n\n" ;
cout << "\n" << date [3] << "\n\n" ;
cout << "\n" << date [4] << "\n\n" ;
cout << "\n" << date [5] << "\n\n" ;
cout << "\n" << date [6] << "\n\n" ;
cout << "\n" << date [7] << "\n\n" ;
cout << "\n" << date [8] << "\n\n" ;
system("pause" );
return 0;
}
Nov 21, 2008 at 1:35am UTC
date[x] = convertdays(month, day, year);
You need to read up on functions.
Topic archived. No new replies allowed.