Array and module - c++

Hello i am working on a program to store a users input of prices into an array. then have a module accept the price array and return tax. here is what i have so far. my module keeps producing LNK2019.
#include <iostream>
using namespace std;
double PrArray[20] = {}, total_price, tax_rate, price, tax;
int i;
double TAXOWED();
void main()
{
cout << "input the sales tax rate as a decimal ";
cin >> tax_rate;
cout << "\nplease enter a price or 0 to quit ";
i = 0;
cin >> price;
PrArray[i] = price;
while (price > 0 && i < 19)
{
i++;
cout << "please enter a price or 0 to quit ";
cin >> price;
PrArray[i] = price;
}
for (i = 0; i < 19; i++)
{
cout << "\n";
cout << PrArray[i];
}
TAXOWED();
system("PAUSE");
return;
}
double TAXOWED(double PrArray[], int i)
{
double total_tax;
for (i = 0; i < 19; i++)
{
tax = tax_rate * PrArray[i];
total_tax = tax;
}
return(total_tax);
}

Your function declaration and definition don't match
// Declaration
double TAXOWED();
// Definition
double TAXOWED(double PrArray[], int i);
So essentially your are declaring a function and never defining it, then defining a separate function below.

Relaying on your comment, the code should looks like this:
#include <iostream>
using namespace std;
double TAXOWED(double price, double tax_rate)
{
return tax_rate * price;
}
int main()
{
double PrArray[20], total_tax[20], tax_rate, price;
cout << "input the sales tax rate as a decimal ";
cin >> tax_rate;
cout << "\nplease enter a price or 0 to quit ";
cin >> price;
if ( price == 0 )
return -1;
int i = 0;
PrArray[i] = price;
for (i = 1; i < 20; i++)
{
cout << "please enter a price or 0 to quit " ;
cin >> price;
PrArray[i] = price;
}
for (int i = 0 ; i < 20; i++)
total_tax[i] = TAXOWED(PrArray[i], tax_rate);
for (int i = 0; i < 20; i++)
cout << "Tax for price #" << i << " : " << total_tax[i] << endl;
system("PAUSE");
return 0;
}
This code will allow you to set tax_rate, each price value and output tax_rate for each price.

Ok this is what i have now. i need the chart at the end to have all the total prices added together
#include <iostream>
using namespace std;
// declerations
double PrArray[20], total_tax[20], tax_rate, price, total_price;
int i;
double TAXOWED(double price, double tax_rate)
{
return tax_rate * price;
}
void main()
{
cout << "input the sales tax rate as a decimal ";
cin >> tax_rate;
cout << "\nplease enter a price or 0 to quit ";
i = 0;
cin >> price;
PrArray[i] = price;
while (price > 0 && i < 19)
{
i++;
cout << "please enter a price or 0 to quit ";
cin >> price;
PrArray[i] = price;
}
for (int i = 0; i < 20; i++)
{
total_tax[i] = TAXOWED(PrArray[i], tax_rate);
}
system("CLS");
cout << " Prices";
cout << "\ntaxrate % " << tax_rate;
cout << "\nprices tax Total price ";
cout << "\n__________________________________________________";
for (int i = 0; i < 20; i++)
{
cout << "\n" << PrArray[i] << " " << total_tax[i] << " " << PrArray[i] + total_tax[i];
}
for (int)
system("PAUSE");
return;
}

Related

C++ Array display winner's name

I have to calculate the total of scores the participants received from the judges. And the one who gets the highest score will be the winner. I have to display the name of the winner but it displays the wrong winner. how do I solve this?
int main(){
int scores[5][2], i, j; int sum =0;
string name [5];
for(int i=0;i<5;i++)
{
cout << "Enter participant's name: \n";
cin >> name[i];
cout<<"Enter scores for " << name[i] <<endl;
for(j=0;j<2;j++)
{
cin >> scores[i][j];
}
}
for(int i=0; i<5; i++)
{
sum = 0;
for(j=0; j<2; j++)
{
sum += scores[i][j];
}
cout<<"Total scores of " << name[i] << " " <<" is "<<sum<<endl;
}
int max, min, count;
int index[5];
max = min, count=0;
index[5]={0};
for(int i=0; i<5; i++)
{
for(j=0; j<2; j++)
{
if (scores[i][j]>max)
{
max=scores[i][j];
}
}
for(int i=0; i<5; i++)
{
if(scores[i][j]==max)
{
index[count]=scores[i][j];
count++;
}
}
if (count==1)
cout<<"The winner is " << index[count-1] << name[i] << endl;
}
return 0;
}
You could use a running maxima algorithm without having to store all entries.
int main()
{
std::string name;
int max_score = -1;
std::string owner_max_score;
cout << "Enter participant's name: \n";
while (std::getline(cin, name))
{
int score1, score2;
cout<<"Enter 2 scores for " << name <<endl;
cin >> score1 >> score2;
if (score1 > max_score)
{
max_score = score1;
owner_max_score = name;
}
if (score2 > max_score)
{
max_score = score2;
owner_max_score = name;
}
cout << "\nEnter participant's name: \n";
}
cout << owner_max_score << " has highest score of " << max_score << endl;
return 0;
}
To stop the loop, terminate the input. Some platforms use Ctrl-D, some use Ctrl-Z or Ctrl-C.
You could also use a std::vector of structs:
struct Student
{
std::string name;
int score1;
int score2;
void console_input()
{
std::cout << "\n"
<< "Enter student's name:\n";
std::getline(std::cout, name);
std::cout << "\n"
<< "Enter 2 scores for "
<< name
<< "\n";
std::cin >> score1 >> score2;
}
int max_score() const
{
return std::max(score1, score2);
}
};
int main()
{
Student s;
std::vector<Student> student_scores;
std::cout << "Enter quantity of students: ";
std::cint >> student_quantity;
for (int i = 0; i < student_quantity; ++i)
{
s.console_input();
student_scores.push_back(s);
}
std::string owner_max_score = student_scores[0].name;
std::int highest_score = student_scores[0].max_score();
for (int i = 1; i < student_quantity; ++i)
{
const int student_max_score = student_scores[i].max_score();
if (student_max_score > highest_score)
{
highest_score = student_max_score;
owner_max_score = student_scores[i].name;
}
}
std::cout << "\n\n"
<< owner_max_score
<< " has highest score: "
<< highest_score
<< "\n";
return 0;
}

Creating a Script to Calculate multiple students and multiple graded assignments that are weighted different amounts

Creating a Script to Calculate multiple students and multiple graded assignments that are weighted different amounts.
//
// main.cpp
// PWGHelper
//
// Created by Lyle Baer on 1/18/16.
// Copyright © 2016 Lyle Baer. All rights reserved.
//
//
#include <iostream>
#include <vector>
using namespace std;
// Main Script
int main()
{
// Vector to store Calculated Grades
vector <int> calculatedGrades;
cout << "Enter Number of Students: \n\n";
int(nStudent);
cin >> nStudent;
// Loop
if (nStudent < 1)
{
const int MAX = 50;
float grades[MAX];
float weights[MAX];
// Greeting
cout << "Hello, its me\n\n";
cout << "Your Personal Weighted Grade Helper\n\n";
cout << "Call me PWG Helper for short.\n\n";
// Input # of exercises
cout << "Enter number of exercises: ";
int num;
cin >> num;
// Input Weights
int correct = 0;
while (correct == 0)
{
for (int i = 0; i < num; i++)
{
cout << "\nWhat is the weight in percent? ";
cin >> weights[i];
weights[i] = weights[i] / (float)100.0;
}
correct = 1;
}
// Input Grades
for (int i = 0; i < num; i++)
{
cout << "\nWhat is the Grade? ";
cin >> grades[i];
}
// Values
cout << "Printed Values\n";
for (int i = 0; i < num; i++)
{
cout << grades[i] << "\n";
cout << weights[i] << "\n";
}
// Find Average
float sum = 0;
for (int i = 0; i < num; i++)
{
sum = sum + grades[i] * weights[i];
}
// Total Sum
cout << "This student has a score of: " << sum;
cin >> sum;
calculatedGrades.push_back(sum);
// Keep console open
cin.get();
cin.get();
}
else
{
// Variables
const int MAX = 50;
float grades[MAX];
float weights[MAX];
// Greeting
cout << "Hello, its me\n\n";
cout << "Your Personal Weighted Grade Helper\n\n";
cout << "Call me PWG Helper for short.\n\n";
// Input # of exercises
cout << "Enter number of exercises: ";
int num;
cin >> num;
// Input Weights
int correct = 0;
while (correct == 0)
{
for (int i = 0; i < num; i++)
{
cout << "\nWhat is the weight in percent? ";
cin >> weights[i];
weights[i] = weights[i] / (float)100.0;
}
correct = 1;
}
// Input Grades
for (int i = 0; i < num; i++)
{
cout << "\nWhat is the Grade? ";
cin >> grades[i];
}
// Values
cout << "Printed Values\n";
for (int i = 0; i < num; i++)
{
cout << grades[i] << "\n";
cout << weights[i] << "\n";
}
// Find Average
float sum = 0;
for (int i = 0; i < num; i++)
{
sum = sum + grades[i] * weights[i];
}
//Total Sum
cout << "This student has a score of: " << sum;
cin >> sum;
calculatedGrades.push_back(sum);
// Keep console open
cin.get();
cin.get();
}
return 0;
}
Example output of this code:
Enter Number of Students: 3
Hello, its me
Your Personal Weighted Grade Helper
Call me PWG Helper for short.
Enter number of exercises: 2
What is the weight in percent? 10
What is the weight in percent? 10
What is the Grade? 85
What is the Grade? 97
Printed Values
85
0.1
97
0.1
This student has a score of: 18.2

Simple grading program returning the wrong answer

Trying to write a simple program that takes six test grades, input by the user, drops the lowest, and then displays average of the remaining five. The program compiles fine, but outputs the wrong answer. Any pointers to where the logic error is occurring would be helpful. I am using these values as sample grades:
85.2
92.3
78.0
51.5
91.6
87.0
and the value returned should be 86.82, but the program is instead returning 80.08.
#include <iostream>
using namespace std;
const int SIZE = 6;
double grades[SIZE];
double low(double[]);
int main()
{
double preavg = 0;
double avg = 0;
cout << "This program will take an input of six test grades\n"
<< "and determine the average of the highest five grades.\n\n"
<< "Please input the first test grade\n";
cin >> grades[0];
cout << "Please input the second test grade\n";
cin >> grades[1];
cout << "Please input the third test grade\n";
cin >> grades[2];
cout << "Please input the fourth test grade\n";
cin >> grades[3];
cout << "Please input the fifth test grade\n";
cin >> grades[4];
cout << "Please input the sixth test grade\n";
cin >> grades[5];
for (int i = 0; i < SIZE; i++)
{
preavg += grades[i];
}
avg = (preavg - low(grades)) / (SIZE -1);
cout << "Your average grade is " << avg << "\n";
system("PAUSE");
return 0;
}
double low(double grades[SIZE])
{
double lowest;
lowest = 100;
for (int i = 0; i < SIZE; i++)
{
if (grades[i] < lowest)
lowest = grades[i];
}
return lowest;
}
Your issue here is possibly coming from the fact you're doing division in double precision using a const int. If you cast SIZE as a double and write the rest of your double numbers with a .0 on the end, it works fine:
#include <iostream>
using namespace std;
const int SIZE = 6;
double grades[SIZE];
double low(double[]);
int main()
{
double preavg = 0.0;
double avg = 0.0;
cout << "This program will take an input of six test grades\n"
<< "and determine the average of the highest five grades.\n\n"
<< "Please input the first test grade\n";
cin >> grades[0];
cout << "Please input the second test grade\n";
cin >> grades[1];
cout << "Please input the third test grade\n";
cin >> grades[2];
cout << "Please input the fourth test grade\n";
cin >> grades[3];
cout << "Please input the fifth test grade\n";
cin >> grades[4];
cout << "Please input the sixth test grade\n";
cin >> grades[5];
for (int i = 0; i < SIZE; i++)
{
preavg += grades[i];
}
avg = (preavg - low(grades)) / ((double)SIZE - 1.0);
cout << "Your average grade is " << avg << "\n";
system("PAUSE");
return 0;
}
double low(double grades[SIZE])
{
double lowest;
lowest = 100.0;
for (int i = 0; i < SIZE; i++)
{
if (grades[i] < lowest)
lowest = grades[i];
}
return lowest;
}

How do I enter a loop in another loop?

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int employeeNum = 0; int totalEmployees = 0;
double hourlyRate = 0.0; double totalhoursWork = 0.0;
double hoursWork = 0.0; double totalnetPay = 0.0;
double grossPay = 0.0; double averagehoursWork = 0.0;
double netPay = 0.0; double totalwithHoldings = 0.0;
double withHoldings = 0.0; double overTime = 0.0;
int x = 0;
while(employeeNum!= 9999)
{
cout <<" Enter Employee Number (9999 to Stop):";
cin >> employeeNum;
if(employeeNum ==9999)
break;
cout <<"Enter hourly rate:";
cin >> hourlyRate;
cout <<"Enter hours worked:";
cin >> hoursWork;
cout <<"Employee Number:"<<employeeNum << endl;
if(hoursWork <= 40)
{
grossPay= hoursWork * hourlyRate;
cout <<" Gross Weekly Pay=" << fixed <<setprecision(2)<< grossPay << endl;
}
else if (hoursWork > 40)
{
overTime = hoursWork-40;
grossPay = (overTime * 1.5 + 40)* hourlyRate;
cout <<" Gross Weekly Pay="<< fixed <<setprecision(2)grossPay << endl;
}
if( grossPay > 1000.00)
{
withHoldings= grossPay*0.28;
}
else if( grossPay <= 1000.00)
{
withHoldings= grossPay*0.21;
}
netPay= grossPay-withHoldings;
cout <<" Net Weekly Pay="<<fixed << setprecision(2) << netPay << endl;
totalhoursWork+=hoursWork;
totalnetPay+=netPay;
totalwithHoldings+= withHoldings;
averagehoursWork= totalhoursWork/totalEmployees;
totalEmployees++;
}
averagehoursWork= totalhoursWork/totalEmployees;
for (int x = 1; x < 44; x = x + 1)
cout <<"*";
cout << endl;
cout <<"Total Employees Entered=" << totalEmployees << endl;
cout <<" Total Hours Worked="<< fixed << setprecision(2) << totalhoursWork << endl;
cout <<" Average Hours Worked="<< fixed << setprecision(2) << averagehoursWork << endl;
cout <<" Total Net Pay="<<fixed << setprecision(2) << totalnetPay << endl;
cout <<" TotalwithHoldings=" << fixed << setprecision(2)<< totalwithHoldings << endl;
for (int x = 1; x < 44; x = x + 1)
cout <<"*";
cout << endl;
system("pause");
return 0;
}
Hourly rate must be greater than $7.25 and less than $100.00. Hours worked must be greater than 0 and less than 120. If the user enters invalid data display and appropriate error message and ask the user to re-enter. What statement should I use for this portion and where should I put it??
You can use Cin and Cout for Input and output,, just use do While loop
int employeeNum = 0;
do
{
Console.WriteLine("enter Employee /Number 9999 to STOP");
employeeNum = int.Parse(Console.ReadLine());
if (employeeNum == 9999)
break;
Console.WriteLine("enter hourly rate ");
double hourRate = Double.Parse(Console.ReadLine());
} while (employeeNum != 9999);
You can do something like this:
cout << "Enter hourly rate:";
cin >> hourlyRate;
while (hourlyRate <= 7.25 || hourlyRate >= 100) {
cout << endl << "Reenter hourly rate (must be in (7.25 - 100))";
cin >> hourlyRate;
}
But stackoverflow is not for others do you'r homework.

Confused about an exercise in my book

I need to be able to have the objects within the array be valid or invalid/ have value or have no value.
So if the user entered only 5 accounts out of 10 possible, it would throw away the last 5 that do not have any value what so ever, so as to not ask for a computed interest for an account that doesn't exist.
How do I do this?
#include <iostream>
#include <iomanip>
using namespace std;
class BankAccount
{
private:
int accountNum;
double accountBal;
static const double annualIntRate;
public:
void enterAccountData(int, double);
void computeInterest();
void displayAccount();
};
//implementation section:
const double BankAccount::annualIntRate = 0.03;
void BankAccount::enterAccountData(int number, double balance)
{
cout << setprecision(2) << fixed;
cout << "Enter the account number " << endl;
cin >> number;
accountNum = number;
while(number < 0 || number < 1000)
{
cout << "Account numbers cannot be negative or less than 1000 " <<
"Enter a new account number: " << endl;
cin >> number;
}
cout << "Enter the account balance " << endl;
cin >> balance;
accountBal = balance;
while(balance < 0)
{
cout << "Account balances cannot be negative. " <<
"Enter a new account balance: " << endl;
cin >> balance;
}
return;
}
void BankAccount::computeInterest()
{
const int MAX_ACCOUNTS = 10;
const int MONTHS_IN_YEAR = 12;
int months;
double rate = 0;
int counter = 0;
cout << endl << "How many months will the account be held for? " << endl;
cin >> months;
counter = 0;
do
{
accountBal = accountBal * annualIntRate + accountBal;
counter++;
}while(months > counter);
cout << endl << "Account # " << accountNum << " 's balance is:$" << accountBal << endl;
}
int main()
{
const int QUIT = 0;
const int MAX_ACCOUNTS = 10;
int counter;
int input;
int number = 0;
double balance = 0;
BankAccount accounts[MAX_ACCOUNTS];
//BankAccount display;
counter = 0;
do
{
accounts[counter].enterAccountData(number, balance);
cout << " Enter " << QUIT << " to stop, or press 1 to proceed.";
cin >> input;
counter++;
}while(input != QUIT && counter != 10);
for(counter = 0; counter < MAX_ACCOUNTS; counter++)
{
accounts[counter].computeInterest();
}
system("pause");
return 0;
}
You should try tonarrow down your questions to just what you are really asking, this is a little confusing to look at.
After the first loop, have it "remember" what value for counter it managed to reach, and then in the second loop only iterate up to that, instead of up to MAX_ACCOUNTS.
And converting from months to years is just dividing by 12 no?
It seems you have skipped the chapters about Arrays. Re-read it!
ps. surely 0 < 1000 while(number < 0 || number < 1000)
void BankAccount::enterAccountData(int number, double balance). What is the need for the two arguments ? Take the input directly for the member variables. Unnecessary operations are commented. This would also do the job -
void BankAccount::enterAccountData()
{
cout << setprecision(2) << fixed;
cout << "Enter the account number " << endl;
cin >> accountNum; // Take input directly to accountNum
// accountNum = number; // No need of this assignment
while(accountNum < 0 || accountNum < 1000)
{
cout << "Account numbers cannot be negative or less than 1000 " <<
"Enter a new account number: " << endl;
cin >> accountNum;
}
cout << "Enter the account balance " << endl;
cin >> accountBal;
// accountBal = balance; // Unnecessary assignment
while(accountBal < 0)
{
cout << "Account balances cannot be negative. " <<
"Enter a new account balance: " << endl;
cin >> accountBal;
}
// return; // What is the need of this return statement? Method signature says
// it doesn't return anything.
}
When you think of declaring variables, think of whether they are necessary. Unnecessary declaring and assigning the variables make the problem looks complex and is the source of confusion. Think simple :)