Basic Console Calculator (Storing a string in a variable) C++ - c++

I'm trying to create a basic console calculator in C++. I'm having a bit of trouble storing a string in a variable from a cin command.
Here is the program for some clarification:
#include <iostream>
using namespace std;
int main()
{
string type_cal;
cout << "Please enter the type of calculation you would like to use: \n";
cout << "1. Addition \n";
cout << "2. Subtraction \n";
cout << "3. Multiplication \n";
cout << "4. Division \n \n";
cin >> type_cal;
if (type_cal = "Addition" or "1")
{
int a;
int b;
int sum;
cout << "Please enter a number to add: \n";
cin >> a;
cout << "Please enter another number: \n";
cin >> b;
sum = a + b;
cout << "The sum of those numbers is: " << sum << endl;
return 0;
}
}
Currently I am in the addition phase since I recently ran into this problem. Quick answers would be appreciated, thank you!

if(type_cal = "Addition" or "1") simply does not make sense.
if(type_cal == "Addition" || type_cal == "1") {
}

Ok I found the problem, or is actually used as || in c++ (thanks aerkenemesis), and = is not the same as == which means equal to (another thanks to Lorehed). Program is working fine now.

For those who are curious, here is the new and revised version of my simple calculator:
#include <iostream>
using namespace std;
float addition();
float subtraction();
float multiplication();
float division();
int main()
{
string type_cal;
cout << "Please enter the type of calculation you would like to use: \n";
cout << "1. Addition " << endl;
cout << "2. Subtraction " << endl;
cout << "3. Multiplication " << endl;
cout << "4. Division" << endl << endl;
cin >> type_cal;
if(type_cal == "Addition")
{
addition();
}
if(type_cal == "Subtraction")
{
subtraction();
}
if(type_cal == "Multiplication")
{
multiplication();
}
if(type_cal == "Division")
{
division();
}
return 0;
}
float addition()
{
float a;
float b;
float sum;
cout << "Please enter a number to add: " << endl;
cin >> a;
cout << "Please enter another number: " << endl;;
cin >> b;
sum = a + b;
cout << "The sum of those numbers is: " << sum << endl;
}
float subtraction()
{
float c;
float d;
float difference;
cout << "Please enter a number to subtract: \n";
cin >> c;
cout << "Please enter another number: \n";
cin >> d;
difference = c - d;
cout << "The difference of those numbers is " << difference << endl;
}
float multiplication()
{
float e;
float f;
float product;
cout << "Please enter a number to multiply: \n";
cin >> e;
cout << "Please enter another number: \n";
cin >> f;
product = e * f;
cout << "The product of those numbers is " << product << endl;
}
float division()
{
float g;
float h;
float quotient;
cout << "Please enter a number to divide: \n";
cin >> g;
cout << "Please enter another number: \n";
cin >> h;
quotient = g / h;
cout << "The quotient of those numbers is " << quotient << endl;
}

Related

C++: Building a mulitfunctional calculator

I am trying to build a calculator in C++. I'm new to the program and have to do this for a school assignment, so sorry for my ignorance. English is also my second language so excuse me if I don't make much sense.
Let's say I have two integers A and B for which a user has to assign a value to either add, subtract, etc. How would I then be able add a third integer (let's say X) without all three showing up when I run the program? So instead of having to type a value for A, B, AND X, it only asks to type a value for X?
For example 4 + 5 = 9, but the calculator can also square numbers, so how do I get the option of a user just filling in 4 squared = 16, while still keeping the former code that lets me add and subtract two numbers?
Maybe seeing the code would help understand what I mean? Sorry if I'm confusing.
#include <iostream.h>
#include <conio.h>
int main ()
{
cout << "Calculator [v.1.0]" << endl;
cout << "(c) 2021 <Chayenne van der Erf>" << endl << endl;
cout << "Kies een bewerking en druk op Enter:" << endl;
cout << "1. Optellen 2. Aftrekken" << endl;
cout << "3. Vermenigvuldigen 4. Delen" <<endl;
cout << "5. Kwadraat 6. Worteltrekken" <<endl;
cout << "7. Reciproke 8. Logarithme" <<endl;
cout << "0. Exit" << endl << endl;
int Bewerking;
cout << "Bewerking: ";
cin >> Bewerking;
cout << "" << endl;
switch (Bewerking) {
case 1:
cout << "+";
break;
case 2:
cout << "-";
break;
case 3:
cout << "*";
break;
case 4:
cout << "/";
break;
default: "Invalid Number";
}
cout << "" << endl << endl;
double A, B;
cout << "Enter een waarde: ";
cin >> A;
cout << "Enter een waarde: ";
cin >> B;
int antwoord;
if (Bewerking == 1) {antwoord = A + B;}
else if (Bewerking == 2 ) {antwoord = A - B;}
else if (Bewerking == 3) {antwoord = A * B;}
else if (Bewerking == 4) {antwoord = A / B;}
cout << "" << endl;
cout << "= " << antwoord << endl;
getch();
return 0;
}
Make the variables, and the reading, conditional on the operation.
Example outline:
if (operation takes one input)
{
double x;
cin >> x;
Calculate result...
}
else if (operation takes two inputs)
{
double x, y;
cin >> x >> y;
Calculate result...
}
else if (operation takes three inputs)
{
double x, y, z;
cin >> x >> y >> z;
Calculate result...
}
Print result...

Function error in c++

I'm studying functions in c++ form a book called "jumping to c++" and there are a problem exercise that is create a calculator and I need make the arithmetic operation in separate functions, sound easy and I think I did it 90% good, the program gives me the correct answer but with some random numbers.
the code is:
#include <iostream>
using namespace std;
int a, b;
int sum()
{
return a + b;
}
int subs()
{
return a - b;
}
int div()
{
return a / b;
}
int mult()
{
return a * b;
}
int ask()
{
cout << "Give me the first number: ";
cin >> a;
cout << "\nGive me the second number: ";
cin >> b;
}
int main()
{
int opcion;
cout << "1. Sum \n2. Substraction \n3. Division \n4. Multiplication \n\nChoose one option from above: \n\n";
cin >> opcion;
if(opcion == 1)
{
cout << ask();
cout << "The result is: " <<sum() <<"\n\n";
} else if (opcion == 2)
{
cout << ask();
cout << "The result is: " << subs() <<"\n\n";
}else if (opcion == 3)
{
cout <<ask();
cout << "The result is: " << div() <<"\n\n";
}else if(opcion == 4)
{
cout << ask();
cout << "The result is: " << mult() <<"\n\n";
}else
{
cout << "Error.\n\n";
}
system("pause");
}
and this is the "error/bug/whatever"
1. Sum
2. Substraction
3. Division
4. Multiplication
Choose one option from above:
4
Give me the first number: 5
Give me the second number: 5
1878005856The result is: 25
Press any key to continue . . .
notice the error before of "The result is:"
appreciate any help, thanks
ask() does not return anything so it should be a void. Also, you do not need to do cout << ask(); since ask() already does the printing inside of it and it is a void (now) so it can't be printed.
Here is the code with the modifications, see comments with **** in front for changes:
#include <iostream>
using namespace std;
int a, b;
int sum() {
return a + b;
}
int subs() {
return a - b;
}
int div() {
return a / b;
}
int mult() {
return a * b;
}
void ask() { // **** Changed to void here
cout << "Give me the first number: ";
cin >> a;
cout << "\nGive me the second number: ";
cin >> b;
}
int main() {
int opcion;
cout << "1. Sum \n2. Substraction \n3. Division \n4. Multiplication \n\nChoose one option from above: \n\n";
cin >> opcion;
if (opcion == 1) {
ask(); // **** Removed cout <<
cout << "The result is: " << sum() << "\n\n";
} else if (opcion == 2) {
ask(); // **** Removed cout <<
cout << "The result is: " << subs() << "\n\n";
} else if (opcion == 3) {
ask(); // **** Removed cout <<
cout << "The result is: " << div() << "\n\n";
} else if (opcion == 4) {
ask(); // **** Removed cout <<
cout << "The result is: " << mult() << "\n\n";
} else {
cout << "Error.\n\n";
}
system("pause");
}
You can try it here
The random number was caused by you doing cout << ask(); even though you had not returned anything.
As aschepler pointed out "make sure you enable and read compiler warnings - there should be one saying that ask() doesn't return anything although declared to return an int."
The problem is your int ask() function.
It must return int value which you are writing to console with cout << ask();
The answer above won't work because you cannot write void to cout.
Since you do not return a value then a random number retruned. My compiler marks that as an error.
Replace type of ask function:
void ask()
{
cout << "Give me the first number: ";
cin >> a;
cout << "\nGive me the second number: ";
cin >> b;
}
Then replace cout << ask(); in every if statement with just ask();
Like this:
if (opcion == 1)
{
ask();
cout << "The result is: " << sum() << "\n\n";
}
else if (opcion == 2)
{
ask();
cout << "The result is: " << subs() << "\n\n";
}
else if (opcion == 3) ...
Consider checking if b==0 in case of devision operation. Or your program will crash if u try to devide by zero.
this function is returning a random integer. convert it to void
int ask()
{
cout << "Give me the first number: ";
cin >> a;
cout << "\nGive me the second number: ";
cin >> b;
}
new
void ask()
{
cout << "Give me the first number: ";
cin >> a;
cout << "\nGive me the second number: ";
cin >> b;
}

What should I do when one function finishes another one opens up?

I have a Problem. I'm Trying to make a regular Calculator and Shape's Area and Perimeter finder.It's a combination. I didn't start on my Shape's Area and Perimeter Finder. This is My main.cpp.
#include <iostream>
#include <string>
#include "AAPO.h" // Its a Header File.
using namespace std;
void Calculators_Operation();
int main()
{
string opera;
cout << "Do you want Arithmetic Calculator or Area and Perimeter Calculator"
<< endl;
cin >> opera;
if (opera == "Arithmetic Calculator" or "arithmetic calculator" or "AC")
{
Calculators_Operation();
}
return 0;
}
This is my Operation Chooser.
#include <iostream>
#include <string>
#include "Arithmetic Chooser.h"
using namespace std;
void Calculators_Addition();
void Calculators_Subtraction();
void Calculators_Multiplication();
void Calculators_Division();
void Calculators_Operation()
{
string answera;
cout << "What Operation do you Want?" << endl;
cin >> answera;
if (answera == "Addition" or "addition" or "+");
{
Calculators_Addition();
};
if (answera == "Subtraction" or "subtraction" or "-");
{
Calculators_Subtraction();
};
if (answera == "Multiplication" or "multiplication" or "*" or "x" or "X")
{
Calculators_Multiplication();
};
if (answera == "Division" or "division" or "/")
{
Calculators_Division();
};
return;
}
This is my AAPO.h.
#ifndef AAPO_H_INCLUDED
#define AAPO_H_INCLUDED
void Calculators_Operation();
#endif // AAPO_H_INCLUDED
My Addition.
#include <iostream>
#include <string>
using namespace std;
void calculators_Addition_2();
void calculators_Addition_3();
void calculators_Addition_4();
void calculators_Addition_5();
void Calculators_Addition()
{
//ADDITION COMPLETE
string numberadd;
cout << "How much numbers do you want?" << endl;
cin >> numberadd;
if (numberadd == "2")
{
calculators_Addition_2();
return;
};
if (numberadd == "3")
{
calculators_Addition_3();
return;
};
if (numberadd == "4")
{
calculators_Addition_4();
return;
};
if (numberadd == "5")
{
calculators_Addition_5();
return;
}
}
void calculators_Addition_2()
{
int add11;
int add12;
int sum;
cout << "Enter the first number" << endl;
cin >> add11;
cout << "Enter the second number" << endl;
cin >> add12;
sum = add11 + add12;
cout << "The sum of the numbers are " << sum << endl;
return;
}
void calculators_Addition_3()
{
int add13;
int add23;
int add33;
int sum2;
cout << "Enter the First Number" << endl;
cin >> add13;
cout << "Enter the Second Number" << endl;
cin >> add23;
cout << "Enter the Third Number" << endl;
cin >> add33;
sum2 = add13 + add23 + add33;
cout << "The Sum of the Numbers are " << sum2 << endl;
return;
}
void calculators_Addition_4()
{
int add14;
int add24;
int add34;
int add44;
int sum3;
cout << "Enter the First Number" << endl;
cin >> add14;
cout << "Enter the Second Number" << endl;
cin >> add24;
cout << "Enter the Third Number" << endl;
cin >> add34;
cout << "Enter the Fourth Number" << endl;
cin >> add44;
sum3 = add14 + add24 + add34 + add44;
cout << "The Sum of the Numbers are " << sum3 << endl;
return;
}
void calculators_Addition_5()
{
int a15;
int a25;
int a35;
int a45;
int a55;
int sum4;
cout << "Enter the First Number" << endl;
cin >> a15;
cout << "Enter the Second Number" << endl;
cin >> a25;
cout << "Enter the Third Number" << endl;
cin >> a35;
cout << "Enter the Fourth Number" << endl;
cin >> a45;
cout << "Enter the Fifth Number" << endl;
cin >> a55;
sum4 = a15 + a25 + a35 + a45 + a55;
cout << "The Sum of the Numbers are " << sum4 << endl;
return;
}
My Subtraction.
#include <iostream>
using namespace std;
void Calculators_Subtraction()
{
int subractify;
int subracta;
int differencea;
cout << "Type in the First Number!" << endl;
cin >> subractify;
cout << "Type in the Second Number!" << endl;
cin >> subracta;
differencea = subractify - subracta;
cout << "The Difference is " << differencea << endl;
return;
}
My Multiplication.
#include <iostream>
#include <string>
using namespace std;
void Calculators_Multiplication_2();
void Calculators_Multiplication_3();
void Calculators_Multiplication_4();
void Calculators_Multiplication_5();
void Calculators_Multiplication()
{
string multicipia;
cout << "How much numbers do you want?" << endl;
cin >> multicipia;
if (multicipia == "2" or "Two" or "two")
{
Calculators_Multiplication_2();
};
if (multicipia == "3" or "Three" or "three")
{
Calculators_Multiplication_3();
};
if (multicipia == "4" or "Four" or "four")
{
Calculators_Multiplication_4();
};
if (multicipia == "5" or "Five" or "five")
{
Calculators_Multiplication_5();
};
return;
}
void Calculators_Multiplication_2()
{
int multi2a;
int multi2b;
int product2;
cout << "Type in the First Number." << endl;
cin >> multi2a;
cout << "Type in the Second Number." << endl;
cin >> multi2b;
product2 = multi2a * multi2b;
cout << "The Product is " << product2 << "." << endl;
return;
}
void Calculators_Multiplication_3()
{
int multi3a;
int multi3b;
int multi3c;
int product3;
cout << "Enter the First Number!" << endl;
cin >> multi3a;
cout << "Enter the Second Number!" << endl;
cin >> multi3b;
cout << "Enter the Third Number!" << endl;
cin >> multi3c;
product3 = multi3a * multi3b * multi3c;
cout << "The Product is" << product3 << "." << endl;
return;
}
void Calculators_Multiplication_4()
{
int multi4a;
int multi4b;
int multi4c;
int multi4d;
int product4;
cout << "Enter the First Number!" << endl;
cin >> multi4a;
cout << "Enter the Second Number!" << endl;
cin >> multi4b;
cout << "Enter the Third Number!" << endl;
cin >> multi4c;
cout << "Enter the Fourth Number!" << endl;
cin >> multi4b;
product4 = multi4a * multi4b * multi4c * multi4d;
cout << "The Product of the Numbers are " << product4 << "!" << endl;
return;
}
void Calculators_Multiplication_5()
{
int multi5a;
int multi5b;
int multi5c;
int multi5d;
int multi5e;
int product5;
cout << "Enter the First Number!" << endl;
cin >> multi5a;
cout << "Enter the Second Number!" << endl;
cin >> multi5b;
cout << "Enter the Third Number!" << endl;
cin >> multi5c;
cout << "Enter the Fourth Number!" << endl;
cin >> multi5d;
cout << "Enter the Fifth Number!" << endl;
cin >> multi5e;
product5 = multi5a * multi5b * multi5c * multi5d * multi5e;
cout << "The Product of the Numbers are" << product5 << "!" << endl;
return;
}
My Division.
#include <iostream>
using namespace std;
void Calculators_Division()
{
float divisia;
float divisiab;
float quotient;
cout << "Enter the Divisor" << endl;
cin >> divisia;
cout << "Enter the Dividend" << endl;
cin >> divisiab;
quotient = divisia / divisiab;
cout << "The Quotient of the Numbers are " << quotient << endl;
return;
}
The Problem right now is that when addition finishes , subtraction starts. After Subtraction, Multiplication. After Multiplication, Division. After That the program ends. I'm sorry. Its just that I'm new to programming (Like one month).
There are many things to improve (some of them already mentioned in comments), but exact answer to your question is:
you have semicolon after your if-statement lines in Calculators_Operation() function body, which makes if-statements useless and your "operation" functions are being called every time (what you already noticed).
you use "or" operator in incorrect way, if you use (answera == "addition" or "+"), you basically say: ((answera == "addition) or "+") and that evaluates to true (see here)
EDIT:
Your function Calculators_Operation may be changed to:
void Calculators_Operation()
{
string answera;
cout << "What Operation do you Want?" << endl;
cin >> answera;
if (answera == "Addition" || answera == "addition" || answera == "+")
{
Calculators_Addition();
}
else if (answera == "Subtraction" || answera == "subtraction" || answera == "-")
{
Calculators_Subtraction();
}
else if (answera == "Multiplication" || answera == "multiplication" || answera == "*" || answera == "x" || answera == "X")
{
Calculators_Multiplication();
}
else if (answera == "Division" || answera == "division" || answera == "/")
{
Calculators_Division();
} else {
cout << "Unknown operation entered!" << endl;
}
}
NOTE: all your if statements need to be changed according to this function.
If anything does not work, please either put in comments what exactly does not work or edit your question with new code posting and question.

Why do my stream input operations get skipped over?

I have this code where in option lists will display when run. my problem is when I enter number 2, the option 2 program doesn't work well. It just go directly to asking the amount paid instead of asking first the cost of purchase.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
float circle (float a)
{
float z;
z = 3.141593 * (a * a);
return (z);
}
float square (float b)
{
float y;
y = b * b;
return (y);
}
float rectangle (float c, float d)
{
float x;
x = c * d;
return (x);
}
float triangle (float e, float f)
{
float w;
w = (e * f) / 2;
return (w);
}
void exit ()
{
cout << "THANK YOU! GOODBYE!" << endl;
}
int main()
{
int x;
do
{
cout << "Please choose an option below: \n";
cout << "1. Area of Shapes\n";
cout << "2. Cost of your items\n";
cout << "3. Flood Control\n";
cout << "4. Fibonacci Numbers\n";
cout << "5. Addition Table\n";
cout << "6. Exit\n";
cin >> x;
if (x == 1)
{
system("cls");
float n;
float l;
float m;
float radius;
float side;
float length;
float width;
float base;
float height;
do
{
cout << "1 => Area of Circle" << endl;
cout << "2 => Area of Square" << endl;
cout << "3 => Area of Rectangle" << endl;
cout << "4 => Area of Trian1gle" << endl;
cout << "5 => Return to Main Menu" << endl;
cout << "0 => Exit" << endl;
cout << "Please enter number of your choice: ";
cin >> n;
system("cls");
{
if (n == 0)
{
exit ();
system("pause");
return 0;
}
else if (n == 1)
{
cout << "Enter radius of the circle: ";
cin >> radius;
l = circle (radius);
cout << "Area of the circle is: " << l << endl;
system("pause");
system("cls");
}
else if (n == 2)
{
cout << "Enter side of the square: ";
cin >> side;
cout << "Area of the square is: " << square (side) << endl;
system("pause");
system("cls");
}
else if (n == 3)
{
cout << "Enter length of the rectangle: ";
cin >> length;
cout << "Enter width of the rectangle: ";
cin >> width;
m = rectangle (length, width);
cout << "Area of the rectangle is: " << m << endl;
system("pause");
system("cls");
}
else if (n == 4)
{
cout << "Enter base of the triangle: ";
cin >> base;
cout << "Enter height of the triangle: ";
cin >> height;
cout << "Area of the triangle is: " << triangle (base, height) << endl;
system("pause");
system("cls");
}
else if (n == 5)
{
exit ();
}
else
cout << "Invalid number. Please enter a valid number below" << endl;
}
}
while (n != 0 && n != 5);
cout << endl << endl;
system("pause");
system("cls");
}
else if (x == 2)
{
system("cls");
string mystr;
float cost = 0;
float amount = 0;
float total;
cout << "Total Cost: P";
getline (cin, mystr);
stringstream(mystr) >> cost;
cout << endl;
total = cost * .06;
cout << "Sales Tax Value: P" << total << endl;
cout << endl;
cout << "Cost of Item: P" << cost + total << endl;
cout << endl;
cout << "Amount Paid: P";
getline (cin, mystr);
stringstream(mystr) >> amount;
cout << endl;
cout << "Total Amount Purchased: P" << cost << endl;
cout << "Sales Tax Value: P" << total << endl;
cout << "Total Amount + Sales Tax: P" << cost + total << endl;
cout << "Total Amount Paid: P" << amount << endl;
cout << "Change: P" << amount - (cost + total) << endl;
system("pause");
cout << endl;
cout << "THANK YOU! ENJOY YOUR MEAL!" << endl;
system("pause");
system("cls");
}
else if (x > 6)
cout << "Invalid Input";
else
{
system("pause");
return 0;
}
}
while (x != 6);
system("pause");
return 0;
}
EDIT
For the posters education
You do
switch (n) {
case 1:
//... Code for n == 1 - If long put into another function. If using local variables put code bloc in braces
break;
case 2:
// Diitto for n==2
default: // No match
// All other values of n not listed above
}
What went wrong
Say you type your menu selection:
2<Enter>
Then the content of the std::cin stream will be:
2\n
When your menu selection runs...
cin >> x;
...it reads a number off the line but doesn't consume any trailing whitespace nor the newline, so the remaining state content could be denoted like this:
\n
Then your code for menu option 2 starts running:
cout << "Total Cost: P";
getline (cin, mystr);
...the getline looks at std::cin and finds the left over \n mentioned above, and says "hey, an empty line - I'll set mystr to an empty string". Notice that it did not do what you'd hoped: namely wait for you to type some more input and read that into mystr.
How to fix it
Before calling getline(cin, mystr) you want to remove the left-over \n typed when entering the menu selection. The code changes for that (adding error handling too):
#include <limits>
...
cout << "Total Cost: P";
std::cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
if (!std::getline(std::cin, mystr))
{
std::cerr << "unable to read mystr\n";
exit(1);
}
std::istringstream iss(mystr);
iss >> cost;
if (!iss)
{
std::cerr << "mystr doesn't contain a valid cost number\n";
exit(1);
}
How you could have found the problem
When you get stuck like this, try adding some "trace" statements to print out the values of variables and find where they differ from your expectation... that can at least give you a better idea how to isolate and describe the problem, and what to google for to fix it.
std::out << "mystr '" << mystr << "'\n";`
Try to use error handling like I've illustrated so the program stops (or prompts for better input) when there's a problem parsing the user's input.

c++ program quits when i input something or try to read something?

Any help would be greatly appreciated, my program quits as soon as i come out of the menu and try to enter something, been racking my brains trying to figure this out and is very annoying as i cant get anything else done until i fix this problem. i am a bit of a begginer at c++ so dont slate me if its a rookie mistake please haha!
This is the source code, its not yet a completed program just cant figure out whats wrong just now.
Thanks for any help!
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
struct cust
{
int employeeno, deptno;
char fname[10], sname[10], weekend[10];
float hours, othours, rate, otrate, normalpay, otpay, grosspay, netpay, totalni, totaltax, ni, tax;
};
int Menu(int& menuchoice);
void InputRecords(cust c[], int row, int menuchoice);
int Calculations(cust c[]);
int SearchNumber(cust c[], int &row);
int DeleteRecords();
int TotalPay();
int main()
{
struct cust c[100];
int menuchoice, row;
Menu(menuchoice);
if (menuchoice == 1){
system("CLS");
InputRecords(c, row, menuchoice);
}
if (menuchoice == 2){
system("CLS");
SearchNumber(c, row);
}
if (menuchoice == 3){
system("CLS");
DeleteRecords();
}
if (menuchoice == 4){
system("CLS");
}
if (menuchoice == 5){
system("CLS");
exit(5);
}
//Calculations(cust c[]);
}
int Menu(int& menuchoice){
cout << " \n\n\n\n\n 1. Input a Payslip" << endl << endl;;
cout << " 2. Read a Payslip " << endl << endl;
cout << " 3. " << endl << endl;
cout << " 4. " << endl << endl;
cout << " 5. Quit the Program" << endl << endl;
cin >> menuchoice;
}
void InputRecords(cust c[], int row, int menuchoice){
char another;
do{
cout << "Please Enter Their Employee Number: " << endl;
cin >> c[row].employeeno;
cout << "Please Enter Their First Name: " << endl;
cin >> c[row].fname,9;
cout << "Please Enter Their Second Name: " << endl;
cin >> c[row].sname,9;
cout << "Please Enter Their Department Number 1 - 9: " << endl;
cin >> c[row].deptno;
cout << "Please Enter The Hours They Have Worked: " << endl;
cin >> c[row].hours;
if (c[row].hours >= 37.5){
cout << "Please Enter Any Overtime They Have Worked: " << endl;
cin >> c[row].othours;
}
cout << "Please Enter Their Rate of Pay: " << endl;
cin >> c[row].rate;
cout << "Please Enter The Date of the Week End (DD/MM/YYYY): " << endl;
cin >> c[row].weekend, 9;
row++;
cout << endl;
//Putting it in the file.
ofstream timesheetFile("Timesheet.txt", ios::app);
if(timesheetFile.is_open()){
cout << "File has been opened." << endl;
timesheetFile << c[row].employeeno << " " << c[row].fname << " " << c[row].sname << " " << c[row].deptno << " " << c[row].hours << " " << c[row].othours << " " << c[row].rate << " " << c[row].weekend << "\n" << endl;
timesheetFile.close();
}else{
cout << "Error! File is not open." << endl;
}
cout << "Would you like to enter another record? Y/N : ";
cin >> another;
cout << endl << endl;
}while(row<100 && another == 'y');
system("CLS");
main();
}
//read records
int SearchNumber(cust c[], int &row){
//system("CLS");
int empno;
cout << "Enter Employee Number : ";
cin >> empno;
for (int i=0; i < row; i++)
{
if (empno == c[i].employeeno){
system("CLS");
cout << c[i].employeeno << endl << c[i].fname << c[i].sname << endl;
}
}
}
//deleterecords
int DeleteRecords(){
}
//calculations
int Calculations(float normalpay, float& hours, float& rate, float otpay, float otrate, float& othours, float grosspay, float tax, float ni, float netpay, float totalni, float totaltax){
ni = 6.8 / 100;
tax = 12.75 / 100;
otrate = 1.5 * rate;
normalpay = hours * rate ;
otpay = otrate * othours;
grosspay = normalpay + otpay;
totalni = grosspay * ni;
totaltax = tax * grosspay;
netpay = normalpay + otpay - totaltax - totalni;
// cout << totaltax << endl;
//
// cout << totalni << endl;
//
// cout << netpay << endl;
}
int TotalPay(){
}
The problem is here
int main()
{
struct cust c[100];
int menuchoice, row;
Menu(menuchoice);
if (menuchoice == 1){
system("CLS");
InputRecords(c, row, menuchoice);
}
You have not given the variable row a value but you use row when you call InputRecords.
From a look at your code it seems to me that the row variable should be moved to the InputRecords function and initalised to zero there. I can't see why you have the row variable in the main function.
Also I can't see why you pass menuchoice to InputRecords, it doesn't get used there. It all seems a bit random, maybe you should review functions and parameter passing.
Looks like your row variable is never being initialized. Why is this?
It's also good practice to initialize your variables like menuchoice
int Menu(int& menuchoice);
void InputRecords(cust c[], int row, int menuchoice);// declared
int Calculations(cust c[]);
int SearchNumber(cust c[], int &row);
int DeleteRecords();
int TotalPay();
int main()
{
struct cust c[100];
int menuchoice, row; // declared again but never initialized
Menu(menuchoice);
if (menuchoice == 1){
system("CLS");
InputRecords(c, row, menuchoice); // used