Posted: Tue Apr 26, 2005 11:31 am Post subject: For Loop Trouble . . .
Hey, i was trying to make just some simple multiplying program, where it asks to input two numbers, and gives the sum of them. After I enter the first number, the program finishes. Any ideas?
code:
#include <iostream>
using namespace std;
int numb,numb2,result;
int main (){cout <<"Enter a number";cin>>numb;return 0;}
int main2 (){cout<<"Enter another number to multiply it by";cin>>numb2;return 0;}
int calculate (){cout<<numb<< "Multplied By"<<numb2<<"Is Equal To"<<numb * numb2;cin>>result;return 0;}
Sponsor Sponsor
wtd
Posted: Tue Apr 26, 2005 11:55 am Post subject: (No subject)
A few things:
The "main" function if your program's "entry point". It's where everything happens. Unless you call a "main2" function from within "main", the code in it won't be executed.
Don't declare global variables. Declare them as locals within the main function.
Use space.
c++:
#include <iostream>
int main() { int n1, n2;
std::cout << "Enter a number: ";
std::cin >> n1;
std::cout << "Enter another number: ";
std::cin >> n2;
Posted: Tue Apr 26, 2005 1:29 pm Post subject: (No subject)
Ok, thanks wtd, probabily makes more sense than mine (using std:: ). One quick question though, how do you make the code type C++ when you post?
wtd
Posted: Tue Apr 26, 2005 1:31 pm Post subject: (No subject)
[syntax="cpp"][/syntax]
jamonathin
Posted: Tue Apr 26, 2005 1:42 pm Post subject: (No subject)
c++:
Thanks man
Andy
Posted: Tue Apr 26, 2005 7:42 pm Post subject: (No subject)
good one.. except he said cpp not c++
wtd
Posted: Tue Apr 26, 2005 8:02 pm Post subject: (No subject)
You can't use "c++" in the syntax tag. "cpp" gets turned into "c++".
jamonathin
Posted: Wed Apr 27, 2005 8:40 am Post subject: (No subject)
Hey, I have a new question now. I've been lookin through the Tutorials, and I've come accros some basic stuff, and I'm havin a little trouble with it, cuz I cant really understand what the error log wants me to do.
I tried out that "password program" and so i can do that, it just mixing stuff together, this is what i have
c++:
#include <iostream> #include <string>
int main () {
std::string password= "password",userword= "";
int entry;
std::cout<<"Enter The Password: ";
std::cin>>userword;
Posted: Wed Apr 27, 2005 10:04 am Post subject: (No subject)
The problem that you are having is with your if statements.
In C++, you must inclose your if statements in code brackets like so:
c++:
if(statement) {
...the code in here gets run if'statement' is true...
} elseif(statement2) {
...this code gets run if statement2 is true...
}
The exception to this is when you have a single line after the if statement, like so.
c++:
if(statement) cout << "Hello" << endl;
is the equivalent of
c++:
if(statement) { cout << "Hello World" << endl;
}
If you wrote this, however, your output might not be what you expect:
c++:
if(statement) cout << "This line gets output if statement is true" << endl;
cout << "This line gets executed regardless of whether or not statement is true" << endl;
The above code is equivalent to this:
c++:
if(statement) { cout << "This line gets output if statement is true" << endl;
} cout << "This line gets executed regardless of whether or not statement is true" << endl;
wtd
Posted: Wed Apr 27, 2005 11:11 am Post subject: (No subject)
First off, if "entry" is meant to be eiher 1 or 0 all the time, it sounds like a boolean value. In this case use a bool variable.
Posted: Wed Apr 27, 2005 11:11 am Post subject: (No subject)
Fisrt problem is that the second function is not called from the first, nor is it properly bracketed. Another problem, which is easiest to fix here is that you use a variable called entry in calculate, but you don't ever decalre it. Since it's obvious that you want entry to have the same value as entry in main, we'll make it a parameter that get's passed. The fix is to add an open brace after the function header (int calculate(...)), and add a varaible to the header:
c++:
...
int calculator(int entry) {
....
}
Another small, problem is that you need to declare a function before you call it, so as it is the main function doesn't know about the existance of the calculate function, so if you try to call calculate from main you'll get an error.
Here's hte rearanged code with proper bracketing, and the calculate procedure moved before the main procedure:
c++:
#include <iostream> #include <string>
int calculator(int entry) { int choice, n1, n2;
if(entry == 1) {
std::cout<<"Welcome to Calculator!";
std::cout<<"What Would Like To Do?";
std::cout<<"1 - Add";
std::cout<<"2 - Subtract";
std::cout<<"3 - Multiply";
std::cout<<"4 - Divide";
std::cin>>choice;
std::cout<<"Please Select Two Numbers. ";
std::cin>>n1;
std::cin>>n2;
Now, the next thing to change is really important: you don't call calculate from main, so it will never be run! From what I can guess you want the calculate code to be run only if the user inputs the correct password, so doing that we get:
c++:
...
int main () {
std::string password= "password",userword= "";
int entry;
std::cout<<"Enter The Password: ";
std::cin>>userword;
Now the last thing we can do is simplify the program be removing any unessessary steps, such as checking entry; since we only call Calculate if the password is correct, entry will always be one, and thus the code in Calculate() will always be executed. From this we can get rid of entry all together, as well as the big if statement in Calculate():
c++:
#include <iostream> #include <string>
int Calculate () { int choice, n1, n2;
std::cout<<"Welcome to Calculator!";
std::cout<<"What Would Like To Do?";
std::cout<<"1 - Add";
std::cout<<"2 - Subtract";
std::cout<<"3 - Multiply";
std::cout<<"4 - Divide";
std::cin>>choice;
std::cout<<"Please Select Two Numbers. ";
std::cin>>n1;
std::cin>>n2;
** note that in this case the delay in calculate was unnessessary, as there is a delay in main.
Hope this helps clear up a little at least, if you have any questions just ask
EDIT (Martin): Gah, learn how to program Cornflake. Fixed your code.
jamonathin
Posted: Wed Apr 27, 2005 11:37 am Post subject: (No subject)
Wow thanks for all ur help guys. I don't have time right now to look through my program to fix all of my errors, but i will. And the reason i used so many ifs, was because i tried elsif and it didn't work, and i was like, wtf mayte? and yeah, but thanks guys.
Martin
Posted: Wed Apr 27, 2005 12:27 pm Post subject: (No subject)
In C++ it's else if, not elsif
wtd
Posted: Wed Apr 27, 2005 4:34 pm Post subject: (No subject)
It's always best to use braces ( { } ). Otherwise you can have confusion.
Also, though neither C nor C++ enforce the division, it's best to separate your code into functions and procedures. (Functions which return a value, and those which return main).
So, you'd have a procedure which reads the numbers from the user, a function/procedure which does the appropriate math, and a procedure which outputs the result.
A common way to handle this is with object-oriented programming. OOP involves grouping related pieces of data and the operations on that data, as well as inheritance and encapsulation.
So, let's look at a simple class for this purpose.
c++:
enum operation { ADD, SUB, MUL, DIV};
class basic_calculator
{ private:
int number1;
int number2;
protected:
int result;
public:
basic_calculator();
int get_number1()const;
int get_number2()const;
int get_result()const;
};
I know... whoa! What's all of that?
Well, we start with an enum, which lists all of the possible operations. This is a measure we take to make our code robust. If we just accept an integer and said 1 for addition, 2 for subtraction, etc. we'd be eft with the case of what to do with 5 or 6 or any other number not covered. With this an operation can only be one of those options.
Next we declare our "basic_calculator" class. "number1", "number2" will be private, so that only functions in the basic_calculator class can access them. "result" will be protected, so that anything which inherits from basic_calculator has access to it.
The rest is public. These are all member functions, and they're just declarations of those functions. We will later define how they actually work.
First off is the constructor which sets up the initial state of the object's variables (number1, number2, and result). It takes no arguments.
Next are functions to read in the numbers. As an argument, these take an input stream to read from. Also in this section is a procedure which does the actual math and modifies "result". It takes, as an argument, an operation to perform.
Next we have accessors which can retrieve each of the values involved, without changing those values. Since nothing is changed, we can specify that it's ok to call these functions on a constant basic_calculator.
void basic_calculator::read_number1(std::istream& in) {
in >> number1;
}
void basic_calculator::read_number2(std::istream& in) {
in >> number2;
}
void basic_calculator::do_math(operation o) { switch(o) { case ADD:
result = number1 + number2;
break;
case SUB:
result = number1 - number2;
break;
case MUL:
result = number1 * number2;
break;
caseDIV:
result = number1 / number2;
break;
} }
int basic_calculator::get_number1()const { return number1;
}
int basic_calculator::get_number2()const { return number2;
}
int basic_calculator::get_result()const { return result;
}
Now, where do we go from here?
Well, what if we want floating point results? We'd have to create a whole other calculator class, right? Wrong. We can use templates to make the basic_calculator class "generic".