Page 2 of 2

Re: need ideas

Posted: Sun Oct 25, 2009 4:40 pm
by dandymcgee
killercoder wrote: I can't seem to figure out whats wrong with this code :(. it wont pause after you give you answer to last question....

P.S. It's a nooby mistake I bet lol.

Code: Select all

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
    int x = 0;
    int c = x + 5;
    string name;
    int answer1;
    cout << "what would you like your name to be?" << endl;
    cout << ">"; cin >> name;
    cout << "hello nice to meet you " << name << endl;
    cin.get();
    cout << "what is your favorite number " << name << endl;
    cout << ">"; cin >> x;
    cout << "Your favorite number is " << x << endl;
    cin.get();
    cout << "what is your favorite number +5?" << endl;
    cout << ">"; cin >> c;
    if( c != x+5 )
        cout << "Wrong@@@@" << endl;
    else if( c = x+5 )
        cout << "Correct!!" << endl;
    cin.get();
}
Try that.

Re: need ideas

Posted: Sun Oct 25, 2009 5:02 pm
by killercoder
it still doesn't pause after you answer the last question :(

Re: need ideas

Posted: Sun Oct 25, 2009 5:02 pm
by andrew

Code: Select all

else if( c = x+5 )
That's a bad bug, watch out for those.

This is what you wanted:

Code: Select all

else if( c == x+5 )

Re: need ideas

Posted: Sun Oct 25, 2009 5:05 pm
by killercoder
thank you soooooooooooooooo much !!!!!!!!!!! :)

Re: need ideas

Posted: Sun Oct 25, 2009 5:06 pm
by short
killercoder wrote:thank you soooooooooooooooo much !!!!!!!!!!! :)
Do you actually understand why his change works? It's important that you do.

Re: need ideas

Posted: Sun Oct 25, 2009 5:08 pm
by killercoder

Code: Select all

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
    int x = 0;
    int c = x + 5;
    string name;
    int answer1;
    cout << "what would you like your name to be?" << endl;
    cout << ">"; cin >> name;
    cout << "hello nice to meet you " << name << endl;
    cin.get();
    cout << "what is your favorite number " << name << endl;
    cout << ">"; cin >> x;
    cout << "Your favorite number is " << x << endl;
    cin.get();
    cout << "what is your favorite number +5?" << endl;
    cout << ">"; cin >> c;
    if( c != x+5 )
        cout << "Wrong@@@@" << endl;
    else if( c == x+5 )
        cout << "Correct!!" << endl;
    cin.get();
}
this is seems correct but its not working i want it to puase after it says if your correct or not but it doesnt :(

Re: need ideas

Posted: Sun Oct 25, 2009 5:09 pm
by killercoder
if I have a bug like that won't it cause a memory leak or something?

and that just says c is equal to x+5 not asking the question if it actually is equal to x+5

Re: need ideas

Posted: Sun Oct 25, 2009 5:26 pm
by andrew
The reason that piece of code is a bug is the '=' operator is actually the assignment operator. The '==' operator is used to test for equality. It's a very common error that the compiler doesn't catch, unless you have the warning level set to the highest.

Re: need ideas

Posted: Sun Oct 25, 2009 5:39 pm
by zeid
if I have a bug like that won't it cause a memory leak or something?
No, memory leaks only occur when you dynamically allocate memory. You wont encounter them for a fair while until you are past understanding the basics.