Help please!

Posts 1–10 of 10 · Page 1 of 1
Help please!
Ok this is the source code for a simple division program, which doesnt work!!

Code:
#include <iostream>
using namespace std;

int main() {
    double result, n, d;
    cout << "Enter value: ";
    cin >> n;
    cout << "Enter divisor: ";
    cin >> d;
    if(d != 0) {
         cout << "d does not equal zero so division is OK" << "\n";
         cout << n << " / " << d << " is " << result;
         }
    cout << endl;
    system("pause");
    return 0;
}
No matter wat is the divisor or the value the answer always comes as 4.24399e-314, here are some screenies:



Quote Originally Posted by Zhhot View Post
Ok this is the source code for a simple division program, which doesnt work!!

Code:
#include <iostream>
using namespace std;

int main() {
    double result, n, d;
    cout << "Enter value: ";
    cin >> n;
    cout << "Enter divisor: ";
    cin >> d;
    if(d != 0) {
         cout << "d does not equal zero so division is OK" << "\n";
         cout << n << " / " << d << " is " << result;
         }
    cout << endl;
    system("pause");
    return 0;
}
No matter wat is the divisor or the value the answer always comes as 4.24399e-314, here are some screenies:



Nowhere did you assign a value to result, for like for example:
result = n/d;
You are just getting nonsense because you didn't give result a value, thus it's just showing random number.

So you should put "result = n/d;" after you have the input for both numerals.
Or you could write a function to do the math
yep you have to be careful to pay attention to little details while programming. Even a small program could have only taken five minutes to write can take an hour to debug if you don't know what ur looking for :L
thanks guys, im not completly fimiliar with the code blocks concept, sorry for posting such a nooby question
Quote Originally Posted by Zhhot View Post
thanks guys, im not completly fimiliar with the code blocks concept, sorry for posting such a nooby question
It's fine, it's what we are here for. Plus I really have nothing better to do
Code:
//Your Original Code
#include <iostream>
using namespace std;

int main() {
    double result, n, d;
    cout << "Enter value: ";
    cin >> n;
    cout << "Enter divisor: ";
    cin >> d;
    if(d != 0) {
         cout << "d does not equal zero so division is OK" << "\n";
         cout << n << " / " << d << " is " << result;
         }
    cout << endl;
    system("pause");
    return 0;
}
You need to put in what Result should equal. you are inserting a variable whose value hasnt been assigned to it.

heres what you should have

Code:
//Fixed Code
#include <iostream>
using namespace std;

int main() {
    double result, n, d;
    cout << "Enter value: ";
    cin >> n;
    cout << "Enter divisor: ";
    cin >> d;

    result = n / d;

    if(d != 0) {
         cout << "d does not equal zero so division is OK" << "\n";
         cout << n << " / " << d << " is " << result;
         }
    cout << endl;
    system("pause");
    return 0;
}
thanks!!!
you could also add this

if (d ==0) cout <<"d is zero so division is false\n";
by this i mean the code not the monkey
Also: I noticed that you used a variation of "/n" and "<< endl"
i recommend you pick one and stay with it throughout ur code. i recommend endl. it looks cleaner than an ugly little '/n' at the end of a sentence.
ya i think i'll stick with endl
Posts 1–10 of 10 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

Talk with us