Access Violation when running compiled program.

Hello,

(Being fairly new at C++) I'm having an issue with a program I'm writing. It has successfully been compiled and ran fine yesterday.
However when I opened it today it crashed.

I debugged it and I'm getting popup window saying "An Access Violation (Segmentation Fault) raised in your program."

I've been double checking everything for a while and I can't find anything I did wrong.

I haven't written all of the code yet, I'm dividing it into function, but leaving the cases empty shouldn't give me any issues.

So far the error only occurs for case MANUALLY and case FROM_FILE in the second switch menu. Case DISPLAY_CHART works just fine.


So here is my code:

Header file:
1
2
3
4
5
6
7
8
9
10
11
#ifndef HEADER_H
#define HEADER_H

using namespace std;

double* rowPrices (double*);
double* readRowPrices (double*);
void pause();
void displayChart(char[][15]);

#endif 



Main:
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <header.h>

using namespace std;


int main()
{
 	
 	const int STORE_PRICE = 1,
 		  DISPLAY_CHART = 2,
 		  SELL_SEAT = 3,
 		  VIEW_TOTALS = 4,
 		  CHECK_SEATS = 5,
  		  MANUALLY = 1,
		  FROM_FILE = 2,
		  MAX_ROWS = 15,
		  MAX_SEATS = 15;
 		  	  
    int menuChoice,
	menu2Choice;
		
	char seat[MAX_ROWS][MAX_SEATS];
		
	double *priceRow;
	
	cout << fixed << showpoint << setprecision(2);
 		  	  

    cout << "\n\tAUDITORIUM MENU"
    	 << "\n1. Register seat prices by row."
    	 << "\n2. Display chart of auditorium."
    	 << "\n3. Sell seats."
    	 << "\n4. Show total seats sold and total earnings."
    	 << "\n5. Show available seats in certain row and in\n"
    	 << "   the auditorium."
	 << "\n\nYour choice: ";
    cin >> menuChoice;
	cout << "\n\n";
    
		switch (menuChoice)
		{
			case STORE_PRICE:
				cout << "1. Enter price manually."
					 << "\n2. Enter from seatPrices.txt."
					 << "\n\nYour choice: ";
				cin >> menu2Choice;
				
					switch (menu2Choice)
					{
						case MANUALLY:
							priceRow = rowPrices (priceRow);
							break;
						case FROM_FILE:
							priceRow = readRowPrices(priceRow);
							break;
							
						default:
							cout << "\n\nYou have to chose between 1 and 4";
							break;
					}
					 
				break;
				
			case DISPLAY_CHART:
					for (int count = 0; count < MAX_ROWS; count ++)
					{
						for (int index = 0; index < MAX_SEATS; index ++)
						{
							seat[count][index] = '#';
						}
					}
				
				displayChart (seat);
				
				break;

			case SELL_SEAT:
		 		break;
				
			case VIEW_TOTALS:
		 		break;
				
			case CHECK_SEATS:
				break;
				
			default:
			    break;
		}
	
		//for (int count = 0; count < MAX_ROWS; count ++)
		//	cout << "\n" << priceRow[count];
			
	
	pause();
	
	return(0);
}



rowPrices():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <header.h>

using namespace std;

double* rowPrices (double price[])
{
 	const int MAX_ROWS = 15;
 	int row;
		 
        cout << endl;
			
			for (int count = 0; count < MAX_ROWS; count ++)
			{
			 	row = count + 1;
			 	cout << "Enter price for row " << row << " : ";
			 	cin >> price[count];
			}
			
			return (price);
}



readRowPrice():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <header.h>
#include <fstream>

using namespace std;

double* readRowPrices( double price[])
{
 		ifstream rowPrices;
 		rowPrices.open ("Prices.txt");
 		double num = 0;
 		
   		     for (int count = 0; count < 15; count ++)
   		     {
			  	 rowPrices >> num;
			  	 price[count] = num;
			 }
			 
		 rowPrices.close();
		 
		 return(price);
}



displayChart():
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
#include <iostream>
#include <header.h>

using namespace std;

void displayChart (char seat[][15])
{
    const int MAX_SEATS = 15,
     	   	  MAX_ROWS = 15;
     	   	  
    cout << "\n"
    	 << "Seat:\t\t1   2   3   4   5"
         <<       "   6   7   8   9   10"
	 <<	   "  11  12  13  14  15\n\n";             
     	   	   
        for (int count = 0; count < MAX_ROWS; count ++)
		{
			cout << "Row: " << count + 1 << "\t\t";
			
				for (int index = 0; index < MAX_SEATS; index ++)
				{
					cout << seat[count][index] << "   ";
				}
				
			cout << "\n";
		}
		
	return;
}



void pause():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <header.h>
#include <conio.h>

using namespace std;


void pause ()
{
 	  printf("\n%s", "Press any key to continue");
	  _getch();
	  printf("\n\n");
	  return;
}



Thank you in advance for all advice and recommendations.


Last edited on
Here is a screenshot, if somehow it can help: http://imgur.com/a/aLpuD
The variable priceRow is an uninitialized pointer. And it remains uninitialized throughout the whole program as far as i can see it. Accessing this uninitialized pointer will lead to undefined behavior. Usually a crash.

Because you always use the constants MAX_ROWS and MAX_SEATS the arrays must have exact this amount of valid data. Everything else is an error. I don't think that you want this behavior?
Standard recommendation: Step through your code in a debugger. That will show you exactly where your code is crashing, and allow you to examine the state of the memory at that point to see why it's crashing.
@ coder777:

Thank you, I have tried, and I'm still trying to work with priceRow. I defined it as double *priceRow because i want this blank array to go in function rowPrices() where it will be filled by the user and then I want to return it to main so that it can be read from there. When I define double priceRow[MAX_ROWS] then i have a conversion problem with my function priceRow. I want to return an array[15] to main but i can't put double[15] in front of rowPrices():
double[15] rowPrices() {}.
I am somewhat new to arrays and I'm quite confused as to how to make this one work quite frankly.

@Mikey Boy

Thank you, I've indeed been doing that however my debugging 'skills' are somewhat limited so far. I do think that what has been pointed out above must be what's crashing my program.
Well I solved it, actually what wasn't working was priceRow = rowPrices(priceRow).
I figured you can't return the array priceRow if it is also the argument of the function rowPrices() in: priceRow = rowPrices(priceRow).

So I created double price[MAX_ROWS] and used it like so:
priceRow = rowPrices(price); and now it works.

Thank you a lot for your time anyway, sorry for bothering.

Have a great day.
I want to return it to main
There is no reason for rowPrices(...) and readRowPrices (...) to return the pointer passed in. The array passed in will be modified. The return value does not have any relevance. Thus priceRow should be an array not a pointer.
Thank you coder 777!
Topic archived. No new replies allowed.