Program crashing even in Debug

I thought I had it, but it keeps crashing, even in debug, so I can't figure out whats wrong. Please help.

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <math.h>
#include "arrayUtils.h"


using namespace std;


// Compute the number of calories in one serving of a recipe
void computeCalories (const char* nutrientFileName, const char* recipeFileName);


int main (int argc, char** argv)
{
    if (argc != 3)
    {
        cerr << "Usage: " << argv[0] << " nutrientFile recipeFile" << endl;
        return -1;
    }

    computeCalories (argv[1], argv[2]);

    return 0;
}




// Compute the number of calories in one serving of a recipe
void computeCalories (const char* nutrientFileName, const char* recipeFileName)
{
    //count #lines in nutrients file
    string nline;
    int nLines = 0;
    ifstream nfile("nutrients0.txt");

    if(nfile.is_open()){
        while(!nfile.eof()){
            getline(nfile,nline);
            nLines++;
        }
        nfile.close();
    }

    //*initialize food data;
    string* nName;
    float* nAmount = new float[nLines];
    string* nUnit;
    float* nCalories = new float[nLines];
    string rName;
    float* rAmount = new float[nLines];
    string* rUnit;
    string* ingredients;

    string name;
    float amount = 0.0;
    string unit;
    float calories = 0.0;
    float servings = 0.0;
    string ingredient;

    //count #lines in recipe file
    string rline;
    int rLines = 0;
    ifstream rfile("recipe0.txt");

    if(rfile.is_open()){
        while(!rfile.eof()){
            getline(rfile,rline);
            rLines++;
        }
        rfile.close();
    }
    //** read nutrients;
    ifstream nIn(nutrientFileName);
    for(int i = 0; i < nLines; i++)
    {
        getline(nIn, name, ';');
        nIn >> amount >> unit >> calories;
        nName[i] = name;
        nAmount[i] = amount;
        nUnit[i] = unit;
        nCalories[i] = calories;
    }

    amount = 0.0;
    //**read recipe;
    ifstream rIn(recipeFileName);
    rIn >> rName;
    rIn >> servings;
    for(int r = 0; r < rLines; r++)
    {
        rIn >> amount >> unit >> ingredient;
        rAmount[r] = amount;
        rUnit[r] = unit;
        ingredients[r] = ingredient;
    }
    //*calorie counter;
		//****totalCalories = 0;
		float totalCalories = 0.0;
		int p = 0;
		int t = 0;
		int allUsed = 1;
		for(int n = 0; n < rLines; n++)
        {
            //search(nutrientName[], endN, ingredientName);
            p = seqSearch(nName, nLines, ingredients[n]);
            if(p >= 0)
            {
                t = seqSearch(nUnit, nLines, rUnit[n]);
                if(t >= 0)
                {
                    totalCalories = nCalories[n];
                }
                else
                    allUsed = 0;
            }
            else
                allUsed = 0;
        }

        //converts totalCalories from float to int
        int total = 0;
        total = (int)floor(totalCalories + .5);

		if(allUsed == 1)
                cout << "One serving of " << rName << " contains " << total << " calories." << endl;
        else
            cout << "One serving of " << rName << " contains at least " << total << " calories." << endl;
}


Here is arrayUtils.h

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#ifndef ARRAYUTILS_H
#define ARRAYUTILS_H



//  Add to the end
//  - Assumes that we have a separate integer (size) indicating how
//     many elements are in the array
//  - and that the "true" size of the array is at least one larger 
//      than the current value of that counter
template <typename T>
void addToEnd (T* array, int& size, T value)
{
   array[size] = value;
   ++size;
}



// Add value into array[index], shifting all elements already in positions
//    index..size-1 up one, to make room.
//  - Assumes that we have a separate integer (size) indicating how
//     many elements are in the array
//  - and that the "true" size of the array is at least one larger 
//      than the current value of that counter

template <typename T>
void addElement (T* array, int& size, int index, T value)
{
  // Make room for the insertion
  int toBeMoved = size - 1;
  while (toBeMoved >= index) {
    array[toBeMoved+1] = array[toBeMoved];
    --toBeMoved;
  }
  // Insert the new value
  array[index] = value;
  ++size;
}


// Assume the elements of the array are already in order
// Find the position where value could be added to keep
//    everything in order, and insert it there.
// Return the position where it was inserted
//  - Assumes that we have a separate integer (size) indicating how
//     many elements are in the array
//  - and that the "true" size of the array is at least one larger 
//      than the current value of that counter

template <typename T>
int addInOrder (T* array, int& size, T value)
{
  // Make room for the insertion
  int toBeMoved = size - 1;
  while (toBeMoved >= 0 && value < array[toBeMoved]) {
    array[toBeMoved+1] = array[toBeMoved];
    --toBeMoved;
  }
  // Insert the new value
  array[toBeMoved+1] = value;
  ++size;
  return toBeMoved+1;
}


// Search an array for a given value, returning the index where 
//    found or -1 if not found.
template <typename T>
int seqSearch(const T list[], int listLength, T searchItem)
{
    int loc;

    for (loc = 0; loc < listLength; loc++)
        if (list[loc] == searchItem)
            return loc;

    return -1;
}


// Search an ordered array for a given value, returning the index where 
//    found or -1 if not found.
template <typename T>
int seqOrderedSearch(const T list[], int listLength, T searchItem)
{
    int loc = 0;

    while (loc < listLength && list[loc] < searchItem)
      {
       ++loc;
      }
    if (loc < listLength && list[loc] == searchItem)
       return loc;
    else
       return -1;
}


// Removes an element from the indicated position in the array, moving
// all elements in higher positions down one to fill in the gap.
template <typename T>
void removeElement (T* array, int& size, int index)
{
  int toBeMoved = index + 1;
  while (toBeMoved < size) {
    array[toBeMoved] = array[toBeMoved+1];
    ++toBeMoved;
  }
  --size;
}



// Search an ordered array for a given value, returning the index where 
//    found or -1 if not found.
template <typename T>
int binarySearch(const T list[], int listLength, T searchItem)
{
    int first = 0;
    int last = listLength - 1;
    int mid;

    bool found = false;

    while (first <= last && !found)
    {
        mid = (first + last) / 2;

        if (list[mid] == searchItem)
            found = true;
        else 
            if (searchItem < list[mid])
                last = mid - 1;
            else
                first = mid + 1;
    }

    if (found) 
        return mid;
    else
        return -1;
}





#endif 


And here are the files that are read.

nutrients0.txt:

graham crackers; 2 squares 59
milk chocolate; 1 bar 235
cheese, swiss; 1 oz 108
marshmallows; 1 cup 159

recipe0.txt:

S'mores
2
4 squares graham crackers
1 bar milk chocolate
2 large marshmallows

First off, you have memory leaks.

Anything newed should also be deleted.

Have you run through the program, line-by-line? Where does it crash?
Last edited on
Taking a really quick look through, I noticed that you declared a bunch of pointers on lines 49-56 and didn't allocate any memory before using them (nName, nUnit, ...).
Ok, I see where I forgot to allocate the arrays now. It starts crashing at the nutrientFile read, but its prob the arrays. Let me make some changes. Thanks.
Yup, that was it. Der on my part. I'll make sure to delete my makings too. Thanks.
Topic archived. No new replies allowed.