I'm posting from my phone and the details are a little iffy but ...
If you right-click on your project folder
Properties
Java build-Path
The 2nd tab .. There should be options to add internal and external .jar files.
I'm posting from my phone and the details are a little iffy but ...
If you right-click on your project folder
Properties
Java build-Path
The 2nd tab .. There should be options to add internal and external .jar files.
char name;
char family;
int age;
Could it be that your name is only 1 character long. If you want a full name, consider the use of a string. Same goes for the family.
That is a lot of if-else statements. Think you can rewrite this with a more logical approach?
Note: This is not to bash you! If you can think of creating this with another approach, it may help with other types of OOP. Consider using a class for each player as either X or O, and using enum. ::hint hint:: :D
2nd Note: Oh, sorry. This was posted ~2 weeks ago. Likely should not have replied to this thread by now.
for loop is very likely your best option. Can you show us any code that you have written? Even if you think the code is wrong, please link it. At the very least, consider writing up a pseduocode to see if we agree with the logical approach.
Writing pseduocode (writing in layman's terms) helps no matter what programming language you are writing in. Once you have that written down, the syntax tends to slide in nicely.
Use the new operator to create either a Ship, CruiseShip or FreightShip object, storing the pointer to the object in the next element of the array."
When making a new object, collect the data that you need and make a new object. Like this.
Soda *newSoda = new Soda("Coca Cola", 20, 1.49);
This states that a type Soda, named newSoda, will be newly created (on the heap) with the parameters of "Coca Cola", 20, 1.49. When adding this to a dynamic array, just reference the address with the * symbol.
As such
Soda *ArrayOfSoda; // declares a pointer of type Soda
ArrayOfSoda = new Soda[15]; // initialized the pointer to a dynamic array of type Soda
int NumOfSodaInArray = 0; // Current size of the array (not the capacity of the array)
// add a Soda to the array
ArrayOfSoda[NumOfSodaInArray++] = *newSoda;
// This grabs the address of the newSoda that was previous made
// Adds the address to the pointer of type Soda
// Increments the NumOfSodaInArray after the pointer was added to the array.
// NumOfSodaInArray is now equal to 1
If you still need help with this, please actually ask a question about it. I was left with a sense of .. huh? And attempted to find a question in the lines of code.
Also, repost the snippet of code that you are having the problem with and what problem you are having with it.
You first want to fill your array. Then send that array to both min and max functions. The array may be filled in the main fucntion, then sent to each of the functions to find the maximum and minimum values in the array.
You can fill an array like this.
int newArray[] = {1,2,3,4,5};
int sizeOfArray = 5;
Which will create a new integer array named, newArray, with 5 integers that can be indexed from newArray[0] through newArray[4].
Once the array has been created, you send the array to each -calling- functions as such.
findMaximumValueinArray(newArray, sizeOfArray); // whereas sizeOfArray is the actual size of the array.
whereas the -declared- and -defined- function will look like this.
int findMaximumValueInArray(int newArray[], int sizeOfArray);
Once you have sent your array to each of the functions, you may then loop through the array using the sizeOfArray as its length so you do not attempt to preview past the bounds of the array. The first value of the array can be considered either your minimum or maximum value of the array and then compare the next value with your min/max value. If the next value is either lesser/greater than your current min/max value, replace your min/max value with the lesser/greater value that you just checked. Continue this until the end of the for loop. By this time you will have found your min/max value in the array.
**
myIterator = find(myVector.begin(), myVector.end(), InRange(10,20));
find();
...
shoudn't it be find_if?
myIterator = find_if(myVector.begin(), myVector.end(), InRange(10,20));
**
I am having trouble with this part of my program assignment.
It would appear you have created and tested each of the classes to make sure they work correctly. This is evident by the comment block for the testing of each ADT class that you made. I would assume that you had made each class so as they would create an instance of each class and display that instance to the screen. This is all assumed that you've gotten to this part of the programming.
What you need to do next is to work on the while loop. By the look of it, you want to continually read from the file until you have reached to the end of the file. The pseudocode that I would suggest includes the following:
while(!end of file) //read in the next character
infile >> getNextChar;
if(getNextChar equals Cruise){
get firstDateInteger;
get secondDateString;
get firstNumberInteger;
get secondNumberInteger;
create new CruiseInstance(firstDateInteger, secondDateInteger, firstNumberInteger, secondNumberInteger);
add CruiseInstance to dynamic array
} else if(getNextChar equals Ship){
get NameString;
get firstInteger
create new ShipInstance(NameString, firstInteger);
add ShipInstance to dynamic array
}else if(getNextChar equals Freight){
get firstNameString;
get yearInteger
get secondInteger
create new FreightInstance(firstNameString, yearInteger, secondInteger);
add FreightInstance to dynamic array
} else {
output to screen "Invalid input character. The program will now exit"
Exit program
}
} // end while loop
... the above pseudocode is basically what you are looking to do. You want to read in the first character, what type of instance …
dariaveltman
please tell what am I doing wrong.
First, please let us know what you are confused about. If you don't know exactly where you are confused in the code or how the code is supposed to work try telling us what you already do know and how you got there. This will help us understand exactly where you were left off as we can continue the discussion from that point instead of possibly repeating something that you may already know.
roc.ky89
I hope this will help you :)
wanna know concept?
I think your post is slightly inappropriate as we should be leading the OP towards his own conclusion instead of telling him the exact answer. By just telling him, they learn nothing. This does not help them later in the future when this type of exercise would have built a better programming structure for the programmer.
Second, you copied and pasted someone elses code. Granted, you did link a site to the page but not until you scroll down do we realize it was copied and pasted from this page. We don't, or at least I don't, expect you to paste it in MLA format, but a simple (CODE WAS COPIED FROM THIS SITE) would have been sufficient.
You're so helpful, Narue :D
Sorry, sorry. Cross213 is right. I misread it. I usually use the word "declaration" instead of "prototype".
int time(); // is the declaration or prototype of the function. This needs to be stated prior to the definition of the function. Functions need to be declared prior to the main() function.
.. example ..
int time(); // prototype, or declaration of a function. Basically declaring the use of this function -prior- to it being -called- in the main() function.
int main(){
//block of code
return 0;
}
int time(){ // function definition, { is the start of the block of code
int minutes = 60;
int seconds = 60;
int hours = minutes * seconds;
return hours;
} // end of block of code, end of function definition. This function is still has the data type of int, so an int is still required as the return type.
As Narue was stating, 'He means (I hope) that the prototype introduces enough information about a function to safely call it without requiring the function's definition to be visible. "
Later on, you are allowed to have the -function definition- in another file, but the declaration (prototype) needs to be in the current file if you plan on using that function.
Hey, that's cool. Doesn't matter if your 10 or 50, we all have the same goal here. We are here to learn, so kudos to you.
A prototype that they are discussing above is the data type or void that is prior to the function name.
void test();
void is the prototype and test is the function name. This is the only function that does not require a -return- in the function definition.
int time();
int is the prototype (data type) and time is the function name. This, like all other data types, requires a return statement in the function definition. Such as this next example.
int time(){ // function definition, { is the start of the block of code
int minutes = 60;
int seconds = 60;
int hours = minutes * seconds;
return hours;
} // end of block of code, end of function definition
// hours is of type int. The function, int time(), requires the return to be of int value. If hours was a type double, float, or any non integer then the compiler would likely give an error.
Functions are made for code that is anticipated to be frequently used. Later on you will learn about classes and functions. That's where the fun begins.
Is there anything specific you do not understand as there is a lot of information from
the beginning
to
the end.
Possibly explain what you do understand and then we can help with the rest. Otherwise what we would need to do is create a response that is nothing but a complete tutorial of what a function is.
Remember, arrays are initialized with a strict size --unless they are dynamic. To access the memory for any array, you access it be 0 through one less than the total size.
int numbers[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// numbers[0] = 1;
// numbers[9] = 10;
// numbers[10] = invalid due to accessing past the allocated memory - program will likely exit or give junk result
Please enter code in like this so it's easier for us to read.
This is wrong:
int main (); // You need brackets for the block of code containing the main function. -not a semi-colon ;
//like this
int main(){
return 0;
}
Okay. First, please format your code correctly using the
blocks that are given to us. Makes things a little easier to read.
I'm figuring your professor does not want want you to create 5 int variables in the main program and then add them to the list as you have done. Imagine if you had a ton numbers to enter. Yeah, that would be a lot of variables to make and keep them juggled properly to add them to a list. (hint: stick with one int variable).
Along with the previous notion of manually creating variables, adding the values to the linked list by manually typing next->next->next the number of times needed is a bit overwhelming when you lost your place after the 76th ->next, and needing to do it, 22 more times? Or was it 37 more times? Make a generic function to input your code.
To input your code, you can loop using a for-loop, or a while loop, or any method you'd like; but likely one you can count with. With each loop through when you create a new node, you do not need to create a new variable name for each new node, just link them together and keep going.
I'd get the previous part done prior to attempting a sorting function/algorithm. Once you have the input well in hand, the sorting function should look similar to any array sorting algorithm but with different syntax.
Once you have it …
From the looks of it, next is a pointer to the object type of what ever p, q, r and t point to. "next" is not a very common name for this that people use(i think). I like to use link. quick look at a simple object that is being used:
struct MyStruct { MyStruct* next; };
So when it says p->next = q, p's MyStruct pointer(next) now points to q.
Its kind've confusing at first. If you google something like "c++ linked list" you should be able to find some more information.
Nice job LevyDee with the explanation. To add a little to it, using LevyDee's example, you have objects that can link together to form a data structure. With any struct or class you can add other member variables to give more definition to each node (object). This is where they get the values of 1, 2, 3, etc from the previous pointers.
struct MyStruct
{
int value;
MyStruct* next;
};
As you have been doing all along with structs, you add a value to the object by first creating an object and then giving that object a value.
MyStruct first, second, third;
first.value = 1;
The variable next is a pointer; as it was determined with the preceding MyStruct* data type. To use this pointer, you must make it point to another object of the same data type, MyStruct.
first.next = second;
This is important, the value of -next- of object -first- contains the address …
outputFile << outChar;
needs to be included in the for-loop, not the while loop.
If it's only included in the while loop, the last character read will be placed in the OUTFILE.TXT. Whereas if the statement is placed in the for loop, every character read will be placed in the OUTFILE.TXT.
The reason why there isn't anything outputting to the outfile.txt is due to the fact that outChar is being manipulated in the for-loop a number of times but only until the end, past the end of the for-loop, is a call made to the outfile. If you check your normal cout output window you will see all your expected characters.
Something to take note on. You may want to change up your indentation slightly as it will make it easier for you to read.
If a user enters a number greater or smaller than you expect, use a while loop to ask the user again instead of automatically exiting the program.
If you already checked for an input/output file to exist, you don't need to check it again.
You also need to add the #include<string> if you are to use getline(infile, str)
Sure, you can add the second part after the calculations.
Prior to that, you may want to slightly edit what you currently have.
I'm assuming the code below is suppose to be read as this:
else if ((choice == 'b') || (choice == 'B'))
{
charges = packB;
cout << "\nMonthly Charge $" << charges << endl;
if (usage > 10) // !!! 20, not 10 as there are 20 free hours in B, 10 in A
{
addCharge = (usage - 10) * hoursB; // !!! make the same change here
cout << "Additional Charges: $" << addCharge << endl;
}
total = charges + addCharge;
cout << "Total Charges: $" << total << endl;
}
As it is, you may as well make a function as choice A and choice B are very similar. It cuts back on the repeated typed code. You would likely send in the -usage difference- (10 for A, 20 for B), -usage- (total number of hours).
And with the function, you may as well send call both A and B if choice A is made, or just send B if choice B is called and have a return type of double. Once the function is finished, you can output the results of the chosen selection, then compare each returned type.
You may find that you should compare A and B with choice C and A with choice B as imagine if a person only uses an hour or …
Odd. The above code seemed to work in a small test program, but once I tried it in my project, it did not work as expected. I changed it slightly to what I've linked below. Seems to work fine now.
/*
Purpose: Create a function to check the input from user. Only valid integer should be returned. All other input should be tossed, user should be asked again for valid integer input.
Name: Saith
Date: 6/4/11
*/
#include<iostream>
using namespace std;
int ReturnChosenNumber();
int main(){
int number = ReturnChosenNumber();
cout << number;
return 0;
}
int ReturnChosenNumber(){
// A very ghetto function. Enjoy the scrap work, make shift. integer checker :D
int number;
cin >> number;
while(cin.fail() ){
cin.clear();
cin.ignore(1000,'\n');
cout << "Input not an integer value. Try again.\n";
cin >> number;
}
return number;
}
Please tell me how bad this code segment is. I figured I would share this horrible code. If it's not as horrible as I think it is, please let me know. Thanks!
/*
Purpose: Create a function to check the input from user. Only valid integer should be returned. All other input should be tossed, user should be asked again for valid integer input.
Name: Saith
Date: 6/4/11
*/
#include<iostream>
using namespace std;
int ReturnChosenNumber();
int main(){
int number = ReturnChosenNumber();
cout << number;
return 0;
}
int ReturnChosenNumber(){
// A very ghetto function. Enjoy the scrap work, make shift. integer checker :D
int number;
cin >> number;
while(cin.fail() ){
cin.clear();
cin.ignore(1000,'\n');
cout << "Input not an integer value. Try again.\n";
number = ReturnChosenNumber();
}
return number;
}
Edit: I changed from using SStream, String, to just cin. The older version used them, but decided cin was just as good.
...I'm a fool.
I went out for a bike ride this afternoon and my mind wandered to this problem. I almost fell from the bike with laughter because I figured out what was happening.
I hadn't known the algorithm as well as I thought I did. Of course every time I run the program that it starts with 2. Once a "random" value is found, between 0 - 2, the max value is incremented to 3, then 4 all the way to the actual max value I was looking for. (Ironic, I've done plenty of sorting algorithms from scratch but this took me for a LOOP! Please excuse the pun).
The biggest reason why this wasn't so apparent to me at first was for the fact that my random numbers were always ranging from tens of thousands to high millions. It took quite a while for one to be as low as 0 through 2; that is when I decided to add the module (%) as Mike suggested.
Once it ran, I saw the incrementing of the max value. I feel foolish for not realizing this far ahead of time so as not to even come close to having this problem in the first place, but I bring this up for any other that may have had a lack of momentary sense as I did to find the answer they were looking for.
Thanks template<> and Mike_2000_17 for your input. I'll try to read an …
Okay. Ill agree with the srand() and making a default constructor with one parameters so it is not called once. Thisstill does not solve the fact of max being 2. You can completely delete srand() and just use rand(). And the max, value is still 2. Obviously the randomInteger class function should not have anything to do with random_shuffle(para1, para2, para3) call.
Either way, thanks for the input so far. Please keep them coming.
Again reading through the site that I just posted, the randomizer is suppose to get a value which is calculated by the pointer difference from the second parameter from the first in random_shuffle(para1, para2, para3). I'm trying to figure out why the pointer subtraction is not working correctly in both cases (My actual program and the newly made simple program).
This morning I attempted to add a default constructor with one parameter, unsigned int, and a private member unsigned int max. I changed the
randomInteger randomizer;
to
randomInteger randomizer(topCard);
It compiled fine, but then I remembered after running it, the parameter in the overloaded parentheses function gets passed this max value; it does not need a private member variable for it. I suppose I can just remake the function so the overloaded parentheses operator does not need to be passed any values, or at least not use the value it was passed.
Below is a small, but complete program, that shows the same results that I was getting before.
/*
Purpose: Recreate RandomInteger with a small test class
Name: Saith
Date: 5/29/11
*/
#include<iostream>
#include<algorithm>
#include"randomInteger.h"
using namespace std;
randomInteger randomizer;
int main(){
int numbers [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
random_shuffle(numbers, numbers + 10, randomizer);
return 0;
}
The randomInteger .cpp and .h files are the exact same. I decided not to resubmit them to reduce the size of this message and spam effect.
I somewhat understand the concept of the parentheses overload when you need to use an object as a function. The question that I have is why does the max variable in the listed code below always input 2.
/*
Purpose: This is the header file of randomInteger.h
The class creates a random integer
Name: Saith
Date: 3/5/11
Edited: 5/28/11
*/
#ifndef RANDOMINTEGER_H
#define RANDOMINTEGER_H
#include<iostream>
using namespace std;
class randomInteger{
public:
unsigned int operator() (unsigned int);
unsigned time_seed();
};
#endif // RANDOMINTEGER_H
/*
Purpose: This is the implementation file, randomInteger.cpp
Name: Saith
Date: 3/5/11
Edited: 5/28/11
*/
#include<iostream>
#include<time.h>
#include"randominteger.h"
using namespace std;
unsigned int randomInteger::operator () (unsigned int max){
// QUESTION: Why is Max always 2? Or at least the times when I run/debug it.
unsigned int r;
do{
srand(time_seed());
r = rand();
}while ( max < r); // repeat while random number is greater than the maximum number.
return r;
}
unsigned randomInteger::time_seed() {
time_t now = time ( 0 );
unsigned char *p = (unsigned char *)&now;
unsigned seed = 0;
size_t i;
for ( i = 0; i < sizeof now; i++ )
seed = seed * ( UCHAR_MAX + 2U ) + p[i];
return seed;
}
It is used in the context as such:
void Deck::shuffle(){
random_shuffle(deck, deck + topCard, randomizer);
}
Shuffle runs fine if I change the max value in the overload definition to 60, so it would read
while ( 60 < r); // repeat while random number …
I'm not sure what you mean by full bonus mark. Is the teacher requesting you to continue the code beyond the scope of what is required by having an 8th or more part of the program?
Also, when you submit code, please submit it with the code tags, it makes it much easier for the rest of us to read.
[ CODE] <<-- beginning of the tag
[/ CODE] <<-- end of the tag Just don't have the spaces
/*---------------------------------------------------------------------------------------
Source File: OOP4.Henri.cpp
Program name: Object Oriented program, Assignment 4
Author: Hassan Aghaei Baradaran (Henri)
Compiler: Bloodshd Dev-C++ 4.9.9.2
Purpose: To define and demonstrate a class representing a Rectangle
-----------------------------------------------------------------------------------------*/
#include <stdlib.h> // system pause
#include <iostream> // input output
#include <string> // to use string class
#include <iterator> // used for line_maker function
#include <sstream> // used for line_maer function
using namespace std;
class Rectangle // CLASS DEFINITION
{
private:
int height; // data members
int width;
char ch;
bool filled;
public: // Methods
Rectangle() // default constructor (part 1 of the program)
{
height = 0;
width = 0;
ch = ' ';
filled = false;
}
Rectangle(int h, int w, char c, bool f) // 4-argument constructor (part 2 of the program)
{ // allows the members to be initialized
height = h;
width = w;
ch = c;
filled = f;
}
// --------------------------------- MUTATOR FUNCTIONS:
void set(int h, int w, char c, bool f) // Set function (part 3 of the …
I guess you are right but in this program he doesnt really need to change it. He created the object before asking for input and it was unnecessary.
You think so? I think what he did was just fine. First creating the empty object and then setting the values to the users choice. This set up will be handy when he learns about pointers and dynamic arrays/classes/structures that are soon to come. Note that there is a second object that was made with the constructor of 4 parameters.
I suppose it comes down to personal preference with this specific example that the OP has given.
Next time put code in code tags. Also the teacher said that you don't need set() function. You first ask user for input like hight and width and then you make a new rectangle with hight and width. So it should look SOMETHING like this
cout << "enter hight: "; int hight; cin >> hight; cout << "enter width: "; int width; cin >> width; cout << "enter star"; char sign; cin >> sign; Rectangle rect(hight, width, sign, false);
void set(int h, int w, char c, bool f) // Set function (part 3 of the program)
{ // assigns valus to data
height = h;
width = w;
ch = c;
filled = f;
}
Seeing as how this is part 3 of the program, as required by the teacher in the comments of the original poster, set function is required. But you are right in the terms of using the constructor to first make your object; the rectangle. What set function will do is change the contents of the already made object instead of making a new object with the changed settings.
You missed an important point. The rectangle "knows" its internal state. When you draw the rectangle, you do not need to send it any data. It already knows its own height, width, etc. (which ave already been set in the constructor or set function). So you just tell it to draw itself; no arguments necessary. Same for the flip.
Your class private member variables do not need to passed as parameters in the function draw_rect. Only arguments need to be be passed through a class function is when the argument does not belong to that specific class; i.e. non-member data types and their variables.
Your member variables are the 4 that you listed, height, width, filled, ch. So any class member function (mutator/accessor/etc) do not require you to list the height, width, filled, or ch (Unless it belongs to another class! And in that case, you'd likely pass the whole class and not likely the single variable of that class).
And the toggle should not do any input or output. When you tell the rectangle to toggle itself, it just toggles the flag internally, that's all -- by calling the function, you are sending a message to the rectangle to change its internal state. To see the results, you ask it to draw itself again in a separate message.
The programmer who is using your class and chooses to use toggle function is expecting to just toggle, not a reconfirmation of wanting to toggle or not. It's like …
One suggestion with your changed if/else that you were working on.
If the program fails to open the file, might as well close the program with
exit(1);
at which point you do not need to worry about an else. If it doesn't fail, the program keeps on going leaving the block that you had to worry about nonexistent (If you don't need an else, you don't need a block for that else).
And for pointers, you will naturally understand them with more practice. It seems like the few references that you did look over may not have been adequate for you. There are a lot of "tutorials" and books out there that are not well written or for a very specific purpose that may be hard to comprehend. Keep it up though. Once you learn how pointers work, you will have a blast with dynamic arrays, classes, etc. :D
Only thing I could possibly think of, which could easily be wrong, would be to close the solution, copy and paste your entire program into a new project. The program you are trying to run could possibly be running with an older solution/project, I don't know.
Let us know when you get this fixed. Thanks
double calculate ( double change)
{
return change * 1.85 + 2.50;
change=+total;
}
In the previous code,
change =+ total;
is never ran. There is a return statement just prior to that statement that will forever skip change =+ total.
You need to change your expression statements in your flow of control.
if (first = length * 1.85 + 2.50) // will always be true
if (response='n') // will always be true
You also don't assign total to anything. At no point in your program do you have written
total = // this partial statement is not in the program, how can you output the total if you haven't equated it to anything.
Yes, after compiling your new code, the only compile errors I got were from your Get function. Why int? Why do you need to return any value? All you are doing is grabbing data from your file and it's already being saved to your object, your pass by reference is making sure of that through the function call.
Also, with your input. Do you
cin >> (number, ' '); // ? No, this doesnt look right.
cin >> number; // does look right.
No, it's all good. Keep it up, though. The more time that passes without keeping up with your studies, the more behind you will get.
If for your output function you do -not- need to write to an outfile but into the DOS that you normally see after compiling/running, you do not need to pass an ifstream to the output function, just pass the class and it's object.
With the input, remember how you can input a number from the program normally such as
cin >> number;
You can do something very similar to that with infile and string/int/double variables :D If you use getline(), you will need to then parse the entire line into what you are looking for.
After trying to compile your code a bit, I saw a couple more problems. Your GetData function, you are trying to access your information through infile.get() which only get's one character at a time, but you need to get a string at a time. You also want to check your functions data type, why double?
Your DisplayTotals function needs to be changed so that it's not writing to the infile, but writing to an outfile.
Good to hear you don't want your homework done for you, it's definitely a plus on my end to see.
There are a number of things wrong with the code, but overall, not a bad start.
You may want to give us a reason why it may be buggy? Tends to boil down to syntax/logical/both. If it's syntax, just start from the top and work your way down.
I'll help you out for a minute or two.
First, your structure definition looks good. I see no problems there. What I do see wrong is the calling of a Record object that you are trying to pass through functions. Like classes, which you will likely learn next, you will have a class/structure and an object of that class that you will be manipulating.
A quick side-track moment. Think of a class/structure as the human race. Think of an object as one particular person. When you want to change someones eyes color, you need to change that particular persons eye color, not the entire human race. You do this in C++ first by creating a data type (class/structure/human race) followed by the variable(object/person); now you may change one particular persons eye color (or any other variable) through that object you just created.
ie int number; // int is a data type, number is it's variable
Record FooFighers; // Record is the data type (class), FooFighters is it's object.
then, to send FooFighters through a …
A possible solution. The location of your number you are comparing for each read number could be associated with the arrays index. (i.e. 4 and array[4] ). Read in the number through a variable, then access the arrays memory location with that read in number (using the number as the index), then incrementing that location.
At which point, you are not comparing any other integers in the input file, you are just counting how many times they occur.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
After rereading the original post and seeing the output required, this solution will not work for your particular case, but may help others in similar cases that do not need to be so strict.
You may consider setting your program up so that it first
1. Reads in the numbers through and array.
2. Checks the array against itself
3. then outputs any repeats it may have.
What you are currently doing is inputting a number into an array, then checking against array values that have -not- yet been initiallized.
The Add() function is declared on line 12. It takes a constant Counter reference,
which is the number to add to the current object.(which is varOne?)It returns a Counter object, which is the result to be assigned to the left side of the assignment statement, as shown on line 35.( which is varThree?) That is, varOne is the object, varTwo is the parameter to the Add() function, and the result is assigned to varThree.Return Counter(itsVal+ rhs.GetItsVal()); Is itsVal+rhs.GetItsVal()); VarOne + VarTwo? and is Return Counter (Var three)?
It appears you have the correct answer, but in the Analysis that is written, it reconfirms what your questions are.
It takes a constant Counter reference,
which is the number to add to the current object.(which is varOne?)
Yes, the object that is calling the function, which is varOne, needs a reference parameter of type Counter.
It returns a Counter object, which is the result to be assigned to the left side of the assignment statement, as shown on line 35.( which is varThree?)
Yes. Once the manipulation from the function is done, it returns a new object that will be saved to the variable on the left side of the assignment operator, in this case to be varThree.
Return Counter(itsVal+ rhs.GetItsVal()); Is itsVal+rhs.GetItsVal()); VarOne + VarTwo? and is Return Counter (Var three)?
Yes, the value from the first object plus the value from the second object is saved and returned to varThree
First, my program works.
I'm posting my code for my own personal benefit with the help of your guidance to know of any better way of handling this code. I was considering the use of
while(cin >> grabname)
to possibly continue grabbing from the input stream until '\n' was reached (hoping <enter> would made an eof approach). Seeing how this made an infinite loop, I had then chosen to switch from strings to char then back to strings. It appeared this was the only solution that I could come up with that allowed a check for any additional values of non-whitespace or '\n'. This approach doesn't seem very smooth to me, any recommendations with string manipulations?
I know I could try getline(cin, FullName), then split it them all through a function with vector<string> but the text did not recommend this and suggesting 3 string variables, mentioned it would be easier that way.
The point of the program is to output a persons name LastName, FirstName MiddleInitial. If no middle name or initial was given then output would look as follows, LastName, FirstName.
Just as a refresh statement for any that did not know -this is not for any class.-
Home-study 100%.
If this is an inappropriate post please let me know.
Thanks in advance.
int main(){
char aChar;
string FirstName, MiddleName, LastName;
cout << "What is your first and last and possibly middle name?\n";
cin >> FirstName >> LastName;
cin.get(aChar);
while(aChar …
A side note, possibly logical error, but I will blame it being past midnight if Im at fault xD
if (0 > sizeIn && sizeIn > 10)
// reads, if sizeIn is less than zero AND sizeIn greater than 10, do this, else do that.
// ... when would it ever be true?
You may want to change it to a more logical pattern of the pseduocode listed below
if (sizeIn is greater than 10 || sizeIn is less than 0)
<do this code>
else
<do that code>
OR, using your && (AND) operand
if (sizeIn is less than or equal to 10 && sizeIn is greater than or equal to 0)
<do this code>
else
<do that code>
Hi, I am working on an exercise in operator overloading. I've created a matrix class and I am supposed to overload operators so I can do arithmetic on matrices efficiently.
This is where the fun part comes into play :D Enjoy it and let it roll.
My directions say that I am supposed to make two matrix arrays using the class constructor that has 2 parameters and a third matrix array that will be used to store the result of arithmetic using the default constructor (1 parameter).
Sounds about right.
Since I am going to use these arrays to overload operators they are going to need to be data members of the class (I think).
Yes. The data that you will be using for your particular class assignment will be class member values of the matrix class that you created.
However, I thought that classes were supposed to be as representative of real life things as possible so making a matrix class with multiple arrays doesn't make sense to me (a matrix is only one matrix).
Nice thinking. It wouldn't make much sense to see one matrix having more than one matrix inside when you are trying to create and have one matrix in all.
Am I misunderstanding classes or is there a different way to make additional matrices using the class constructor I am not thinking of? Thanks all, here is the code in question.
I think you have part of it down. Once you …
You may want to list the compiler problems, if any, for us to view. Seeing as how there is no description, other than the code, to the problem it is difficult for us to figure out whether this is a 1. syntax problem, 2. logical problem. Lastly, what is this program supposed to do? I could start guessing but I suppose that wouldn't be very helpful.
double& operator [](int i) //[] which can used as the left value
{
if(i==0) return x;
else if(i==1) return y;
else if(i==2) return z;
else cerr<<"our of rand index of Point";
}
The function has a return type of double which you have returned three times in your above code. The error is due to a lack of a 4th return from your if-else statement. You need to return or exit the function; or possibly some other solution that I can't think of at the moment.
else cerr<<"our of rand index of Point";
// either return here or exit() prior to the end of block of code
i.e.
double& operator [](int i) //[] which can used as the left value
{
if(i==0) return x;
else if(i==1) return y;
else if(i==2) return z;
else{
cerr <<"our of rand index of Point";
// return 0.0 or
// exit(1)
// some sort of error handling
}
}