Error: No operator = matches these operands - c++

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;
}
}
}

Related

Exception handling in c++ how do I implement it here

I just created a program in c++ about adding matrices but I just dont know how to exception handle the part where to choose a number from 1 to 10 so that the user can only choose a number from 1 to 10 and if he puts a wrong input in it shows an error message and is asked to input a number again
#include <iostream>
using namespace std;
int r, c, a[10][10], b[10][10], sum[10][10], i, j;
void matrix(string s) {
cout << "Enter number of rows (between 1 and 10): ";
cin >> r;
cout << "Enter number of columns (between 1 and 10): ";
cin >> c;
}
void storeValues(string s) {
cout << endl << "Enter elements of " << s << " matrix:" << " " << endl;
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
cout << "Enter element " << i + 1 << j + 1 << " : ";
if (s == "1st") {
cin >> a[i][j];
}
else
cin >> b[i][j];
}
}
void addMatrices() {
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
sum[i][j] = a[i][j] + b[i][j];
}
void displayResult() {
cout << endl << "Sum of two matrix is: " << endl;
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
cout << sum[i][j] << " ";
if (j == c - 1)
cout << endl;
}
}
int main()
{
string s = "1st";
matrix(s);
storeValues(s);
s = "2nd";
storeValues(s);
addMatrices();
displayResult();
return 0;
}
You could easily make a function that handles bounded input for you, something like:
int bounded_input(int lower, int upper, std::string message) {
int r;
bool valid = false;
cout << message << " (between " << lower << " and " << upper << "): ";
do {
cin >> r; // NOTE: You will want to check for invalid input like "three" ...
if (r < lower || r > upper) {
cout << "Invalid, please enter between " << lower << " and " << upper << ":";
} else {
valid = true;
}
} while (!valid);
return r;
}
Then you can simply call this for all of your bounds:
int row_bound = bounded_input(1, 10, "Enter row bounds");

Showing values in an array

I have been coding another programming challenge from a book. It is about asking a user to input n numbers inside an array. Every time the user inputs, the current numbers entered should show up. Numbers less than or equal to zero should not be accepted. I have managed to do the first condition. However, it even shows the "empty" slots. I have tMy code will explain it. Here:
#include <iostream>
using namespace std;
int main() {
int size = 0, input_num [100], i, j;
cout << "Enter an array size: ";
cin >> size;
if (size <= 0) {
while (size <= 0) {
cout << "Enter an array size again: ";
cin >> size;
}
}
cout << "\n\nYou may now enter " << size << " numbers ";
cout << "\n------ ------ ------ ------\n";
for (i = 0; i < size; i++) {
cout << "Enter a number: ";
cin >> input_num [i];
cout << "\nEntered Numbers: ";
for (j = 0; j < size; j++) {
cout << input_num [j] << " " ;
}
cout << "\n";
}
}
In your output section, it should be j <= i, not j < size.
cout << "\nEntered Numbers: ";
for (j = 0; j <= i; j++) {
cout << input_num [j] << " " ;
}
cout << "\n";

Need advice with my average calculator code

Good day,
I need help with my below code. I have a problem with calculating the average.
The code works as it should expect for calculating the average of the second entry.
I have highlighted the average that does not add up in the below picture. Its weird that the average is only not adding up on the second entry?
#include <iostream>
using namespace std;
float snelheid[][50] = {{}};
float lading[] = {};
float snelheidGem[] = {0};
int ladingNo = 1;
int snelheidToets = 1;
int main()
{
cout << "Hoeveel ladings word getoets?: ";
cin >> ladingNo;
for (int i = 0; i < ladingNo; i++)
{
int n = i + 1;
if (n == 1 || n == 8 || n > 19)
{
cout <<"Wat was die " << n << "st lading?: ";
cin >> lading[i];
cout <<"Hoeveel skote was geskiet vir die "<< n << "st lading?: ";
cin >> snelheidToets;
snelheidGem[i] = {0};
for (int t = 0; t < snelheidToets; t++)
{
int m = t + 1;
if (m == 1 || m == 8 || m > 19)
{
cout <<"Wat was die spoed van die " << m << "st skoot?: ";
cin >> snelheid[i][t];
}
else
{
cout <<"Wat was die spoed van die " << m << "de skoot?: ";
cin >> snelheid[i][t];
}
snelheidGem[i]=snelheidGem[i]+snelheid[i][t];
}
snelheidGem[i] = snelheidGem[i]/snelheidToets;
cout << snelheidGem [i] << endl;
cout << endl;
}
else
{
cout <<"Wat is die " << n << "de lading?: ";
cin >> lading[i];
cout <<"Hoeveel skote was geskiet vir die "<< n << "de lading?: ";
cin >> snelheidToets;
snelheidGem[i] = {0};
for (int t = 0; t < snelheidToets; t++)
{
snelheid[i][t] = {0};
int m = t + 1;
if (m == 1 || m == 8 || m > 19)
{
cout <<"Wat was die spoed van die " << m << "st skoot?: ";
cin >> snelheid[i][t];
}
else
{
cout <<"Wat was die spoed van die " << m << "de skoot?: ";
cin >> snelheid[i][t];
}
snelheidGem[i]=snelheidGem[i]+snelheid[i][t];
cout << snelheid[i][t] << " snelheid" << endl;
cout << snelheidGem[i] << " gemiddeld" << endl;
}
snelheidGem[i] = snelheidGem[i]/snelheidToets;
cout << snelheidGem [i] << endl;
cout << endl;
}
}
return 0;
}
Average calculation
On this line:
float snelheidGem[] = {0};
you are creating an array of size 1, but are then indexing elements such as snelheidGem[1], and snelheidGem[2], etc, which invokes undefined behavior (UB). UB means anything can happen, including giving the correct average sometimes, but not at other times.
You can give it a sufficiently large size:
float snelheidGem[50] = {0};
or much better, use a std::vector, to which you can add elements by doing push_back.
Note that you have the same issue with:
float lading[] = {};

error string subscript out of range c++

i have a guessed name game. and i have a problem with this function. in the nested loop if the user entered a wrong letter it gives me this error:
line: 1440
expression: string subscript out of range
this is the function:
void Play(int selection, int FArraySize, int MArraySize,string Female[], string Male[])//Receive the selected name category array and its size and implements the game logic as shown in the sample run. Uses function GetRandomName(…).
{
int MAX_TRIES = 4;
int m = 0;
int x=0;
int j=0;
ofstream ofFile;
ifstream InFile;
int num_of_wrong_guesses=0;
char letter;
string GuessedName;
GuessedName = GetRandomName(selection, FArraySize, MArraySize, Female, Male);
cout << "Guess the following name:" << endl;
for(int y = 0; y < GuessedName.length(); y++){
cout << "?";
j++;
}
cout << "\nEnter a guess letter? or * to enter the entire name" << endl;
cin >> letter;
int i =0;
for ( int count = 0 ; count <= MAX_TRIES ; count++)
{
while (i <= j)
{
if (letter == GuessedName[i])
{
i = m;
cout << letter << " exists in the name";
cout << "\nyou have " << MAX_TRIES << " remaining guess attemps... "<< endl;
break;
}
if (i == j)
{
cout <<"Sorry! " << letter << " dose not exist in the name";
cout << "\nyou have " << MAX_TRIES-- << " remaining guess attemps... ";
break;
}
i++;
}
cout << "\nGuess the following name:" << endl;
for(int y = 0; y < GuessedName.length(); y++){
cout << "?";
j++;
}
cin >> letter;
}
return;
}
hope you can help me.
while (i <= j) should be while (i < j).
Otherwise GuessedName[i] will try to access out-of-bounds when i == j. j - 1 is the index of the last letter.

Set element in array to 0 or NULL

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 );