I then overloaded the I/O operators so I could print to the screen information related Sales_data.
Code:
// overload ostream in order for cout to work
std::ostream& operator << (std::ostream & out,
const Sales_data & cSales_data) {
out << cSales_data.bookNo << ", " << cSales_data.units_sold << ", "
[Code] ....
My first issue is with overloading the addition operator. Everyone works correctly except for std::cout << item << std:endl; will no not output the ISBN number only the units_sold and revenue when added together.
After total = total + item;, I would like to print the total for this particular ISBN. However, I only get: blank, total units, total revenue where blank is where the ISBN would go but doesn't print after addition. My second question has to do with comparing the ISBN's of the books entered during the while loop. I would like to do something like
Code:
if (item_i.bookNo == item_i+1.bookNo) {
total = total + item;
} else {
std::cerr << "Books entered must have the same ISBNs" << std::endl;
}
Unfortunately, I cannot figure out how to set up a comparison of the bookNos. If I used #include <casset> in the overload + rule, it will immediately exit since I have no way to compare the ISBNs.
I'm trying to compare 2 fractions using operator overloading. The program crashes when this is called;
this is definition: bool operator == (const Fraction& f1, Fraction& f2) { if (f1==f2)return true; else return false; }
this is my calling in the main: Fraction f1, f2; cout<<"Enter in the format: 2/4 or 4/9 "; cout << "enter first fraction: "; f1.Input(); cout << "enter second fraction: "; f2.Input();
Fraction result: result = (f1 == f2);//i think problem lies here. result.Show();
and this is the prototype of the operator: friend bool operator == (const Fraction& f1, Fraction& f2);
What my ultimate goal here is to make a program that asks the user how many numbers they would like to add followed by asking the user what numbers they want to add. I want the amount of numbers they can add to be infinite and have the loop continue adding the numbers until it reaches the final number. I just need to know how to do this.
Here's my code:
#include <iostream> #include <string> using namespace std; int main() { //variables int amount; float num; float sum;
So, I have the beginnings of a rock paper scissors game which i have created before in Java, and i am attempting to create it in c++.
Problem is, the function "int checkConvertInput" containing a loop to make sure input is valid, is not exiting the loop. As far as i can see the two strings are not comparing. Is there a library function for this? I know C had strcmp but i am not sure if it applies here.
#include <iostream> #include <string> #include <vector> using namespace std; int checkConvertInput(int playerSign, string signs[]);
I'm writing a delete function for a linked list, and I'm having issues with this bit of code:
void deleteNode(int data){ node* del = NULL; t = h; n = h; while(n != NULL && n->_data != data){ t = n; n = n->next; } }
Or more precisely, this portion:
&& n->_data != data
n is my new node variable, _data is the storage variable in the private section of my class, and data is the information being searched for that needs to be deleted. Everything works without this section of the code. My assumption is that n->_data is somehow wrong, but I don't see how. I've tried everything I can think of- using parenthesis, using the variable rather than the pointer, I've tried expressing the pointer in a different way, I've tried using my t variable rather than n, and I've found examples online that use this exact same expression without any issues.
The code works fine and prints the 4 strings. Where i m riddled is whether "ptr" in display function is a pointer or a string ? If it's a pointer then what is the type of the pointer? If it's an array, then as per my understanding, we cannot use an array name in expressions such as ptr++ (K&R).
#include<iostream.h> #include<conio.h> class vector { float x_comp1,y_comp1,z_comp1,x_comp2,y_comp2,z_comp2; float add_x,add_y,add_z;
[code]....
Now the problem is that when i run the program, i get correct value of the two vectors, but i am not getting the right value for the addition...it might be the x_comp1 and so on are not getting those values even i am assigning them..
So I need to use boost/regex for regular expressions. someone told me that it needs to be built. the first problem is boost doesn't tell you how to build it and the second is i did sudo apt-get install libboost something. I don't remember the exact name of the package. it installed but i dont know how i would build it when its installed.
In order to parse mathematical expressions I am trying regular expressions and a recursive algorithm, but I have a problem with the four basic operations: +, -, *, /.
Trying to analyze a string like "a+(b+c)", if I use the pattern for a sum "(.+)+(.+)" the program matches it recognizing as subpatterns: "a+(b" and "c". How could I achieve the program to try also the other possibility?
I think that it would be great something like an regex_iterator which worked with regex_match instead of regex_search. I mean, an iterator that iterates over all the possible ways to match a given regular expression and a given string. This way I could loop through all these possibilities until the two subpatterns produced were correct mathematical expressions.
/* It will read in a infix expression from a text file.check if the parentheses in the input expression are balanced.convert the infix expression into a postfix expression and evaluate the expression.*/
int main() { string expression; string postfixExpression; double result; testBalanced();
1) ask the user to input a mathematical expression in the following format:
NUMBER Operator NUMBER Operator NUMBER Example: 17 + 15 - 3 Example: 2 * 3 - 4
How to output the answer of the users equation. Is there a function that includes all math operators (+,-,/,*)? Would i need to write each possible scenario using if statements?
1)What should be the best variable for adding two 6-digit hexadecimal,such as 0034AD,0057EA? I would like to use array of character but it seems hard to handle.
I need addition array1 array2 in new array before I make this program but the values of array1 and array2 there are in code c# that is worked without problem.
But now i need the user enter the values of arrays, i make that but i failed in addition array1 and array2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MyArray {
I have the following code to calculate arithmetic expressions :
#include <iostream> using namespace std; using namespace std; const char * expressionToParse = "6.5-2.5*10/5+2*5"; char peek(){ return *expressionToParse;
[code]....
The problem is that it does not work properly with decimal numbers for example it evaluates 6-2*10/5+2*5 = 12 which is correct but for 6.5-2.5*10/5+2*5 it returns 6 instead of 11.5 .
i new to c++ programming. i have a program due soon in vector addition. i am to design should be able to add vectors with certain magnitude and direction and give its resultant magnitude and direction (i know how to do this mathematically but programming is not working. the user should be able to select how many vector he/she wants (i don't know how to do this, so i added 3 vectors). here's my work - i did this purely algebraically. it's still incomplete but it compiles. my "if else" statement also doesn't respond correctly.
#include <iostream> #include <string> #include <vector> #define PI 3.14159 #include <math.h> std::vector<int> v; // declares a vector of integers using namespace std;
I'm working on a matrix class and I've hit a few snags. Here's what I've got so far:
Matrix.h
#include <iostream> #include <conio.h> using namespace std; class Matrix { private: int matrix[50][50];
[Code] ....
Where I have questions is in implementing the addition and subtraction bits into the class itself. I understand that I'm going to have to do a copy operation (this from reading in C++ Primer Plus 5th Edition). Right now I'm just after adding/subtracting as the rest will be variations on the same theme.
So, here it goes:
As I understand the problem, I need to pass my two matrices as arguments to an addition function. This is going to involve having to copy the values of the two existing matrices into temporary matrices within the addition function, then I'll add them, and store them in a new matrix which will be my return value. So...something like this:
int Matrix::matrixAdd(int R, int C, const Matrix & matrix1, const Matrix & matrix2) { int sum; Matrix matrix; for (int i = 0; i < R; i++)
[Code] ....
I do end up with errors there...C2240, and C2662. Again, I'm new to working through this, but that's what I've got. My idea is that I'm passing the maximum size of the array as defined by the user, in this case a 2x2 array, it'll cycle through and add up to that imposed limit...I went 2x2 because it's small enough that testing doesn't drive me up a wall.
Below is a code that is used to calculate complex numbers (a+bi, where i = sqroot (-1)) through multiplication and addition.
However, on my output file, no Header is being printed; the only thing that is being printed is "8 + 7i + = "
"complex.h" is included at the end of the code.
Code: // Trey Brumley// CMPS // Dr. Tina Johnson // March 1, 2013 // Program 2: Classes // This program will demonstrate the use of classes by using a custom "complex-number" (a+bi) class.
I'm writing an addition and subtraction calculator that takes input as: 5+6-3+2. You should be able to add or sub as many numbers as you want. I want the while loop to stop when the user hits enter. I put the getchar() function to catch the and break the loop but it is also swallowing the '-' sign, which I want to use to subtract and is instead adding the numbers with "sum+=number". How can I get around that?
Code:
#include <stdio.h> int main(int argc, char *argv[]){ int number, sum = 0;