C++ :: Overloading Using IStream - Not Outputting Objects

Mar 6, 2015

I'm implementing istream and ostream overloading for a program that takes in 3 objects, adds them, subtracts them, and multiplies them. I'm having difficulty in outputting the objects. The program outputs the first object just fine, but for some reason defaults the other two...

Code:

#ifndef COMPLEX_H
#define COMPLEX_H
#include <string>
Class Complex

[Code] ....

View 1 Replies


ADVERTISEMENT

C++ :: Operator Overloading IStream

Jan 26, 2015

I am working on this assignment...

Code:
#include <iostream>#include <iomanip>
using namespace std;
class Score
{
private:
// Value at which we'll shift digits from million_counter to billion_counter
static const int THRESHOLD = 1000000000;

[Code] ....

It gives the errors:
line 105 error: million_counter was not declared in this scope
line 106 error: normalizeScore was not declared in this scope
line 110 error: million_counter was not declared in this scope
and more of that until
line 170 error: no match for 'operator<<' in 'std:perator<< <std::char_traits<char> >((* & std::cout), ((const char*)"a+b is ")) <<operator+((*c(const Score*) (& a)), (*(const Score*)(& b)))'

I thought that because i declared friend functions, they would be able to access the private variables of the class.

View 2 Replies View Related

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++ :: Polynomial Objects - Deep Copy And Operator Overloading

Feb 22, 2015

In main I instantiate two Polynomial objects -- p1 and p2:

int main() {
const int SIZE = 3;
Polynomial *p1 = new Polynomial(SIZE);
Polynomial *p2 = new Polynomial(SIZE);

//Read data into p1
std::cout << "Initialize Polynomial Coefficients (1)" << std::endl;

[Code] .....

The implementation file for Polynomial is as follows:

Polynomial::~Polynomial() {
delete [] this->m_Ptr;
this->m_Ptr = NULL;
} Polynomial::Polynomial(int size) {

[Code] .....

What works: Adding two pointers. The output is correctly produced.

The problem in particular occurs in main when p1 and p2 are attempted to be multiplied. The code attempts to release the memory upon multiplication but I receive a run-time error.

The output for the difference of the two polynomial objects is incorrect. It is displaying addresses.

View 3 Replies View Related

C++ :: Overloading Input Operator For Rational Type Objects To Work

Nov 13, 2014

I cannot get my function overloading the input operator for rational type objects to work.

lab9.cpp: In function ‘std::istream& operator>>(std::istream&, const rational&)’:
lab9.cpp:186:20: error: invalid initialization of reference of type ‘std::istream& {aka std::basic_istream<char>&}’ from expression of type ‘rational’
return (inputFrac);

lab9.h

#include <iostream>
using namespace std;
class rational {
public:
rational();
rational(int a, int b);

[Code] ......

View 4 Replies View Related

C++ :: String Outputting Twice?

Nov 3, 2014

When I run the program, the prompt I am giving the user before I collect their data, shows up twice in row. Here's the part of the code I'm referring to:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
// method prototypes
void characterInstructions();
void phraseInstructions(); //

[Code] ....

View 6 Replies View Related

C++ :: Outputting Associated Data In Struct

May 2, 2013

So this is the last part of a program I've been working on for four weeks now. This question may be a tough one considering the amount of files included in the program.

The program is to read in a file of requests between two cities, read in a file of flights and cities that occur between the cities. It then checks to see if there is a path between the flights and output an itinerary. I have the correct itinerary outputting, but when attempting to output the associated flight number and price according to the city, I am getting odd data. How can I output the correct flight number and price associated with each flight on the itinerary.

I'll post out the output I am currently getting and the section where I am outputting the data. I'm sure I'll need to post more files so the program can be understood.

Don't want the code done for me, just a point in the right direction! I don't want to let this program defeat me!

Output: Code: Request is to fly from Atlanta to San-Diego.The flight itinerary is:
Flight # From To Cost
10 Atlanta Chicago $134529069
10 Chicago Miami $134529069
10 Miami Dallas $134529069
10 Dallas San-Francisco $134529069
10 San-Francisco San-Diego $134529069
This function finds a path between cities. Code: bool flightMap::IsPath(string originCity, string destinationCity){
StackClass aStack, bStack;
flightStruct flightRec;
string topCity, nextCity;
bool success;
int index = 0;

[Code].....

View 1 Replies View Related

C++ :: How To Pause After Outputting Results

Feb 19, 2013

I mean on the executable file. It just displays the results and quickly flashes away, cin.get() has no effect and system("PAUSE") is undeclared.

I never found a single sure way to pause effectively. Is there a method that works all the time? Sometimes cin.get() gets skipped even in the code itself. The IDE I am using is Code Blocks if that matters any.

View 4 Replies View Related

C++ ::  NCurses Outputting Vectors

May 19, 2013

The following code works perfectly with "normal" C++

#include <iostream>
#include <vector>
int main() {
std::vector <std::string> hello {

[Code] ....

This prints "HELLO" on the screen. A function a bit more like NCurses is:

for (std::vector <std::string>::const_iterator it = hello.begin(); it != hello.end(); it++) {
std::string temp = *it;
std::cout << temp.c_str() << std::endl;
}

This still works with "normal" C++. Now, how do I get this working with NCurses? I tried

#include <curses.h>
#include <string>
#include <vector>
int main() {
initscr();

[Code] ....

But this just gives me an empty screen.

View 2 Replies View Related

C++ :: Outputting Randomized Arrays

Apr 7, 2013

For an assignment I am having a user enter the name of a file that contains "Song Data" the file is set up like this:

(example)

1012 C
3333 R
90 E

And so forth for 15 lines. The first value represents the song Id and the second is a character C, D, E, or R that represents the type of song. After the file is opened I have two parallel arrays that are read from the file:

1. int songId[15]
2. char typeOfSong[15]

I am able to read the entire file, which symbolizes playing the playlist in order, but now I have to play a randomized playlist.

It has been given to us that we need to set up two parallel arrays, that are also parallel to the first two (songId and typeOfSong). These must also be one dimensional. The first array will will be indexes into the songId array telling is what order to put the songs in and the second is an array of true/false values telling us if the song has already been chosen or not.

I have that:

void playRandomPlaylist (int songId[], char typeOfSong[]) {

// the size of the file and therefore arrays is known
rand() % songId[15]

// choosen and notChoosen symbolizing true and false values
typedef enum {choosen, notChoosen};
}

View 1 Replies View Related

C++ :: Getting Data From A File And Outputting

Apr 17, 2013

I am given a data file that contains sports data for 24 people. There are two rounds of the event. The first 48 lines of the file give the data for the 1st round and the second 48 lines give data for the 2nd round. The file looks like this:

Azrin, Neil
2.3 6.0 5.0 6.7 7.8 5.6 8.9 7.6
Babbage, Charles
2.3 5.6 6.5 7.6 8.7 7.8 5.4 4.5
Burks, Arthur
2.8 4.5 5.4 6.6 7.7 8.8 5.6 6.5
Crick, Francis
1.6 6.5 6.5 6.5 7.3 7.5 7.8 5.6
and so on ............

This data continues for all 24 people and then repeats with different score values for the second round. The first number after the persons name is the level of difficulty and then next 7 numbers are the scores. I am trying to get this data out of the file and set it up in the following format:

Name Of Person
Round 1: level of difficulty scores
Round 2: level of difficulty scores

So the table for the above data would look something like this:
Arzin, Neil
Round 1: 2.3 6.0 5.0 6.7 7.8 5.6 8.9 7.6
Round 2: 2.1 6.0 5.0 6.5 7.8 5.6 8.8 7.6
Babbage, Charles
Round 1: 2.3 5.6 6.5 7.6 8.7 7.8 5.4 4.5
Round 2: 2.3 5.4 6.5 7.6 8.7 7.2 5.4 5.5

And so on for all 24 people. This is the code I have for it, but it just reads the file directly:

// I have already opened the file!
while (inFile) {
string name;
getline(inFile,name);
cout << name << endl;
}

How I can do some sort of formatting on this data?

View 1 Replies View Related

C++ :: Comparing Value To Array And Outputting

Feb 6, 2013

i have 3 arrays total, 2 of them i am comparing to see if any of the values match at all, and then i am putting the value that matches into the 3rd array. currently i am trying

int isthere (int match[], int maxmatchLength,
const int vec1[], int vec1Length,
const int vec2[], int vec2Length)
{
for (i=0; i<vec1Length; i++)
if vec1[i]==vec2[i];
vec1[i] = match[i];
}

But this will just match the same values are in the same spot. how do i fix it so that it compares one value in the first array to the whole second array, before going to the next number.

View 1 Replies View Related

C/C++ :: Getting Error When Outputting To File

Sep 5, 2014

it isn't actually writing anything to the file. It is opening the employeesOut.txt file but not writing anything. I'm getting the error

libc++abi.dylib: terminating with uncaught exception of type std::invalid_argument: stoi: no conversion
(lldb)

I believe the error is within my main.cpp, so here that is, I have multiple classes.

#include <iostream>
#include <string>
#include <fstream>
#include "Employee.h"
using namespace std;
bool openFileForReading(ifstream& fin, const string& filename);

[code]....

View 12 Replies View Related

C++ :: Sudden Exit Right After Outputting Expression

May 1, 2014

This program I'm working on just exits unexpectedly right after outputting "Expression?"

int main(void) {
provideHelpIfNecessary();
while(true){
cout << "Expression? ";

[Code] ....

And I'm not allowed to change anything in main().

View 2 Replies View Related

C++ :: Zlib Outputting Blank File?

Oct 30, 2014

I wrote a program to compress a file with gzip, which works fine. I also wrote a program to decompress the gzip file, which runs but outputs a blank file.

Code:

main.cpp:
#include "decompress.h"
int main(int argc, const char * argv[]) {
if(argc == 3) {
decompressor(argv[1], argv[2]);

[code].....

View 4 Replies View Related

C++ :: Outputting First Word Of Each Sentence In String

Dec 20, 2014

I have been trying to get this to work for a while now - with no success.

Basically I am trying to write a function which the returns the first word of each input sentence in a single string - this is part of a larger cryptography program I am working on.

So for example, if this string was passed into the function:

"This is what I mean. Is it right? A poor puppy abandoned. Secret torturing of dogs are happening. Message: be on the watch."

It should return:

//declared in class "steganalyse"
string cyphertext;
string punctuation = ".?!;:'";
book is_first_word

[Code] .....

But this only returns the first word:

This

Any other way to return the first word of each sentence in a string.

View 1 Replies View Related

C++ ::  Program To Grade T Or F Test Not Outputting Information

Jul 2, 2014

I wrote a program to grade T or F test. It is running, but not outputting the information.

Code:

#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
using namespace std;
double grading(char*answer, char* stuResponse, double graded);

[Code] ...

text doc:

TFFTFFTTTTFFTFTFTFTT
ABC5403 TFTFTFTT TFTFTFFTTFT
ABC5404 TFTFFTTFFTFFFTTTFTFT

View 5 Replies View Related

C++ :: Outputting Contents Of Stack On Input Line?

Sep 3, 2013

outputting the contents of my stack on the input line.

View 7 Replies View Related

C/C++ :: Vector Of Class Outputting Garbage Values

Jun 21, 2014

I have a class that uses std::vector. I push back class objects onto the vector but when I try to cout the data members (with get functions) I get garbage values.

Here is the class:

#ifndef JOBS_H
#define JOBS_H
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
using namespace std;
class Jobs{

[Code] ....

I know I am missing the implementation of several functions but I'm just testing my vector to see if it is working and it isn't. The getBlockValue() function should output 0 for each job. When I push back one object the output is 0. When I push back 2 objects the output is 0. However when I push back 3 objects I get random values. Why is this? I thought vectors were dynamic?

View 8 Replies View Related

C++ :: Outputting Transactions From A Text File To Screen

Jan 19, 2014

I am working on a wage application with four options which are as follows:

Subtracts an amount from the shop bank account (The account is text file "shop"). You do not need to add the same amount to another account but you should record the transaction with a timestamp in a separate file. The application should prevent the account balance from becoming overdrawn.

List the five most recent transactions to the screen. If there haven’t yet been five transactions then list all of them.

Print the account name, number and current balance to the screen.

Exit the program.

I have completed 1,3 and 4 but I am completely stumped as to how to go about number 2.

#include <limits>
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <ctime>
#include <string>
int read_balance(void);
void write_balance(int balance);

[Code] .....

View 1 Replies View Related

C++ :: Cout Statement Not Working - Program Outputting Zero For Variable

May 20, 2013

Why is my program outputting zero for the variable pop?

Code:
#include <iostream>
int main(void){
using namespace std;

//capture data
cout << "Enter the world's population: ";

[Code] ....

It's obviously not std::cout, but I'm thinking its the way I'm operating on long?

View 5 Replies View Related

C++ :: How To Stop Outputting Data Based On A User Input

Sep 27, 2013

how to stop outputting data based on a user input. The text file is as follows:

1. a
2. b
3. c

and the code I'm using is as follows:

int main (){
string line;
int search;
cout << "Enter a number from 1-3" << endl;
cin >> search;
search++;
ifstream myfile ("example.txt");

[Code]...

What I want to do is to just output number 1 (the whole line) if the user enters number 1. However, I get an error on the second condition w/c is the "&& line!= search"

View 1 Replies View Related

C++ :: Outputting Partially Filled Array Onto Text File?

Jul 24, 2014

I have a partially filled array and I want to put that into a text file. Say I don't know how partially filled it is. How would I stop at the very end of the array where it has the last amount of data.

ex.
int array[9] = {1,2,5,6};
ofstream output("txt file");
for(int i = 0;i < 9; i++){
output << array[i]
}

How would I stop as soon as i becomes 4 because that is when the array stops?

View 1 Replies View Related

Visual C++ :: Reading And Outputting Files - Sentences And Abbreviations

Mar 26, 2013

I have got an assignment. It is about comparing two files one file has sentences and the other abbreviations. so far i have managed to compare the abbreviations which appear in the sentence and erase punctuation linked to abbreviations.

However I am currently stuck on putting the punctuation back in when outputting the file. I am also stuck with replacing matching words in the sentences with the expanded abbreviations. Not sure which code to show. but sentences and abbreviations are below.

ABBREVIATIONS

Aberd Aberdeen
admin administration
approx approximate
Austral Australia
div division
Capt Captain
Comdr Commander
e east
HQ Head Quarters
m metres
mil million
mt Mount
n north
NATO North Atlantic Treaty Organisation
NSW New South Wales
pop population
s south
sq square
w west

Text file

NATO troops were on exercise in Aberd Capt Jones of the first div told Comdr Frank that his troops were near the E flank of the NATO forces. Complaining about the amount of Admin, Capt Jones radioed the NATO HQ asked for navigation relative to the HQ. The Captain then left.

For example the abbreviation "Aberd" in the sentence above needs to be replaced with Aberdeen from the abbreviations file. I need the last part of the abbreviation not "Aberd Aberdeen" need just "Aberdeen". Hope the info is understandable.

View 6 Replies View Related

Visual C++ :: Control Structure Outputting Wrong Iterations Via Ints?

Mar 18, 2013

I have this:

Code:
for (chr_min = 0; chr_min <= 24; chr_min += 3) {
strOut = "";
for (ctr_row = 1; ctr_row <=3; ctr_row++) {
switch (ctr_row) {
case 1:
strOut = strOut + Line1.substr(chr_min, 3);

[Code] ....

it is outputting to the shell window, this:

Code:
1
1
1
2
2
3

and that pattern apparently repeats for every iteration that's specified in the outer loop. am I missing something here?

View 14 Replies View Related

C/C++ :: Objects Hold References To Other Objects?

Nov 12, 2014

This has been bothering me for a while now, and I finally put together an example:

#include <iostream>
#include <string>
using namespace::std;

[Code]....

In the code above, the two classes hold pointers to each other, and that's fine but it doesn't seem right since C++ prefers to pass by reference. Yes, it can still do that (see testbox and testball) but even that seems odd to me because still you need to use pointer notation for the enclosed object. Am I the only one who feels this way, and should I just get over it? Or am I missing something that would allow an object to hold a reference?

View 4 Replies View Related







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