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.
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.
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;
}
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
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 …
Yeah, you can't do that with the while loop.
while(choice != A && choice != B && choice != C ... etc)
OR you can write it as
while( choice < 1 || choice > 4)
// basically read as, while choice is less than 1 or greater than 4, do this again
// Meaning, not 1 or 2 or 3 or 4
rational rational ::operator +(rational r){ rational sum; sum.numerator =numerator+sum.numerator; sum.dnomenator=dnomenator+sum.dnomenator; cout<<"The sum afer adeng both fraction is :"<<endl; return sum; }
Few things wrong with the logic of this.
1. You need to add two separate objects together, not one object and the sum of two objects.
Ex.
You want A + B = C
not A + C = C
This is shown with both additions in the previous code.
2. You need to find a common denominator between both fractions. If they are already equal, then you can just add the numerator and KEEP THE SAME denominator. If both denominators are different, you need to find a common denominator, then change the numerators respectively prior to adding the numerators.
Finally, you can use your reduction function to simply your answer.
// Example - rational is a class name not shown but defined else wherefriend rational operator+(const rational& var1, const rational& var2){<some code>}
What I wrote above is wrong with this set up. It is correct that the + operator only takes one argument, not two. The first argument is actually the variable on the left side of the + sign in the equation between the two objects. The second variable is the one that is passed which is on the right side of the object.
A + B // A is the first argument, B is the second which is passed to the function.
The reason why you want to overload an operator to a class is due to the unknown variables you actually want to add. You will specify which variables to be combined into a -new- object, the returned object.
// class
class stuff{
public:
<some other functions declared>
// declared friend operator
friend stuff operator +(const rational& obj);
private:
int var1;
int var2;
};
// declared function
stuff stuff::operator +(const rational& obj){
stuff temp; // will create a new object of type stuff to store the sum
// we will add the two objects together with the desired variables and
// save them in the new temp object.
temp.var1 = var1 + obj.var1;
// in the example of A + B, var1 is referenced to A,
// and obj.var1 is referenced to B.
temp.var2 = var2 + obj.var2;
// in the example of …
Please reread what I wrote, then check your answer with mine. Do they look similar? Do they have the same amount of parameters? You also declined to add the friend function declaration in the class definition, without it, the class will not know where to look for the + operator.
Here is a quick tutorial that should help with your needs.
To add overloading operators to a class definition, you need to add them as a FRIEND function.
friend <type_name> operator <operator>(const <type_name>& var1, const <type_name>& var2)
// Example - rational is a class name not shown but defined else where
friend rational operator+(const rational& var1, const rational& var2){
<some code>
}
Would be how you would define a friend operator function. The difference between a friend function and an ordinary function that are defined in a class, the friend function is not a calling function from any one particular object but may be used for one or more objects (unary operation, binary operation)
Once you figure out how to overload the + operator, you can overload the << operator instead of using the show() function.
friend ostream& operator << (ostream& outs, const rational& object);
//Precondition. If outs is a file, outfile has been opened.
// Object have been given a value. Will output object members to outs ostream
}
vmanes sir one more thing did i used the constructor for the p2 correctly ?
If you are asking about the declaration of your objects to your class as such
phone p1,p2(a,b,c);
Then yes, you can give values of a, b, c with this constructor. Problem occurs when compiling, "What are the values of a, b, c, and are they integer values, character values, what is the data type you are looking for?".
You need to state what the data type is and how you will get those values into the constructor when the object is being initialized.
A. Already have inputted variables, such as
phone p1,p2(1, 2, 3);
B. Have the main program require the user to input variables,
int a, b, c;
cout << "input numbers" << endl;
cin >> a >> b >> c;
phone p2(a,b,c);
This is basically a rephrase of what Vmanes was suggesting. You will also need to edit what Vmanes had suggested about the structure of your class definition. If you can copy and paste what you have now and any errors you can't figure out, we may be able to help you more along.
Good luck with your programming club.
I can see why you wanted to use your MAIN() in the first instance.
if(choice<0 || choice>4){
cls();
main();
}
If the selected choice is not valid, just restart the program. Instead of restarting the program, just ask the user to "try again" with a more valid input.
do{
<some code>
while(some input is not valid); // if invalid, do again; else continue past the while statement
You may want to revisit while,do-while,for loops again. Once you have this working correctly, you can "basically repeat this" for another other main() you had when dealing with invalid input.
You can create a namespace for all of your classes and then add that global variable in that namespace.
You may consider changing your constructors to your classes to add an additional parameter. As the class is created that variable will be used as a parameter that you can share between all classes.
P.S. Nathan seemed to beat me to it.
Remember cin.ignore has two parameters.
cin.ignore(1000, '\n');
This is to clear the input stream useful with while mixing cin.get(param1) and cin >> variable1.
Example 1
/*
Purpose: threads/354626
Name: Saith
Date: 3/20/11
*/
#include<iostream>
using namespace std;
int main(){
int var1;
char var2;
cout << "Input some number.\n";
cin >> var1;
cout << "Your number is "<< var1 << endl;
cout << "Input another number.\n";
cin.get(var2);
cout << "Your number is "<< var2 << endl;
return 0;
}
Input:
1
// c // would have been the second input
Output:
1
[blank space]
and will not bother pausing for a keystroke due to a character already in cin (the '\n' character). If you add the cin.ignore(para1, para2); syntax, the '\n' character will be removed from the stream input along with any other value.
Example 2
/*
Purpose: threads/354626
Name: Saith
Date: 3/20/11
*/
#include<iostream>
using namespace std;
int main(){
int var1;
char var2;
cout << "Input some number.\n";
cin >> var1;
cin.ignore(1000, '\n');
cout << "Your number is "<< var1 << endl;
cout << "Input another number.\n";
cin.get(var2);
cout << "Your number is "<< var2 << endl;
return 0;
}
Input:
1
c
Output:
1
c
First, as Crutoy as responded, you want to populate the array.
Use nested for-loops. A generic one you may find would include something similar to this.
/*
Purpose: thread347853 - filling a multidimensional array
Name: Saith
Date: 2/17/11
*/
#include<iostream>
using namespace std;
int main() {
const int vertical = 2;
const int horizontal = 2;
int list[vertical][horizontal];
for(int index = 0; index < vertical; index++){ // will fill your array vertically
for(int index2 = 0; index2 < horizontal; index2++){ // fill your array horizontally
cout << "Please input [" << index << "]["<< index2<<"]" << endl;
cin >> list[index][index2];
}
}
cout << "OUTPUT "<< endl;
// And to output them, you would do the same thing.
for(int index = 0; index < vertical; index++){
// will output your array vertically
for(int index2 = 0; index2 < horizontal; index2++){
// output your array horizontally
cout << list[index][index2] << " ";
}
cout << endl;
// as the index increases (vertically) a new line will be make for the next set of numbers
}
return 0;
}
If you compile and test this code, it may help you understand how/why multi-dimensional work. You can really cut down on the amount of coded lines. Imagine, if not 10x10 but 1000 x 1000, trying to code in for each possibility. Yeah. Good luck on that one! Good luck to you on this current project.
-Saith
Odd though, I just created this. It gave me no linking error >< Even with a char passed to an int
Because you are passing a char to a int variable.
Well its O.K. if its not in a header file where we can assume things like the user will use things like 'using namespace std;'
Being the newbie that I am. I can't say that is true. I had "using namespace std;" in the implementation AND application file but NOT in the header file as I attempted to build the project without the std:: preceding vector<int> v1;. I got an error.
I included the std::, the error resolved. I removed std:: and included "using namespace std;" and the error was resolved.
Okay, to use
vector<int> v1;
without the need of the preceding std::, you need to include the
using namespace std;
at the beginning of the header file, else std:: will be required.
#ifndef SSASSG1_H
#define SSASSG1_H
#include<vector>
class NumberSet
{
public:
NumberSet(int array1[]); //transfers the data from an array to a vector
NumberSet( ); //default constructor
int array_size_horizontal;
private:
std::vector<int> v1;
int vectorsize;
};
#endif
scratch that. I just rebuilt it again after no errors, and vector<int> v1; gave me an error.
So, I tried std::vector<int> v1; again. The only error after that was asking me to put a
#endif at the end of the HEADER file
I copied and pasted what you had.
The only solution I came up after compiling with no errors is:
#include<vector> in the header file
Oh, and get rid of
std::
in
std::vector<int> v1;
So its just
vector<int> v1;
What happens when you add the semicolon at the end of the if-statement, the if statement checks to see if the statement is true or false. It ends there. The block of code following the if-statement is interpreted as a new block of code, regardless if the if- statement was true or not, that will run.
/*
Purpose: The semicolon at the end of the if-statement
Name: Saith
Date: 2/5/11
*/
#include<iostream>
using namespace std;
int main() {
if(4 > 5); // we know this is false, but will run the following block of code anyway
{
cout << "hello world";
}
return 0;
}
Is a snippet of full program that will output hello world.
// Original
if(begllts <= intarray[m] && intarray[m] <= endllts);
// Akill10 changed to
if(begllts <= intarray[m] && endllts >= intarray[m])
is the same exact thing. No difference, other than Akill10 got rid of the ; at the end of his if-statement.
OH
I see
check your IF STATEMENT.. GET RID OF THE ;
if(begllts <= intarray[m] && intarray[m] <= endllts); //<--- that last ;, Get rid of it BAD, NEWBIE BAD .. :D
Okay. I just compiled your code and debugged a bit. The reason why are getting an error when running your code is due to the divide by zero effect. It's GG.
You are wondering why numOfClasses = 0 by the time you call the second function, it's because numOfClasses actually equals zero.
Why? Because in the first function, you passed the value argument, pass-by-value, and not pass-by-reference. You told the function that numOfClasses equals 3 through cin, but that assignment to numOfClasses was only valid in that functions block of code. Once the block ends, numOfClasses value is now wiped and replaced by the value prior to the function call, zero. Making all the parameters for that first function, pass-by-reference, you will save those variables past the end of the block from the function. You will also need those variables saved past that first block to be used in the third function.
Also, in your third function, the output function to the screen with the final results. There are no syntax errors, but there are logical errors. Your output of letter grade outputs the LAST LETTER GRADE entered. NOT the average letter grade. So if you inputted A, A, C. Your letter grade is C, which should be B.
Also, for -just in case-, you want to equate totalPoint to zero prior to using totalPoints += qp; as it also means, totalPoints = totalPoints + qp; and if there is some random junk from a previous …
//Prototypes being used:
void userData(string, int, char);
double calData(int, int, int);
void displayData(string, char, double);
// You need to change your function declaration of calData to two arguments, not three
/*
Purpose: Have user input lines of code until sentinel value has been found. Output results
Name: Saith
Date: 2/4/11
*/
#include<iostream>
#include<string>
using namespace std;
int main() {
string line, paragraph; // strings are initialized as empty, no need to assign them.
while(line != "done"){
paragraph += line;
cout << "Enter newest line.\n";
getline(cin, line);
}
cout << paragraph;
return 0;
}
Here is a sample that I just created using user input.
call the function multiple times.
If you are reading the input from a file, you can run a while loop that checks for EOF.
If you are reading from user input, make a sentinel to end the while loop.
while(string_name != "Done"){ // or change the string to whatever you want, "Exit" "Finished", "0"
<some code here>
}
You don't need to have the data type when calling your function in the main() function.
int main()
{
//called functions
userData(string studentName, int numOfClasses, char letterGrade);
calData(int totalPoints, int numOfClasses);
displayData(string studentName,char letterGrade, double gpa);
getch();
return 0;
}
You also need to initialize totalPoints to zero prior to calling it for the first time. You can debug the code with break lines and then check what your local variables equate to line by line.
You also may want to make functions for each of the cases.
case '1':
first_function(Parameter_list);
break;
case '2':
second_function(Parameter_list);
break;
default:
break;
}
void first_function(Parameter_list){
<code>
}
void second_function(Parameter_list){
<code>
}
makes it cleaner, you can also use this code outside of the switch statement.
The nested for loop is what you are looking for. You will use nested for loops once you get into array of arrays.
void buildSquare( int length, char symbol){
// Length = Length x Length to make the box
// Symbol will be used to make the box
// The for loop nested inside a for loop help creates
// this first for loop will be used for the vertical length of the box
for(int side = 0; side < length; side++)
{
// this second for loop will be used to create the horizontal length of the box
for(int side2 = 0; side2 < length; side2++){
cout << symbol;
}
cout << endl;
}
}
Good luck trying to contact Mr. Bill for that answer.
I suggest googling it. As I just did,
http://www.makeuseof.com/tag/5-ways-to-make-your-own-screensavers-windows/
Question? How is this related to C++? If you have any program that you would like use to look at, I know I would be interested in seeing any progress you've made.
You also may want to read up on strings and string input, from what I read from multiple other sources, you want to be using the standalone function getline(cin, npcname) vs the regular cin >> input. Mixing cin >> with strings may give some funky results.
This is what I came up with. The code works for any valid input from 1 - 9. You can change the range from a number higher than 9 or lower than 1.
#include<iostream>
using namespace std;
int main(){
int number;
cout << "Please input a number 1 - 9.\n";
cin >> number;
if(number < 1 || number > 9){
cout << "Input valid number between 1 - 9.\n";
exit(1);
}
cout << "You input was " << number << endl;
return 0;
}
I just wrote it. You can copy and paste what I wrote and build it. It should work for you. You may want to toss your while loop back in there to check to make sure that the input is valid, between 1 and array size-1.
// This part, as copied and pasted from my previous note, will output the desired array to cout.
for(int index = 0; index < number; index++){
cout << name[number-1][index];
}
As Fbody mentioned, change the size of your array accordingly. To output any type of array, especially two dimensional, use for loops.
#include <iostream>
using namespace std;
int main () {
char name[20][20] = {"1. One",
"2.Two",
"3.Three",
"4. Four",
"5. Five",
"6. Six",
"7. Seven",
"8. Eight",
"9. Nine",
"10. Ten",
"11. Eleven",
"12. Twelve",
"13. Thirteen",
"14. Fourteen",
"15. Fifteen",
"16. Sixteen",
"17. Seventeen ",
"18. Eighteen ",
"19. Nineteen",
"20. Twenty"
};
int number;
cout << "Enter the number, I will repeat after you.\n";
cin >> number;
for(int index = 0; index < number; index++){
cout << name[number-1][index];
}
return 0;
}
Can you paste the code that you have written so far?
I feel like a goof, trying to help out but upload incorrect programming on my very first entry! ><
CORRECTION:
function(happy_num); // pass by value
function2(happy_num); // pass by reference
Incorrect:
unhappy_num = function(happy_num); // pass by value
unhappy_num = function2(happy_num); // pass by reference
** voids can not return int **
I just started back into C++ from taking some time off from it as well, slight rusty, but let's see if I can give you a shot at what you are asking for.
class FullOfFunctions{
public:
void function(int happy_num); //pass by value
void function2(int & happy_num); //pass by reference
private:
};
When you use the function for either in the main() program, their use will look the same.
int main() {
int happy_num, unhappy_num;
unhappy_num = function(happy_num); // pass by value
unhappy_num = function2(happy_num); // pass by reference
return 0;
}
void FullOfFunctions::function(int happy_num) {
//purposely left blank
// random code
}
void FullOfFunctions::function2(int & happy_num){
// purposely left blank
// random code
}
As far as I'm aware, even with input/output streams to class member functions, you will not need the & while calling the function in the main() program. Don't quote me on this but the compiler will know prior to manifesting the program if the variable passed, including passing objects of classes or structures, will be either "pass by variable or reference."
I hope this helps.