I have created an Array that is sized by user input.
int spotInArray = 0;
Bankcard* a = NULL;
int n;
cout << "Enter number of cards" << std::endl;
cin >> n;
a = new Bankcard[n];
This is my code I have in my switch for the user to select which card they want to delete.
int choice;
cout << "Enter Number of card you would like to delete: " << endl;
cin >> choice;
for (int i = n; i < spotInArray; i++)
{
a[n] = a[n + 1];
a[choice - 1] = 0;
}
I am getting this error on a[choice - 1] = 0;
IntelliSense: no operator "=" matches these operands
Here is the full code.
int main()
{
//Bankcard bankcard1("Blue Card", 1, .05, 3000.00, 430.32, 200.35, 124.00);
int spotInArray = 0;
Bankcard* a = NULL; // Pointer to int, initialize to nothing.
int n; // Size needed for array
cout << "Enter number of cards" << std::endl;
cin >> n; // Read in the size
a = new Bankcard[n]; // Allocate n ints and save ptr in a.
//for (int i=0; i<n; i++) {
//a[i] = bankcard1;
//bankcard1.show();
//}
int choice;
showMenu();
cin >> choice;
while (choice != 6)
{
switch(choice)
{
case 1 : {
string productName;
int cardNum;
double interestRate;
double maxLimit;
double outstandingBalance;
double purchaseAmount;
double paymentAmount;
cout << "Enter Card Name(No spaces, no special characters)" << std::endl;
cin >> productName;
cout << "Enter Number of Card" << std::endl;
cin >> cardNum;
cout << "Enter interest Rate" << std::endl;
cin >> interestRate;
cout << "Enter Max Limit" << std::endl;
cin >> maxLimit;
cout << "Enter Outstanding Balance" << std::endl;
cin >> outstandingBalance;
cout << "Enter Purchase Amount" << std::endl;
cin >> purchaseAmount;
cout << "Enter Payment Amount" << std::endl;
cin >> paymentAmount;
Bankcard bankcard1(productName, cardNum, interestRate, maxLimit, outstandingBalance, purchaseAmount, paymentAmount);
a[spotInArray] = bankcard1;
spotInArray++;
break;
}
case 2 : update();
break;
case 3 : {
int choice;
cout << "Enter Number of card you would like to delete: " << endl;
cin >> choice;
for (int i = 0; i < n; i++)
{
a[n] = a[n + 1];
a[choice - 1] = 0;
}
}
deleteCard();
break;
case 4 : overLoad();
break;
case 5 : {
for ( int i = 0; i < spotInArray; i++)
{
cout << a[i];
}
}
break;
case 6 : exit();
break;
}
showMenu();
cout << endl;
cin >> choice;
};
std::cin.get();
std::cin.get();
return 0;
}
Unless there are other code that you haven't shown, currently this for loop shouldn't even run:
for (int i = n; i < spotInArray; i++)
{
a[n] = a[n + 1];
a[choice - 1] = 0;
}
because spotInArray is 0 and i starts at n (and you are incrementing i, too).
Are you sure it's failing on that line?
There are few incorrect things with the code. First of all, a[n] is invalid. Valid index of array of size n are 0 to n-1. Avoid handling memory your self. Use std::list instead in this case ( if there is frequent removals ).
std::list<Bankcard> bCards(n);
// Take input to for the cardToRemove.
bCards.erase( bCards.begin() + cardToRemove );
Related
I have this program that is barely started:
#include <iostream>
#include <iomanip>
#include <ctime>
#include <string>
using namespace std;
class Grade
{
public:
string studentID;
int userChoice = 0;
int size = 0;
double* grades = new double[size]{0};
};
void ProgramGreeting(Grade &student);
void ProgramMenu(Grade& student);
string GetID(Grade& student);
int GetChoice(Grade& student);
void GetScores(Grade& student);
int main()
{
Grade student;
ProgramGreeting(student);
ProgramMenu(student);
}
// Specification C1 - Program Greeting function
void ProgramGreeting(Grade &student)
{
cout << "--------------------------------------------" << endl;
cout << "Welcome to the GPA Analyzer! " << endl;
cout << "By: Kate Rainey " << endl;
cout << "Assignment Due Date: September 25th, 2022 " << endl;
cout << "--------------------------------------------" << endl;
GetID(student);
cout << "For Student ID # " << student.studentID << endl;
}
void ProgramMenu(Grade &student)
{
cout << "--------------------------------------------" << endl;
cout << setw(25) << "Main Menu" << endl;
cout << "1. Add Grade " << endl;
cout << "2. Display All Grades " << endl;
cout << "3. Process All Grades " << endl;
cout << "4. Quit Program." << endl;
cout << "--------------------------------------------" << endl;
GetChoice(student);
}
string GetID(Grade &student)
{
cout << "Enter the student's ID: ";
cin >> student.studentID;
if (student.studentID.length() != 8) {
cout << "Student ID's contain 8 characters ";
GetID(student);
}
return student.studentID;
}
int GetChoice(Grade &student)
{
cout << "Enter your selection: ";
cin >> student.userChoice;
if (student.userChoice == 1) {
GetScores(student);
}
else if (student.userChoice == 2)
{
}
else if (student.userChoice == 2)
{
}
else if (student.userChoice == 4)
{
exit(0);
}
else
{
cout << "Please enter 1, 2, 3 or 4" << endl;
GetChoice(student);
}
}
void GetScores(Grade &student)
{
int count = 0;
double score = 0;
cout << "How many test scores would you like to enter for ID# "
<< student.studentID << "? ";
cin >> student.size;
while (count != student.size) {
cout << "Enter a grade: ";
cin >> score;
for (int i = 0; i < student.size; i++) {
student.grades[i] = score;
}
count++;
}
for (int i = 0; i < student.size; i++) {
cout << student.grades[i] << " ";
}
}
I am trying to make sure my array is recording all test scores, but when I output the array in my GetScore function, each element in the array is the same or equal to the last score I entered. For example, if I choose 3 for size and then enter three values of 99.2 86.4 90.1, all three elements will read 90.1.
Why is this happening?
Your Grade class is allocating an array of 0 double elements (which is undefined behavior), and then your GetScores() function does not reallocate that array after asking the user how many scores they will enter, so you are writing the input values to invalid memory (which is also undefined behavior).
Even if you were managing the array's memory correctly (ie, by using std::vector instead of new[]), GetScores() is also running a for loop that writes the user's latest input value into every element of the array, instead of just writing it to the next available element. That is why your final output displays only the last value entered in every element. You need to get rid of that for loop completely.
Try something more like this instead (there are several other problems with your code, but I'll leave those as separate exercises for you to figure out):
class Grade
{
public:
...
int size = 0;
double* grades = nullptr;
};
...
void GetScores(Grade &student)
{
int size = 0;
double score = 0;
cout << "How many test scores would you like to enter for ID# " << student.studentID << "? ";
cin >> size;
if (student.size != size) {
delete[] student.grades;
student.grades = new double[size]{0};
student.size = size;
}
for(int i = 0; i < size; ++i) {
cout << "Enter a grade: ";
cin >> score;
student.grades[i] = score;
}
for (int i = 0; i < size; ++i) {
cout << student.grades[i] << " ";
}
}
Alternatively:
#include <vector>
...
class Grade
{
public:
...
vector<double> grades;
};
...
void GetScores(Grade &student)
{
size_t size = 0;
double score = 0;
cout << "How many test scores would you like to enter for ID# " << student.studentID << "? ";
cin >> size;
student.grades.resize(size);
for (size_t i = 0; i < size; ++i) {
cout << "Enter a grade: ";
cin >> score;
student.grades[i] = score;
}
/* alternatively:
student.grades.clear();
for (size_t i = 0; i < size; ++i) {
cout << "Enter a grade: ";
cin >> score;
student.grades.push_back(score);
}
*/
for (size_t i = 0; i < size; ++i) {
cout << student.grades[i] << " ";
}
}
I removed my for loop from my while loop.
void GetScores(Grade &student)
{
int count = 0;
double score = 0;
cout << "How many test scores would you like to enter for ID# "
<< student.studentID << "? ";
cin >> student.size;
while (count != student.size) {
cout << "Enter a grade: ";
cin >> score;
student.grades[count] = score;
count++;
}
for (int j = 0; j < student.size; j++) {
cout << student.grades[j] << " ";
}
}
I have 5 programs that run perfectly fine individually, but when I combine them I get error messages and won't build. I have a menu to pick which program to run using if statements. Also a do while loop to repeat the programs. I believe it has something to do with the functions because I haven't had this problem before with simple programs. The program should first ask which program you want to run from the menu. It will run that program, then ask if you want to repeat.
I don't know what to try other than what is now in the program. I did take the do while loop out but still had the issue with the if statements.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream>
#include <stdlib.h>
using namespace std;
int main()
{
int choice;
char repeat;
if (repeat == 'm' || 'M'){
cout << "1. Perfect Scores\n"
"2. Larger Than n\n"
"3. Roman Numeral Converter\n"
"4. Monkey Business\n"
"5. Lottery\n"
"6. Exit\n";
cout << "Pick which program you would like to run." << endl;
cin >> choice;
}
else if (choice == 1){ // -----------Perfect Scores------------
do{
int countPerfect(int a[])
{
int i=0;
for(int l=0; l<10; l++)
if(a[1]==100)
i++;
return i;
}
{
int score[10];
for(int i=0; i<10; i++)
{
cout << "Enter score " << i+1 << endl;
cin >> score[i];
while(score[i]<0 || score[i] > 100)
{
cout << "Enter score between 1 and 100." << endl;
cin >> score[i];
}
}
int n = countPerfect(score);
cout << "No of perfect scores: " << n << endl;
cout << "To repeat, press Y. For main menu, press M" << endl;
cin >> repeat;
return 0;
}
}while(repeat == 'Y' || repeat == 'y');
}
// ------------Larger Than n--------------
else if (choice == 2){
do{
void display_greator(int A[], int size, int n)
{
int i;
for(i=0; i< size; i++)
{
if(A[i]>n)
{
cout << A[i] << endl;
}
}
}
int main(void)
{
int i, size;
cout << "Enter the size of your array:"<< endl;
cin >> size;
int N[size];
cout << "Enter a list of " << size << " numbers:" << endl;
for( i=0; i<size; i++)
{
cin >> N[i];
}
int num;
cout << "Enter your number n:" << endl;
cin >> num;
display_greator(N, size, num);
cout << "To repeat, press Y. For main menu, press M" << endl;
cin >> repeat;
}
}while(repeat == 'Y' || repeat == 'y');
}
// ----------------------Roman Numeral Converter------------------
else if (choice == 3){
char repeat;
do{
{
int n;
string romanNumbers[]={"I", "II", "III", "IV", "V", "VI", "VII",
"VIII", "IX", "X", "XI", "XII",
"XIII", "VIX", "XV", "XVI", "XVII", "XVIII", "XIX", "XX"};
cout << "Enter a decimal number or enter 0 to quit." << endl;
cin >> n;
if(n==0)
exit(0);
do
{
cout << "Enter number between 1 and 20" << endl;
cout << "Enter number or enter 0 to quit" << endl;
cin >> n;
} while(n < 0 || n > 20);
{
cout << "Enter decimal equivalent roman number:" << endl;
cout << "Enter a number between 1 and 20:" << endl;
cin >> n;
if(n==0)
exit(0);
cout << "To repeat, press Y. For main menu, press M" << endl;
cin >> repeat;
}
while(n > 0 || n < 20);
return 0;
}
}while(repeat == 'Y' || repeat == 'y');
}
// ---------------Monkey Business--------------------
else if(choice == 4){
do{
const int DAYS = 7;
double getTotalAmountOfFood(int[][DAYS],int);
double getLeastAmountOfFood(int[][DAYS],int, double);
double getGreatestAmountOfFood(int[][DAYS],int, double);
{
const int MONKEYS = 3;
double totalFood, averageFood, leastFood, greatestFood;
int foodInfo[MONKEYS][DAYS];
for(int i= 0; i< MONKEYS; i++)
{
cout << "Enter the food information of the monkey" <<
(i + 1) << ":" << endl;
for(int j = 0; j < DAYS; j++)
{
cout << "Day" << (j + 1) << ":" << endl;
cin >> foodInfo[i][j];
while(foodInfo[i][j] < 0)
{
cout << "Day " << (j+1) << ":" << endl;
cin >> foodInfo[i][j];
}
}
cout << endl;
}
totalFood = getTotalAmountOfFood(foodInfo, MONKEYS);
leastFood = getLeastAmountOfFood(foodInfo, MONKEYS, totalFood);
greatestFood = getGreatestAmountOfFood(foodInfo, MONKEYS, 0);
averageFood = totalFood / DAYS;
cout << "The average amount of food per day for three monkeys(in pounds):"
<< averageFood << endl;
cout << "The least amount of food per week for monkeys(in pounds) is:"
<< leastFood << endl;
cout << "The greatest amount of food per wek for a monkey is(in pounds):"
<< greatestFood << endl;
cout << "To repeat, press Y. For main menu, press M" << endl;
cin >> repeat;
return 0;
}
double getTotalAmountOfFood(int food[][DAYS],int mnks)
{
double total = 0;
for(int i = 0; i < mnks; i++)
{
for(int j = 0; j < DAYS; j++)
{
total += food[i][j];
}
}
return total;
}
double getLeastAmountOfFood(int food[][DAYS], int mnks, double leastAmount)
{
double least = leastAmount;
double weekTotal;
for(int i = 0; i < mnks; i++)
{
weekTotal = 0;
for(int j = 0; j < DAYS; j++)
{
weekTotal += food[i][j];
}
if(least > weekTotal)
least = weekTotal;
}
return least;
}
double getGreatestAmountOfFood(int food[][DAYS], int mnks, double greatestAmount)
{
double greatest = greatestAmount;
double weekTotal;
for(int i = 0; i < mnks; i++)
{
weekTotal = 0;
for(int j = 0; j < DAYS; j++)
{
weekTotal +=food[i][j];
}
if(greatest < weekTotal)
greatest = weekTotal;
}
return greatest;
}
}while(repeat == 'Y' || repeat == 'y');
}
// ----------------Lottery--------------------
else if (choice == 5){
char repeat;
do{
srand(time(NULL));
int winningDigits[5];
int player[5];
int num;
int matchCount = 0;
for (int i = 0; i < 5; i++)
{
winningDigits[i] = rand() % 10;
}
cout << "Enter 5 integers in the range of 0 to 9." << endl;
for(int i = 0; i < 5; i++)
{
cout << "Number #" << (i + 1) << ": " << endl;
cin >> num;
while (num < 0 || num > 9)
{
cout << "Invalid number! It should be in the range of 0 through 9." << endl;
cout << "Number #" << (i + 1) << ": " << endl;
cin >> num;
}
player[i] = num;
}
for (int i = 0; i < 5; i++)
{
if (winningDigits[i] == player[i])
{
matchCount++;
}
}
cout << "Winning digits: " << endl;
for (int i = 0; i < 5; i++)
{
cout << winningDigits[i] << " " << endl;
}
cout << "Player's digits: " << endl;
for (int i = 0; i < 5; i++)
{
cout << player[i] << " " << endl;
}
cout << endl << endl << "Number of digits matched: "
<< matchCount << endl;
cout << "To repeat, press Y. For main menu, press M" << endl;
cin >> repeat;
return 0;
}while(repeat == 'Y' || repeat == 'y');
}
else if (choice == 6)
{cout << "Bye" << endl;}
}
I'm expecting to be able to choose a program to run, repeat it, and repeat the entire program from main menu.
I am not going to point out all errors in your code, they are just too many. You went too fast too far. If you think the complexity of code and the errors it procudes are intimidating you are right. I wrote codes with more lines, but yours is too complicated for me. Go in small steps. Start with something along the line of:
int choose() { return 0; }
void func1() {}
void func2() {}
int main() {
int choice = 0;
while ( choice = choose() ) {
switch(choice) {
case 1 : func1(); break;
case 2 : func2(); break;
}
}
}
Your main function does not have to be more complex than that.
Write this, not more. Make sure it compiles, then in tiny steps fill the gaps, after each step compile, see if it does what you expect and only then continue to put more.
Some problems in your code (partly stolen from comments):
you cannot have more than one main
you cannot define functions inside functions
you cannot start an if statement with else if
int N[size]; is not standard C++ for a non-compile-time-constant size. Use std::vector instead
see here why using namespace std is considered bad practice
if (repeat == 'm' || 'M') is not doing what you expect, it should be if (repeat == 'm' || repeat == 'M'). In yours 'M' is taken as a bool which is always true (because it isnt 0).
make sure to initialize variables. Using variables that are not initialized causes undefined behaviour.
please next time reduce your code to a mcve and try to concentrate on a single problem, also include the error in the question
I cannot help myself than to point that for each single problem there are duplicate questions, which brings me back to: Don't do too many things at once. Fixing 100 errors at once is extremely difficult, fixing 1 error is doable.
last but not least, pay attention to compiler errors and warnings while you write the code (again: not after you wrote several pages, but after each single line)
This program is supposed to accept values from the keyboard, and require that the user re-enter the value for the employee's id number. However it keeps outputting "Invalid variable" even if I enter a correct value. It needs to only output that if the value is already been entered. For example
if I enter "3453" as the id number it will still output "Invalid Variable" even if I have not entered that number before.
#include <iostream>
using namespace std;
struct Employee
{
int idNum;
double payRate;
char firstName, lastName;
};
int main()
{
int error;
const int SIZE = 5;
Employee employee[SIZE];
for (int k = 0; k < SIZE; ++k)
{
employee[k].idNum = 0;
employee[k].payRate = 0;
}
for (int count = 0; count < SIZE; ++count)
{
error = 0;
cout << "Enter the employee's id number " << endl;
cin >> employee[count].idNum;
for (int i = 0; i < SIZE; ++i)
{
if (employee[i].idNum == employee[count].idNum)
error = 1;
}
while (error == 1)
{
cout << "Invalid entry. Please enter a new id number " << endl;
cin >> employee[count].idNum;
for (int i = 0; i < SIZE; ++i)
{
error = 0;
if (employee[i].idNum == employee[count].idNum)
error = 1;
}
}
cout << "Enter the employee's pay rate " << endl;
cin >> employee[count].payRate;
cout << "Enter the employee's first name " << endl;
cin >> employee[count].firstName;
cout << "Enter the employee's last name " << endl;
cin >> employee[count].lastName;
int choice;
cout << "Enter 1 to search for an employee by id number, enter 2 to search by last name, and enter 3 to search by pay "
<< endl;
cin >> choice;
}
int choice;
cout << "Enter 1 to search for an employee by id number, enter 2 to search by last name, and enter 3 to search by pay "
<< endl;
cin >> choice;
if (choice == 1)
{
int idNumC;
cout << "Enter an id number ";
cin >> idNumC;
for (int count = 0; count < SIZE; ++count)
{
if (employee[count].idNum == idNumC)
cout << employee[count].idNum;
}
}
if (choice == 2)
{
char name;
cout << "Enter the employee's last name " << endl;
cin >> name;
for (int count = 0; count < SIZE; ++count)
{
if (employee[count].lastName == name)
cout << "ID number: " << employee[count].idNum
<< " First name: " << employee[count].firstName
<< " Last Name: " << employee[count].lastName
<< " Hourly Pay: " << endl;
}
}
if (choice == 3)
{
int name;
cout << "Enter the employee's last name " << endl;
cin >> name;
for (int count = 0; count < SIZE; ++count)
{
if (employee[count].payRate == name)
cout << "ID number: " << employee[count].idNum
<< " First name: " << employee[count].firstName
<< " Last Name: " << employee[count].lastName
<< " Hourly Pay: " << endl;
}
}
}
My program also will not accept a value of more than one letter into the name. If I try and enter that into the program, the program keeps printing "Invalid entry" until I hit ctrl+c.
for (int i = 0; i < SIZE; ++i)
This checks every element in the array, including the one you have just read. You probably meant to put
for (int i = 0; i < count; ++i)
which will check every element up to (but not including) the one you have just read.
in
for (int i = 0; i < SIZE; ++i)
{
if (employee[i].idNum == employee[count].idNum)
error = 1;
}
employee[count] is one of the employee[i] you're going to compare against which means at some point you will
if (employee[count].idNum == employee[count].idNum)
Which is guaranteed to be true.
But if instead you
int tempId;
cin >> tempId;
for (int i = 0; i < SIZE; ++i)
{
if (employee[i].idNum == tempId)
error = 1;
}
and then set
employee[count].idNum = tempId;
at some later time, you can avoid this problem.
Addendum: I recommend picking this logic up and placing it in its own function. That way A) you don't have to repeat it inside the loop a few lines down where you're repeating the check for each retry and it gets the logic out of the way of the rest of the code. B) you can use the same function later for any other "Does this employee exist?" checks you need to write in the future.
In general You want to have lots of simple, easily-tested functions over one monolithic jack-of-all-trades.
I am trying to push a certain struct that exists in my private class into a vector of the class type records. I get the variable data in my main function but for some reason I keep getting an out of bounds error when trying to copy the information into the struct. If you could explain the error in my method of pushing the struct into the class vector that would be great... I included my planned print function as well.
Here's the class :
class students
{
public:
// All necessary member functions here
students(int RUID, string first_name, string last_name, vector<double> quiz_grades, array<double, 3> exam_grades)
{
record records;
records.RUID = RUID;
records.first_name = first_name;
records.last_name = last_name;
for (int i = 0; i < quiz_grades.size(); ++i)
{
records.quiz_grades[i] = quiz_grades[i];
}
for (int i = 0; 0 < 3; ++i)
{
records.exam_grades[i] = exam_grades[i];
}
student_records.push_back(records);
}
void printRecords()
{
//vector<record>::iterator it = student_records.begin(); it != student_records.end(); ++it
for (unsigned int i = 0; i < student_records.size(); ++i)
{
cout << "Ruid: " << student_records[i].RUID << endl;
cout << "First Name: " << student_records[i].first_name << endl;
cout << "Last Name: " << student_records[i].last_name << endl;
for (unsigned int j = 0; j < student_records[i].quiz_grades.size(); ++j)
{
cout << "Quiz grade " << j + 1 << " is: " << student_records[i].quiz_grades[j] << endl;
}
for (int k = 0; k < 3; ++k)
{
cout << "Test grade " << k + 1 << " is: " << student_records[i].exam_grades[k] << endl;
}
}
}
// using the friend function in class
friend void change_name(students stdn); // passing all necessary inputs
private:
struct record
{
int RUID;
string first_name;
string last_name;
vector<double> quiz_grades;
array<double, 3> exam_grades = { 0,0,0 };
};
vector<record> student_records;
};
Here's my main function:
int main()
{
string input;
bool quit = false;
int RUID;
string first;
string last;
double grade = 100;
vector<double> quizG;
array <double, 3> examG = { 0, 0, 0 };
cout << " --'new' to add, 'quit' to end--" << endl;
while (quit != true)
{
cout << "Please enter would you would like to do: ";
cin >> input;
cout << endl;
if (input == "quit")
{
quit = true;
break;
}
if (input == "new")
{
cout << "Please enter the RUID: ";
cin >> RUID;
cout << endl << "Please enter the first name: ";
cin >> first;
cout << endl << "Please enter the last name: ";
cin >> last;
cout << "Enter quiz grades. Enter number less than 0 to stop." << endl;
while (grade >= 0)
{
cout << "Enter quiz grade: ";
cin >> grade;
if (grade >= 0)
{
quizG.push_back(grade);
}
else if (grade < 0)
{
break;
}
}
for (int i = 0; i < 3; ++i)
{
cout << "Please enter " << i + 1 << " test grade: ";
cin >> grade;
examG[i] = grade;
}
}
students stdn(RUID, first, last, quizG, examG);
//stdn.printRecords();
}
// change_name(stdn);
return 0;
In students::students(),
for (int i = 0; 0 < 3; ++i)
// ^^^ should be `i`
is obviously wrong.
How come when I do fractionAry[i] = new Fraction(num1, denom1) it works and stores the created fraction into index of i.
But when I do fractionAry[i][j] = new Fraction(num1, denom1), the compiler gives me an error saying that no operator matches these operands?
If I would like to store the fraction into j, how would I do that? I am not entirely sure how to store a fraction into an array of array of fractions...
Here's my code, thanks for the help ahead of time.
void createArray() {
Fraction** fractionAry;
int aryCount;
int arySize;
int i, j;
int num1, denom1;
cout << "\nHow many arrays of fractions (treating these as array of arrays of fractions? ";
cin >> aryCount;
if (aryCount > 0) {
fractionAry = new Fraction*[aryCount];
for (i = 0; i < aryCount; i++) {
cout << "\nCreating array index # " << i
<< "\n How many fractions(s)? ";
cin >> arySize;
if (arySize > 0) {
fractionAry[i] = new Fraction[arySize + 1];
fractionAry[i][0] = arySize;
for (j = 1; j < arySize + 1; j++) {
cout << "\n Enter the numerator: ";
cin >> num1;
cout << " Enter the denominator: ";
cin >> denom1;
while (denom1 == 0) {
cout << "\nCan't set to 0! Enter a new denominator: ";
cin >> denom1;
}
fractionAry[i] = new Fraction(num1, denom1);
// fractionAry[i][j] = new Fraction(num1, denom1); I would like to do this instead
}
cout << "\nFor array index #" << i << endl;
for (j = 0; j < arySize + 1; j++) {
cout << " Element index #" << j << " : " << *(*(fractionAry + i) + j) << endl;
}
}
}
}
}
Instead of having fractionAry[i][j] = new Fraction(...) use fractionAry[i][j] = Fraction(...). The type at fractionAry[i][j] is Fraction not Fraction* which is what the new operator returns.
void createArray() {
Fraction** fractionAry;
int aryCount;
int arySize;
int i, j;
int num1, denom1;
cout << "\nHow many arrays of fractions (treating these as array of arrays of fractions? ";
cin >> aryCount;
if (aryCount < 1)
{
//print error message or exit function
}
fractionAry = new Fraction*[aryCount];
for (i = 0; i < aryCount; i++)
{
cout << "\nCreating array index # " << i
<< "\n How many fractions(s)? ";
cin >> arySize;
if (arySize < 1)
{
//Print error message decrement i and continue or exit function
}
fractionAry[i] = new Fraction[arySize + 1];
//I don't understand the purpose of assigning the below value or making the array one size bigger
fractionAry[i][0] = arySize;
///
for (j = 1; j < arySize + 1; j++)
{
cout << "\n Enter the numerator: ";
cin >> num1;
cout << " Enter the denominator: ";
cin >> denom1;
while (denom1 == 0)
{
cout << "\nCan't set to 0! Enter a new denominator: ";
cin >> denom1;
}
fractionAry[i][j] = Fraction(num1, denom1);
}
cout << "\nFor array index #" << i << endl;
for (j = 0; j < arySize + 1; j++)
{
cout << " Element index #" << j << " : " << fractionAry[i][j] << endl;
}
}
}