Crash with error EXC_bad_access code=1 address=0x0 - c++

I've searched this website and many others for this seemingly common crash people experience and I've read that this error means that a line in my code is trying to access deallocated memory.
I'm a beginner in this language and am trying to play around with arrays in order to fully understand them and I can't seem to figure out what my error is.
I will try to make it as easy and clear as possible: My program simply asks for :
How many subjects and -subsequently- grades the user is going to input
The name of the subject followed by its grade for the number previously entered by the user (So if user entered 3 at the first question the program is going to ask three times to input the subject and grade)
My code is the following :
void moyenne()
{
string subjects[0];
double grade[0];
int lim;
cout << "**Program which calculates the average grade of several subjects, each with coefficient 1 **\n";
cout << "How many subjects are you going to enter grades for?\n";
cin >> lim; // User enters the "limit" to which the "for" loop is going to end
cout << "Please enter seperately the subjects of the grades you'd like to enter followed with their respectful grade.\n";
for (int i = 0; i<=lim-1; i++) // "lim-1" because if lim = x, i will go from 0 to x which is x+1 array slots
{
cout << "Enter subject.\n";
cin >> subjects[i];
cout << "Enter grade for " << subjects[i] << ".\n";
cin >> grade[i];
}
for (int j = 0; j<=lim-1; j++)
{
cout << subjects[j] << grade[j];
}
}
The program systematically and precisely crashes at the third request for a grade, whatever limthe user inputs as long as lim > 2. The error is :
And the console is :
**Program which calculates the average grade of several subjects, each with coefficient 1 **
How many subjects are you going to enter grades for?
3
Please enter seperately the subjects of the grades you'd like to enter followed with their respectful grade.
Enter subject.
Maths
Enter grade for Maths.
15
Enter subject.
Physics
Enter grade for Physics.
17
Enter subject.
English
(lldb)

Related

How do I make my program pass data to my array correctly? (Homework)

I've set my array size to 20 (I set it to 19 assuming it's counting 0). I set my for loop to only run so long as gradeCount <= to gradeCounted yet it will keep running no matter how many times I enter data. If I enter 3 grades without pressing enter between each one, such as "23 23 23" it will return "Enter Grade" 3 times in a row, rather, for as many grades as I enter, separated by spaces. I don't understand why it's not passing data into the array and ending the for loop properly. I'm sure my code is an ugly mess, sorry.
Also, when entering code into stackoverflow, it said to indent the code 4 spaces to format? I couldn't initially indent the code with the code button and there was no {} button either. What am I missing? It was only after a notification to fix it that I was able to. Thanks for your time, I don't want to be a pain in the ass for you guys.
//This program asks user how many grades there are, inputs grades, and displays median of said grades.
#include <iostream>
using namespace std;
//Variables
////////////////////const int limitGrades = 20; //Array "boxes"? //Ignore this //for now.
int gradeCounted; //Number of grades from user.
const int SIZE = 19;
//Array
float grades[19]; //Max grades that can be entered.
//Functions
void gradeTaker()
{
cout << "You may input up to 20 grades. \n";
cout << "First enter the number of grades you have: \n";
cin >> gradeCounted;
//requests how many grades there are and stores them in array
for (int gradeCount = 0; gradeCount <= gradeCounted + 1; gradeCount++)
{
for (float &grade : grades)
{
cout << "Enter grade: \n";
cin >> grade;
}
}
};
int main()
{
gradeTaker();
cout << "grades so far";
for (int grade : grades)
cout << grade << endl;
system("pause");
}
The size of the array is separate from how you access it. Accessing 20 values is the equivalent to accessing indices from 0 to 19.
float grades[20];
for(size_t i = 0; i < 20; i++){ // print all values of grades
std::cout << grades[i] << "\n";
}
Furthermore, your for loop in gradeTaker will ask you for a value for each index of grades a total of gradeCounted + 2 times. To fix this, only iterate over the indices that you're assigning a value to like so:
for (int gradeCount = 0; gradeCount < gradeCounted; gradeCount++){
cout << "Enter grade: \n";
cin >> grade[gradeCount];
}
Finally... the for loop in your main function will iterate across the entire array which may include uninitialized values. You should initialize the array or use a dynamic data structure like std::vector and just push_back the necessary values.
(P.s. highlight code in the text-block and press CTRL+K to indent.)

How do I get my function to return to a specific point in my program in an if statement?

I want my function to work so that if someone enters more grades than the 20 allowed it returns to another point, whether in the function or out of it, so that the user has the option to try again. What do I need to change, I know return can't be the answer as it just exists the function and goes to the next line after the function call. This is for homework, and I'm honestly not sure this is even necessary, I just want it to be clean and handle user errors.
void gradeTaker()
{
cout << "You may input up to 20 grades. \n";
cout << "First enter the number of grades you have: \n";
cin >> gradeCounted;
if (gradeCounted > arraySize)
{
cout << "You entered a number of grades greater than 20, try again \n";
return;
}
}
The current result of my code is it exits the function and continues to the following line. I don't know the code necessary for what I want.
you can use while loop to take the input with the condition:
void gradeTaker() {
cout << "You may input up to 20 grades. \n";
cout << "First enter the number of grades you have: \n";
cin >> gradeCounted;
while (gradeCounted > arraySize) {
cout << "You entered a number of grades greater than 20, try again \n";
cin >> gradeCounted;
}
// do whatever with gradeCount
}
Also do a while loop, label with goto statement, and some other logic to achieve.
Call the function 'gradeTaker()' from inside itself instead of 'return'

C++: How do I prompt a user for input using an int array and char array in the same for loop?

I need my program to prompt the user for Id# and answers (a,b,c,d etc) for 15 students. However it is possible that less than 15 students wrote the quiz. I have to use 2 seperate arrays to store Id #'s and the answers for each student. I have written the following code however it doesn't function properly. If I enter more than 1 letter it exits the loop but if I only enter one letter it works properly. Help would be much appreciated!
int id[15];
char responses[150];
for (int c = 0; c < 15; c++)
{
cout << endl << "Student id: ";
cin >> id[c];
if (id[c] < 0) break;
cout << "Student's responses with no spaces in lower case: ";
cin >> responses[c];
}

Compare two arrays C++

I'm trying to create a function that uses dynamic allocated arrays instead of vectors because I want to see how they work. Basically, this function asks the user to input how many people they are going to enter followed by their name; then, they enter how many of these people want to register for an ID followed by their phone #.
For example:
"How many customers will you like to enter? " 3 //user inputs 3
Bob Allen //user input
Ellen Michaels //user input
Jane Andrews //user input
"How many people want to register for the VIP ID? " 4 //user inputs 4; user can enter a new name here if they forgot to enter it the first time
Jane Andrews 213-2312
Bob Allen 111-1212
Jonathan Drew 211-9283
Michael Jacks 912-3911
//what the program spits out after the function is implemented
Final Registration: Guaranteed VIP IDs
Bob Allen 111-1212
Ellen Michaels NOT GUARANTEED
Jane Andrews 213-2312
//The new users entered in the 2nd round will not be listed in the final registration because it is too late for them to sign up
Basically, the problem I'm having is:
1. How to check if the person is not listed in the second round
2. Ignore the new people the user entered in the second round
3. Print the final registration in the same order the user entered the names in the first round
This is what I have right now, but this only works if the same number of users is entered the first and second time around. It does not check if there are new or omitted
string* people;
cout << "How many customers will you like to enter? " << endl;
int howmany;
cin >> howmany;
people = new int[howmany];
for (int i=0;i<howmany;i++)
{
cin >> people[i];
}
cout << "How many people want to register for the VIP ID? " << endl;
int num_of_register;
string* vip;
cin >> num_of_register;
vip = new int[num_of_register];
for (int i=0;i<num_of_register)
{
cin >> vip[i];
for (int j=0;j<howmany;j++)
{
if (num_of_register == howmany) {
if people[j] == vip[i] //check if they're the same people or not
cout << "Final Registration: Guaranteed VIP Ids" << endl;
cout << people[i];
else {
//check if the names are the same
}
}
}
Any guide/advice will be helpful. I'm not sure how to approach this problem. Thank you!
First you get howmany and allocate memory for people. In the second round, there is a chance that user enters a new person. You can first get number of customers and then their names and in the second part, check if the name exist in the first part
cin >> num_of_register;
if( num_of_register > howmany )
{
cout << "You can't register new people\n";
}
for (int i=0;i<num_of_register; i++)
{
cin >> vip[i];
int customerExists = -1;
for (int j=0;j<howmany;j++)
{
if( people[j] == vip[i] )
{
customerExists = i;
break;
}
}
if(customerExists == -1)
{
cout << "You can not enter new customer\n";
// get people[i] again
i--;
}
else
{
cout << "Final Registration: Guaranteed VIP Ids" << endl;
cout << people[customerExists];
}
}

How to get two inputs from a same input (C++)

Title probably sounds confusing so first I'll show you my code, I made this simple program to get two input values and multiply them, and another thing, but that's not important, It works correctly:
#include <iostream>
using namespace std;
main()
{
int a,b,c,d,e;
char j = 4;
cout << "Welcome to Momentum Calculator\n\n";
cout << "------------------------------\n";
cout << "Please Enter Mass in KG (if the mass in in grams, put \"9999\" and hit enter): \n\n";
cin >> a;
if (a==9999) {
cout << "\nPlease Enter Mass in grams: \n\n";
cin >> d;
}
else {
d = 0;
}
cout << "\nPlease Enter Velocity \n\n";
cin >> e;
if (d == 0)
{
c = (a*e);
}
else {
c = (e*d)/100;
}
cout << "\nMomentum = " << c;
cin.get();
cin.ignore();
while (j == 4)
{
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
main();
}
}
Now as you can see, my variable is an int (integer) and my problem is If I enter an English letter (a-z) or anything that is not a number will cause it to repeat my program unlimited times at an unlimited speed. I want a string/char to see if my var "a" is a letter or anything but don't know how to. I can do it, however, I want user to input only one time in "a" and mine makes him to enter again. Please Help :)
There is a function called isalpha in ctype library, checks whether your variable is an alphabetic letter so you can do using isalpha function.
Will isdigit or isalpha from standard library help you?
P.S.
1KG contains 1000 grams, so you should divide by 1000, not by 100;
UPDATE:
Seems I understood your question...
You need cin.clear(); before cin.get() and cin.ignore().
Otherwise the these calls won't do anything, as cin is in an error state.
I think you can get a as an String, and see if it contains English letter or not, if it contains, again ask for the input ( you can do it in a while loop ). And when a correct input entered, parse it and find what is it's number.