These are my two errors:
error C2664: 'StudentMax' : cannot convert parameter 1 from 'int' to 'int []'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
This is my code, what do I need to fix?
// This program reads in data from a txt file, puts them in an array and calculates grades using that data.
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <fstream>
#include <string>
using namespace std;
float lab1, lab2, lab3, lab4, lab5, lab6, lab7, lab8, lab9, lab10;
float hw1, hw2, hw3, hw4, hw5, hw6, hw7, hw8;
float midterm = 3*(midterm);
float finall = 4*(finall);
float labScore;
float labAvg;
float hwScore;
float hwAvg;
float finalGrade;
string letterGrade;
string studentNameFirst;
ifstream inFile;
// prototypes for arrays
const char* names[10] = // kids names
{
"Mike",
"Carol",
"Greg",
"Marcia",
"Peter",
"Jan",
"Bobby",
"Cindy",
"Alice",
"Sam"
};
int* averages[10]; // number grades
string KidsLetterGrades[10]; // letter grades
int scores[4] = //elements comprising the final grade
{
labAvg,
hwAvg,
midterm,
finall
};
// function prototypes
int lowestScore(int scores[]);
int StudentMax(int scores[]);
void ComputeCourseGrade(int* averages[], string KidsLetterGrades[]);
void PrintInfo(const char* names[], string KidsLetterGrades[], int* averages[]);
int main(){
inFile.open("inputGrades.txt", ios::in); // opens input file
if (!inFile)
{
cout << "Cannot open input file. "; // file not open
return 1;
}
while (!inFile.eof( )) // until file ends
{
inFile >> studentNameFirst >> lab1 >> lab2 >> lab3 >> lab4 >> lab5 >> lab6 >> lab7 >> lab8 >> lab9 >> lab10 >> hw1 >> hw2 >> hw3 >> hw4 >> hw5 >> hw6 >> hw7 >> hw8 >> midterm >> finall;
ComputeCourseGrade(averages, KidsLetterGrades);
PrintInfo(names, KidsLetterGrades, averages);
}
return 0;
}
void ComputeCourseGrade(int* averages, int scores[], string letterGrade)
{
for(int i = 0; i < 10; i++)
{
labScore = .2*((lab1 + lab2 + lab3 + lab4 + lab5 + lab6 + lab7 + lab8 + lab9 + lab10)/100)*100;
labAvg = (lab1 + lab2 + lab3 + lab4 + lab5 + lab6 + lab7 + lab8 + lab9 + lab10)/10;
hwScore = .1*((hw1 + hw2 + hw3 + hw4 + hw5 + hw6 + hw7 + hw8)/80)*100;
hwAvg = (hw1 + hw2 + hw3 + hw4 + hw5 + hw6 + hw7 + hw8)/8;
finalGrade = ((labScore) + (hwScore) + (midterm) + (finall)); //attempting to make the lowest grade of the 4 things = to full credit for that grade aka dropping the lowest grade.... ideas on how?
lowestScore(scores);
finalGrade = finalGrade - lowestScore(scores);
finalGrade = finalGrade/4;
finalGrade = averages[i]; //assigns final grade in number form to array elements of average
if ( finalGrade >= 90)
letterGrade = "A";
else if (finalGrade >= 85)
letterGrade = "A-";
else if (finalGrade >= 80)
letterGrade = "B+";
else if (finalGrade >= 75)
letterGrade = "B";
else if (finalGrade >= 70)
letterGrade = "B-";
else if (finalGrade >= 65)
letterGrade = "C+";
else if (finalGrade >= 60)
letterGrade = "C";
else if (finalGrade >= 55)
letterGrade = "C-";
else if (finalGrade >= 50)
letterGrade = "D+";
else if (finalGrade >= 45)
letterGrade = "D";
else if (finalGrade >= 40)
letterGrade = "D-";
else letterGrade = "F";
letterGrade = KidsLetterGrades[i]; // assigns letter grades to array elements for kids letter grades
}
return;
}
int StudentMax(int averages[]) // to find student with highest class grade
{
int max = averages[0];
for( int m = 0; m < 10; m++)
{
averages[m];
if(averages[m] > max)
{ // finds maximum number among array elements
max = averages[m];
}
}
return max;
}
void PrintInfo(const char* names[], const char* KidsLetterGrades[], int averages[])
{
int count = 0;
StudentMax( averages[count]);
while (count < 10)
{
names[count];
cout << names[count] << KidsLetterGrades[count] << StudentMax( averages) << endl;
}
count++; // prints names then letter grades then averages
return;
}
int lowestScore(int scores[])
{
int lowest = scores[0];
for( int n = 0; n < 4; n++)
{
scores[n];
if(scores[n] < lowest)
{
lowest = scores[n];
}; //finds lowest number among array elements
}
return lowest;
}
You're passing an int to StudentMax, which expects an array (int[]).
You currently have StudentMax( averages[count]);, which is the offending line. You're not doing anything with that result, though, so just delete the line. That should fix both errors.
Related
I have this program, which should give me the smallest original price of the item based on the input from the user.
There are some conditions, for example, if the quantity of the mask is more than 9 the price will be discounted by 10%, 15% if its more than 30 and 20% if it's more than 50. The result should give the answer Here is my code:
#include <iostream>
#include <iomanip>
using namespace std;
int mprice; //price input
int mquantity; //quantity input
int n; //first input
int fee = 2000; //const fee
float finalprice;
float maskCalc(int price, int quantity) {
float holder = (float)(price - fee) / (float)(quantity);
if (quantity > 0) {
finalprice = holder;
}
//if between 10 and 30
else if (quantity > 9) {
finalprice = holder / 0.9;
}
//between 30 and 49
else if (quantity > 30) {
finalprice = holder / 0.85;
}
//more than 50
else if (quantity > 49) {
finalprice = holder / 0.8;
}
//less than ten
else {
finalprice = holder;
}
return finalprice;
}
int main()
{
cin >> n;
float arr[n];
// Input oruulah loop
for (int i = 0; i < n; i++) {
cin >> mprice >> mquantity;
x = maskCalc(mprice, mquantity);
arr[i] = x;
}
for (int i = 1; i < n; i++) {
if (arr[0] > arr[i]) {
arr[0] = arr[i];
}
}
printf("%.2f", arr[0]);
return 0;
}
I gave the input
3
5000 3
7000 10
3000 1
the answer was 555.56 which is correct, but when I give something like
3
2500 1
7000 10
3000 1
it is giving me 0.00 while I was expecting this to give me 500.00. Any help will be appreciated.
You need to check for the highest quantity first in your if-else switch, otherwise you always fall into the default (<10) case.
//more than 50
if (quantity >= 50) {
finalprice = holder / 0.8;
}
//between 30 and 49
else if (quantity >= 30) {
finalprice = holder / 0.85;
}
//if between 10 and 30
else if (quantity >= 10) {
finalprice = holder / 0.9;
}
//less than ten
else {
finalprice = holder;
}
This is what I have so far but I am not getting the right outputs.
#include <iostream>
using namespace std;
int main(void) {
int year_number, month_number, day_number;
cout << "What year were you born in?\n";
cin >> year_number;
cout << "What month were you born in?\n";
cin >> month_number;
cout << "What day were you born on?\n";
cin >> day_number;
month_number -= 2;
if (month_number < 0) {
month_number += 12;
year_number -= 1;
}
month_number *= 83 / 32;
month_number += day_number;
month_number += year_number;
month_number += (year_number / 4);
month_number -= (year_number / 100);
month_number += (year_number / 400);
day_number = month_number % 7;
cout << "The weekday number you were born on is " << day_number << endl;
return 0;
}
Here are the instructions:
Decrease month number by 2;
if month number becomes less than 0, increment it by 12 and decrement year by 1;
take month number and multiply it by 83 and divide it by 32;
add day number to month;
add year number to month;
add year/4 to month;
subtract year/100 from month;
add year/400 to month;
find the remainder of dividing month by 7;
I am not sure where you got those instructions for Zellers congruence. But here is an implementation that follows the formula of wikipedia.
#include <iostream>
#include <math.h>
int main(void) {
std::string days[7] = { "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
int year_number = 2020;
int month_number = 1;
int day_number = 22;
if( month_number < 3 ){
month_number += 12;
year_number -= 1;
}
int century_number = year_number / 100;
year_number = year_number % 100;
int day = day_number + floor( (month_number + 1) * 13.0 / 5.0 ) +
year_number + floor( year_number / 4.0 ) +
floor( century_number / 4.0 ) -
2 * century_number;
day = day % 7;
std::cout << "The weekday you were born on is " << days[day] << std::endl;
return 0;
}
Live Demo
Another of your problems is probably that you don't floor your intermediate results where you should.
I have an issue with my program which is used to calculate addition and subtraction for Hexadecimals. The algorithm of my program is:
Take 2 strings inputted by the user, and "+" or "-" depending on the
operator they chose
Convert the 2 strings into decimals, and add or subtract
If the second number is bigger, use the bigger number to subtract the smaller number, and put a "-" in front of it when returning the result
(1 - 8 should = -7, but instead, I take 8 - 1 = 7, and return "-" and "7" so it gives "-7)
Take the decimal result from the operation and convert back to hexadecimal
Return string of hexadecimal
However, I have run into a issue where my calculations give me wrong answers.
(For e.g FFFFFF + FFFFFFFFFF prints "FFFFFFE" instead of "10000FFFFFE")
What can I do to solve the issue?
I created my own Power function for this program as I need a a number that can go up to 16 Fs for the Hexadecimal string.
Power Function:
unsigned long long int result = 1;
int i;
for (i = 0; i < y; i++)
{
result *= x;
}
return result;
Code:
int i;
int power = FirstHexaNumber.length() - 1;
int power2 = SeconHexaNumber.length() - 1;
int checkLength = FirstHexaNumber.length();
int checkLength2 = SeconHexaNumber.length();
unsigned long long int decimalNumber = 0;
unsigned long long int decimalNumber2 = 0;
unsigned long long int totalDecimal;
int temporary;
string result;
if (Operator == '+') //check if operator is add or minus
{
//Convert Hex to Decimal for first number
for (i = 0; i < checkLength; i++)
{
if (int(FirstHexaNumber[i]) >= 48 && int(FirstHexaNumber[i]) <= 57) { // check if FirstHexaNumber 0 to 9
decimalNumber += ((int(FirstHexaNumber[i])) - 48) * powerFunc(16, power); //formula to convert hexadecimal into decimal, int(FirstHexaNumber[i]) is used to convert hexa into a number
}
else if (int(FirstHexaNumber[i]) >= 65 && int(FirstHexaNumber[i]) <= 70) // check if FirstHexaNumber is A to F
{
decimalNumber += ((int(FirstHexaNumber[i])) - 55)*powerFunc(16, power);
}
else if (int(FirstHexaNumber[i]) >= 97 && int(FirstHexaNumber[i]) <= 102) // check if FirstHexaNumber is a to f
{
int x = powerFunc(16, power);
decimalNumber += ((int(FirstHexaNumber[i])) - 87)* x;
}
power--; //power-- since it starts from "HexaNumber.length - 1". Power should decrease as assignment of power goes down
}
//Convert Hex to Decimal for second number
for (i = 0; i < checkLength2; i++)
{
if (int(SeconHexaNumber[i]) >= 48 && int(SeconHexaNumber[i]) <= 57) {
decimalNumber2 += ((int(SeconHexaNumber[i])) - 48) * powerFunc(16, power2); //formula to convert Hexadecimal to Decimal
}
else if (int(SeconHexaNumber[i]) >= 65 && int(SeconHexaNumber[i]) <= 70)
{
decimalNumber2 += ((int(SeconHexaNumber[i])) - 55)*powerFunc(16, power2); //formula to convert Hexadecimal to Decimal
}
else if (int(SeconHexaNumber[i]) >= 97 && int(SeconHexaNumber[i]) <= 102)
{
unsigned long long int x = powerFunc(16, power2);
decimalNumber2 += ((int(SeconHexaNumber[i])) - 87)*x; //formula to convert Hexadecimal to Decimal
}
power2--;
}
totalDecimal = decimalNumber + decimalNumber2; //Adds the total decimal to convert into hexadecimal
if (totalDecimal == 0)
{
return "0";
}
//Converts Decimal to Hexadecimal
for (i = 0; totalDecimal != 0; i++) //as long as totalDecimal does not hit 0 from being divided by 16, run the loop
{
temporary = totalDecimal % 16; //use temporary as a variable to temporarily hold onto the number remainder of mod 16
if (temporary >= 10) //if temporary >= 10, that means it needs to be converted to alphabet
{
result.insert(0, 1, temporary + 55); //result.insert inserts a string of text into a spot, and pushes everything else backwards.
} //in this case, result.insert assigns "temporary+55" into the spot of characters 0 to 1.
else //else, it means that the decimal will be a number, add 48 to convert to ascii
{
result.insert(0, 1, temporary + 48);
}
totalDecimal = totalDecimal / 16; //divide by 16 to move on to finding the next digit/alphabet
}
return result;
}
else if (Operator == '-') //check if operator is add or minus
{
//Convert Hex to Decimal for first number
for (i = 0; i < checkLength; i++) //as long as the loop does not exceed the length of the hexadecimal, run it
{
if (int(FirstHexaNumber[i]) >= 48 && int(FirstHexaNumber[i]) <= 57) {
decimalNumber += ((int(FirstHexaNumber[i])) - 48) * powerFunc(16, power);
}
else if (int(FirstHexaNumber[i]) >= 65 && int(FirstHexaNumber[i]) <= 70)
{
decimalNumber += ((int(FirstHexaNumber[i])) - 55)*powerFunc(16, power);
}
else if (int(FirstHexaNumber[i]) >= 97 && int(FirstHexaNumber[i]) <= 102)
{
decimalNumber += ((int(FirstHexaNumber[i])) - 87)*powerFunc(16, power);
}
power--;
}
//Convert Hex to Decimal for second number
for (i = 0; i < checkLength2; i++)
{
if (int(SeconHexaNumber[i]) >= 48 && int(SeconHexaNumber[i]) <= 57) {
decimalNumber2 += ((int(SeconHexaNumber[i])) - 48) * powerFunc(16, power2);
}
else if (int(SeconHexaNumber[i]) >= 65 && int(SeconHexaNumber[i]) <= 70)
{
decimalNumber2 += ((int(SeconHexaNumber[i])) - 55)*powerFunc(16, power2);
}
else if (int(SeconHexaNumber[i]) >= 97 && int(SeconHexaNumber[i]) <= 102)
{
decimalNumber2 += ((int(SeconHexaNumber[i])) - 87)*powerFunc(16, power2);
}
power2--;
}
if (decimalNumber >= decimalNumber2)
{
totalDecimal = decimalNumber - decimalNumber2; //subtract bigger number by smaller number.
if (totalDecimal == 0)
{
return "0";
}
for (i = 0; totalDecimal != 0; i++)
{
temporary = totalDecimal % 16;
if (temporary >= 10)
{
result.insert(0, 1, temporary + 55);
}
else
{
result.insert(0, 1, temporary + 48);
}
totalDecimal = totalDecimal / 16;
}
return result;
}
else
{
totalDecimal = decimalNumber2 - decimalNumber; //subtract bigger number by smaller number.
if (totalDecimal == 0)
{
return "0";
}
for (i = 0; totalDecimal != 0; i++)
{
temporary = totalDecimal % 16;
if (temporary >= 10)
{
result.insert(0, 1, temporary + 55);
}
else
{
result.insert(0, 1, temporary + 48);
}
totalDecimal = totalDecimal / 16;
}
return "-" + result;
}
}
return 0;
You can try this snippet:
int a,b;
cout << "\nEnter A in hex: ";
cin >> hex >> a;
cout << "\nEnter B in hex: ";
cin >> hex >> b;
cout << "\n Addition of " << hex << a <<" and "<< hex << b << " is " << hex << a+b;
cout << "\n Substraction of " << hex << a << " and " << hex << b << " is " << hex << a - b;
int x = powerFunc(16, power); should be long long x = powerFunc(16, power);
Don't know full source of the function pow, the return type should be long long too.
Hexadecimal Calculator can be more simple.
#include <sstream>
std::stringstream ss1(s1),ss2(s2);
ss1 >> std::hex >> i1;
ss2 >> std::hex >> s2;
std::cout << std::hex << std::uppercase << i1 + s2 << std::endl;
std::stringstream res;
res << std::hex << std::uppercase << i1 + i2;
return res.str();
below is a .cpp file that i am including in another .cpp file. I am getting an error that states emp.grosPay() must be a modifiable lvalue. any ideas on fixing it?
#include <iostream>
#include <string>
#include "weeklyEmp.h"
using namespace std;
const double WEEKLY_ALLOWANCE = 39.42;
const double FICA_TAX_RATE = 0.0765;
weeklyEmp::weeklyEmp()
{
my_name = ?name?;
}
weeklyEmp::weeklyEmp(string initName,
double initHours,
double initRate,
int initExemptions,
string initFilingStatus)
{
my_name; initName;
my_hours; initHours;
my_rate; initRate;
my_exemptions; initExemptions;
my_filingStatus; initFilingStatus;
}
//--modifiers
void weeklyEmp::set_hours(double thisWeeksHours)
//post: Set the hours worked for a given week
{
my_hours = thisWeeksHours;
}
void weeklyEmp::set_rate(double thisWeeksRate)
//post: Change the employee's hourly rate of pay
{
my_rate = thisWeeksRate;
}
//--accessors
double weeklyEmp::grosPay() const
//post: Return gross pay with overtime
{if(my_hours <=40) return my_hours * my_rate;
else
return (40*my_rate) + (my_hours-40) * 1.5 * my_rate;
}
double weeklyEmp::incomeTax() const
//post: Return the federal income tax
{
double result(0.0);
double taxableIncome(grosPay() - my_exemptions * WEEKLY_ALLOWANCE);
if(my_filingStatus == "S" || my_filingStatus == "s")
{
if (taxableIncome <= 23.00)
result = 0.00;
else if(taxableIncome <= 397.00)
result = 0.15 * (taxableIncome - 23.00);
else if(taxableIncome <= 928.00)
result = 56.10 + 0.28 * (taxableIncome - 397.00);
else if(taxableIncome <= 2121.00)
result = 204.78 + 0.33 * (taxableIncome - 928.00);
else
result = 598.47 + 0.28 * (taxableIncome - 2121.00);
}
if(my_filingStatus == "M" || my_filingStatus == "m")
{
if(taxableIncome <= 65.00)
result = 0.00;
else if(taxableIncome <= 689.00)
result = 0.15 * (taxableIncome - 65.00);
else if(taxableIncome <= 1573.00)
result = 93.60 + 0.28 * (taxableIncome - 689.00);
else if(taxableIncome <= 3858.00)
result = 341.12 + 0.33 * (taxableIncome - 1573.00);
else
result = 1095.17 + 0.28 * (taxableIncome - 3858.00);
}
/* round to the nearest penny */
/* include compfun.cpp for round function */
result =(result, 2);
return result;
}
double weeklyEmp::FICATax() const
//post: Return the social security tax
{
return grosPay() * FICA_TAX_RATE;
}
string weeklyEmp::name() const
//post: Return the employee's name
{ return my_name;
}
The portion of code with the error is below and marked * **
int main()
{
string name;
double rate;
double hours;
int exemptions;
string filingStatus;
cout <<"Name: ";
cin >> name;
cout << "Hourly Rate:";
cin >> rate;
cout << "Hours Worked:";
cin >> hours;
cout << "Exemptions: ";
cin >> exemptions;
cout<< "S) ingle / M) arried: ";
cin >> filingStatus;
cout << " " << endl;
weeklyEmp emp(name, hours, rate, exemptions, filingStatus);
double net = ***emp.grosPay()*** = emp.incomeTax() - emp.FICATax();
}
double net = emp.grosPay() = emp.incomeTax() - emp.FICATax();
// ^^^
grosPay doesn't return a reference, so I'm assuming you didn't mean to use an = sign there. May have been a slip of the thumb.
Did you mean to use the minus operator -?:
double net = emp.grosPay() - emp.incomeTax() - emp.FICATax();
// ^^^
The error was occurring because you can't modify a const object, an object returned by a const function, or an rvalue that isn't bound to a non-const rvalue-reference.
double net = ***emp.grosPay()*** = emp.incomeTax() - emp.FICATax();
^^^
The second '=' should be a '-' sign; I think you're trying to subtract, not assign.
Hi guys I am kinda new on c++ so I was writing a program and it works fine, but there is a problem. Every time I type number bigger than 100 my program crashes and I don't know why. Could anyone help me?
Program code:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int i = 10;
while(i > 0)
{
i--;
int b = 0, c = 1, d = 0, e, number, how = 0, number1, start, to, number2, split1, split2, mass, start1 = 0, start2 = 0, number3, how1, number4, number5;
cout << "\nIveskite skaiciu \n";
cin >> number;
cout << "\n";
number1 = number;
while(number1 > 0)
{
number1 = number1 / 10;
how = how + 1;
}
how1 = how - 1;
start = pow(10, (how - 1));
to = pow(10, how);
mass = to - start;
number2 = start - 1;
int split[mass][mass], numbers[mass], ok[mass];
while(start1 < mass)
{
start1++;
e = number2 + start1;
numbers[start1] = e;
split[start1][0] = e;
}
while(start2 < mass)
{
start2++;
number3 = numbers[start2];
d = 0;
b = 0;
c = 1;
while(d <= how1)
{
d++;
split1 = number3%10;
split2 = number3 / 10;
number3 = split2;
split[start2][d] = split1;
number4 = b + split[start2][d];
b = number4;
number5 = c * split[start2][d];;
c = number5;
}
if(number4 == number5)
{
ok[mass] = numbers[start2];
cout << number4 << " " << number5 << " >" << ok[mass] << endl;
}
}
}
Seems to me its the 2D array split that gets too large for your stack. You could probably try to allocate it dynamically as suggested here: how to deal with large 2D arrays