synlight's Profile
Reputation: 44
Craftsman
- Group:
- Active Members
- Active Posts:
- 259 (0.42 per day)
- Joined:
- 14-September 11
- Profile Views:
- 1,824
- Last Active:
Apr 05 2013 02:58 PM- Currently:
- Offline
Previous Fields
- Country:
- US
- OS Preference:
- Who Cares
- Favorite Browser:
- FireFox
- Favorite Processor:
- AMD
- Favorite Gaming Platform:
- Playstation
- Your Car:
- Who Cares
- Dream Kudos:
- 0
Latest Visitors
-
vividexstance 
09 Apr 2013 - 09:27 -
GunnerInc 
04 Apr 2013 - 18:15 -
jon.kiparsky 
04 Apr 2013 - 12:07 -
CTphpnwb 
03 Apr 2013 - 08:25 -
jjl 
10 Mar 2013 - 20:46
Posts I've Made
-
In Topic: Nested Structs and Pointers
Posted 5 Apr 2013
Thanks again, Jim. My project is up and running!!!
As for Warnings.. okay.. I don't want to be responsible for your head exploding but..
Professor told us the first day of class to ignore them. I blew him off until I realized he was making us use so many deprecated/unsafe functions. I was throwing Warnings all over the place in all my programs when I followed his rules. I stopped paying attention to them.
Now I realize they are a valuable tool. I thought something like an accidental 1 instead of an i to print an int would throw an error. I was wrong.
I'm taking a month off after I graduate, and I'm going to spend it trying to learn how to do things properly.
As of this very minute.. I have logged 80 (awake) hours in 6 days working on the program.
Thank you, Jim, and Baav, and Jon.K for your help. I couldn't have done it without you.
It was a trial by fire, for sure LOL. -
In Topic: Nested Structs and Pointers
Posted 5 Apr 2013
It's the list function that's causing the error.. I just don't understand why, since I called it the same way I called all the other elements of the customer array.. -
In Topic: Nested Structs and Pointers
Posted 5 Apr 2013
synlight, on 05 April 2013 - 08:47 AM, said:
jimblumberg, on 05 April 2013 - 08:46 AM, said:Please post the smallest possible complete program that illustrates your problem. Basically I need to see a small main that calls these functions and how you are trying to print the values that is causing the problem.
Jim
Okay.. give me 5 minutes. Thank you, Jim.
I think I got it as small as possible.. EDIT!! I had to add the list function that is causing the violation! Sorry!!!!!
# include <cstdio> # include <cstring> # include <cstdlib> # include <windows.h> # include <cctype> using namespace::std; struct dueDate { int month; int day; int year; //constructor dueDate(): month(0), day(0), year(0) {} }; struct record { int custID; char name[28]; char state[3]; char discCode; double balanceDue; dueDate date; }; //PROTOTYPES void getString(const char *prompt, char *buffer, int); void getDate(struct record *customer, int); bool validDate(char *testDate, struct record *customer, int); void list(struct record *customer); int main() { record customer[50]; int pos = 0; getDate(customer, pos); //this causes the Access Reading Vio printf("%i%i%i", customer[pos].date.month, customer[pos].date.day, customer[pos].date.year); list(customer); }//END MAIN /*********************************************** * Gets date as string* ************************************************/ void getDate(record customer[], int pos) { const int length = 40; char testDate[length]; bool valid = false; while(!valid) { getString("Please enter the date as XX/XX/XXXX:", testDate, length); valid = validDate(testDate, customer, pos); } }//END GETDATE /*********************************************** * Check valid date* ************************************************/ bool validDate(char testDate[], record customer[], int pos) { int month = 0; int day = 0; int year = 0; char copy[12]; char tMonth[3]; char tDay[3]; char tYear[5]; bool valid = false; //not all variables are used in this test program - they are used in the actual program char* token; //GET MONTH token = strtok(testDate,"/"); strcpy(tMonth, token); month = atoi(tMonth); //GET DAY token = strtok(NULL, "/"); strcpy(tDay, token); day = atoi(tDay); //GET YEAR token = strtok(NULL, "/"); strcpy(tYear, token); year = atoi(tYear); int numDays = 0; bool monthFlag = false; bool dayFlag = false; bool yearFlag = false; customer[pos].date.month = month; customer[pos].date.day = day; customer[pos].date.year = year; printf("%i%i%i", customer[pos].date.month, customer[pos].date.day, customer[pos].date.year); //I removed all the testing.. the function will return true for testing purposes. return true; } /********************************* * Gets and returns a String * *********************************/ void getString(const char prompt[], char buffer[], int max) { printf("\n%s",prompt); fgets(buffer,max,stdin); char* p = strchr( buffer, '\n'); if( p != NULL ) *p = 0; else while(fgetc(stdin) != '\n'); }//END GETSTRING void list(record customer[]) { printf("Due Date\n"); printf("%18s%i\%i\%1\n", customer[0].date.month, customer[0].date.day, customer[0].date.year); printf("\n\n"); } -
In Topic: Nested Structs and Pointers
Posted 5 Apr 2013
jimblumberg, on 05 April 2013 - 08:46 AM, said:Please post the smallest possible complete program that illustrates your problem. Basically I need to see a small main that calls these functions and how you are trying to print the values that is causing the problem.
Jim
Okay.. give me 5 minutes. Thank you, Jim. -
In Topic: Nested Structs and Pointers
Posted 5 Apr 2013
Thank you, Jim. I read the article to linked.
My project is fully tested and running with the exception of the dueDate (struct inside struct). When I print my struct array, I get an Access Reading Violation error on the dueDate (all other array elements print fine)..
So. I populate the date by passing the record array it to a getDate function.
I then pass it to a validDate function, which populates it. I am able to print the correct date inside the validDate function, with no Access Violations.
I'm going to try to post relevant code..
Declared above main:
struct dueDate { int month; int day; int year; //constructor dueDate(): month(0), day(0), year(0) {} }; struct record { int custID; char name[28]; char state[3]; char discCode; double balanceDue; dueDate date; };
call to getDate:
getDate(customer, pos);
getDate function
void getDate(record customer[], int pos) { const int length = 40; char testDate[length]; bool valid = false; while(!valid) { getString("Please enter the date as XX/XX/XXXX:", testDate, length); valid = validDate(testDate, customer, pos); } }//END GETDATE
validDate function(abbreviated to remove testing since that is working well and is very long)
bool validDate(char testDate[], record customer[], int pos) { int month = 0; int day = 0; int year = 0; char copy[12]; char tMonth[3]; char tDay[3]; char tYear[5]; bool valid = false; char* token; //GET MONTH token = strtok(testDate,"/"); strcpy(tMonth, token); month = atoi(tMonth); //GET DAY token = strtok(NULL, "/"); strcpy(tDay, token); day = atoi(tDay); //GET YEAR token = strtok(NULL, "/"); strcpy(tYear, token); year = atoi(tYear); int numDays = 0; bool monthFlag = false; bool dayFlag = false; bool yearFlag = false; if(monthFlag == true && dayFlag == true && yearFlag == true) valid== true; customer[pos].date.month = month; customer[pos].date.day = day; customer[pos].date.year = year; //THIS CODE PRINTS THE PROPER INTEGERS, and prints mem addresses when I use & customer[pos].date.month, etc //BUT when I use the same exact line of code in main, I get a Access Reading Violation runtime error. printf("%i%i%i", customer[pos].date.month, customer[pos].date.day, customer[pos].date.year); return valid; }
Is this happening because I am passing the array to this function from another function?
My Information
- Member Title:
- D.I.C Regular
- Age:
- Age Unknown
- Birthday:
- Birthday Unknown
- Gender:
-
- Location:
- Charleston, SC
- Years Programming:
- 0
Contact Information
- E-mail:
- Click here to e-mail me
|
|


Find Topics
Find Posts
View Reputation Given
|
Comments
brianborn1968
10 Jul 2012 - 06:19BRampersad
13 Dec 2011 - 04:07Gorian
12 Dec 2011 - 10:14