C++ :: Overloading Addition And Comparing Expressions In A While Loop

Apr 28, 2015

I have two questions that are related to each other. The first one is about overloading the addition operator.

I have defined a struct as the following:

Code:
#include <iostream>
#include <string>
struct Sales_data {

[Code] ....

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.

Code:
// addition operator rules
Sales_data Sales_data::operator + (const Sales_data & data2) {
units_sold += data2.units_sold;
revenue += data2.revenue;
return *this;
}

Now here is the code in its entirety

Code:
#include <iostream>
#include <string>
// Sales_data structure
struct Sales_data {
std:: string bookNo;
unsigned units_sold = 0;

[code] .....

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.

View 3 Replies


ADVERTISEMENT

C++ :: Operator Overloading - Comparing Objects (Fractions)

Jul 25, 2013

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

View 7 Replies View Related

C/C++ :: Making Addition Loop?

Nov 19, 2014

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;

[code]....

View 3 Replies View Related

C/C++ :: Loop Not Comparing Values Correctly?

May 12, 2014

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

[Code] ....

View 6 Replies View Related

C/C++ :: Comparing Pointer To Variable As While Loop Condition

Aug 11, 2014

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.

View 2 Replies View Related

C :: Cannot Use Array Name In Expressions

Nov 27, 2014

Code:

int main() {
char *arr[] = {"Hello", "World", "Good", "Morning"};
display(arr);
return;
}

[code]....

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

View 2 Replies View Related

C++ :: Regular Expressions Not Working?

Aug 25, 2014

I have this bit of code that I found here: [URL] and at first regex would work, but then when I hit "[]" it wouldn't. Here's the code:

#include <iostream>
#include <string>
#include <regex>

[Code]....

View 2 Replies View Related

C# :: Cannot Convert Lambda Expressions

Mar 31, 2015

I get the following error:

Cannot convert lambda expression to type system delegate because it's not a delegate type (on invoke).

The Second error is: Client.PrivateChat.txtReceive is inaccessible due to its protection level..

private PrivateChat pChat;
private void client_Received(Client sender, byte[] data) {
this.Invoke(() =>
{
for (int i = 0; i < clientList.Items.Count; i++) {
var client = clientList.Items[i].Tag as Client;
if (client == null || client.Ip != sender.Ip) continue;

[Code] .....

View 4 Replies View Related

C++ :: Not Getting Right Value For Addition

Jan 17, 2013

Code:

#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..

View 4 Replies View Related

C++ :: Use Boost / Regex For Regular Expressions?

Sep 5, 2013

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.

View 2 Replies View Related

C++ :: Email Validation Without Using Regular Expressions?

Dec 11, 2013

writing a program for "Validating the e-mail id without using regular expressions in c/c++"?

View 4 Replies View Related

C++ :: Evaluating Logical Boolean Expressions

Jan 10, 2013

I am looking for a library to aid me in evaluating Boolean expressions. For example, i have an expression like this:

(1*(5+3)+9*65/5-(354*4565*5643+98) >= 12345) && ( 654*987+123 || (2345 > 23423 && 1 != 2)))

(It can also be much longer!) and would like to evaluate them to a true/false boolean.

There are tons of libraries to calculate the (numerical) result of a mathematical expression, but this is not what i want to do.

View 5 Replies View Related

Visual C++ :: Regular Expressions - How To Get Iterator

May 22, 2014

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.

View 3 Replies View Related

C :: Shunting-yard Algorithm To Evaluate Expressions?

Sep 16, 2014

Can I use Shunting-yard algorithm to evaluate Expressions (arithmetic, relational, and logical) ?

View 8 Replies View Related

C++ :: Implement Program For Evaluating Infix Expressions

Dec 3, 2014

Also, can't use namespace std for this.

#include<iostream>
#include<stack>
#include<fstream>
#include<iomanip>
#include<queue>
#include<cassert>

[Code] ....

/* 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();

[Code] ....

View 8 Replies View Related

C++ :: Mixed Expressions / Loops And Formatted Output

Feb 22, 2015

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?

View 2 Replies View Related

C# :: Join Tables In LINQ With Lambda Expressions

Dec 5, 2014

i have stuck in a join and i cant figure out where the problem is, i have those tables

public class Themes
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }

[Code].....

View 12 Replies View Related

C++ :: Convert To Hexadecimal And Hex Addition

Nov 20, 2013

#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>

[Code]...

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.

View 3 Replies View Related

C/C++ :: Addition Of A Number To A Pointer

Jan 25, 2015

#include<stdio.h>
#include<conio.h>
void main()

[Code]....

Why it gives output = 6. when i use j=j-9; then it gives output = 65506.

View 3 Replies View Related

C# :: Addition Arrays In New Array

Jun 7, 2014

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 {

[Code] .....

View 1 Replies View Related

C++ :: Evaluating Arithmetic Expressions Using Recursive Descent Parser?

Feb 25, 2013

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 .

View 3 Replies View Related

C++ :: Vector Addition With Magnitude And Direction

Sep 29, 2013

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;

[code].....

View 4 Replies View Related

C/C++ :: Matrix Class - Addition / Subtraction

Jul 7, 2014

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.

View 14 Replies View Related

C/C++ :: Addition Of Infinite Numbers Using String

Feb 26, 2013

How exactly did u accept it as a string? i mean did u use ascii values or anything? and what was your code for keeping track of carry?

View 1 Replies View Related

C++ :: Calculate Complex Numbers Through Multiplication And Addition

Feb 28, 2013

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.

[Code] ......

View 4 Replies View Related

C :: Addition And Subtraction Calculator That Takes Input?

Feb 23, 2013

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;

[Code]....

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved