Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Please help fix my program :)
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Cancer Sol




PostPosted: Thu Mar 07, 2013 2:55 pm   Post subject: Please help fix my program :)

Please help fix my program. I use Code::Blocks (GNU-GCC Compiler).
c++:

#include <iostream>
#include <string>
using namespace std;
int main ()
{
    cout << "Welcome Edward Yang's slope calculator." << endl;
    cout << "Choose an option:" << endl;
    cout << "Option 1 will calculate the slope using m=rise/run." << endl;
    cout << "Option 2 will calculate the slope using m=(y2-y1)/(x2-x1)" << endl;
    int option;
    cout << "Enter the option here: ";
    cin >> option;
    if ( option = 1 )                 //Var rise, rune, slope
        {
            cout << "Please enter the rise and the run." << endl;
            double rise, run;
            cout << "Please enter the rise here: ";
            cin >> rise;
            cout << "Please enter the run here: ";
            cout >> run;
            cout << "m=rise/run" << endl;
            cout << "m=" << rise << "/" << run << endl;
            double slope = rise / run;
            cout << "m=" << slope << endl;
            cout << "The slope is " << slope << "." << endl;
        else if ( option = 2 )     //Var y2, y1, x2, x1, m
            cout << "Please enter the value of y2,y1,x2, and x1." << endl;
            double y2, y1, x2, x1, m;
            m= (y2 - y1) / (x2 - x1);
            cout << "Enter the value of y2 here: ";
            cin >> y2;
            cout << "Enter the value of y1 here: ";
            cin >> y1;
            cout << "Enter the value of x1 here: ";
            cin >> x1;
            cout << "m=(y2-y1)/(x2-x1)" << endl;
            cout << "m=(" << y2 << "-" << y1 << ")/(" << x2 << "-" << x1 << ")" << endl;
            cout << "m=" << y2 - y1 << "/" << x2 - x1 << endl;
            cout << "m=" << m;
            cout << "The slope is " << m << "." << endl;
        else
            cout << option << " is not a valid optioin.";
        }
}


Umm... sorry, how do I show the c++ syntax on my post? I thought that's how I'm supposed to do it..
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Thu Mar 07, 2013 3:14 pm   Post subject: RE:Please help fix my program :)

You forgot to tell us what's wrong with it. Does it compile? Does it do the wrong thing? When and how does it do the wrong thing? What have you tried to solve it?

You should also remember that a single equals sign (=) is assignment, and two (==) is comparison. You want to use comparison in your if statements, but you are using assignment, which will not do what you want.
Zren




PostPosted: Thu Mar 07, 2013 3:21 pm   Post subject: RE:Please help fix my program :)

It's syntax="cpp" btw.
Tony




PostPosted: Thu Mar 07, 2013 3:26 pm   Post subject: Re: RE:Please help fix my program :)

DemonWasp @ Thu Mar 07, 2013 3:14 pm wrote:
You want to use comparison in your if statements, but you are using assignment, which will not do what you want.

Btw, a compiler will warn you about such mistakes.
Quote:

$ cat /tmp/test.c
int main(){
int i=0;
if(i=1){
return 1;
} else {
return 0;
}
}

$ gcc -Wall /tmp/test.c
/tmp/test.c: In function ?main?:
/tmp/test.c:3: warning: suggest parentheses around assignment used as truth value
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Cancer Sol




PostPosted: Thu Mar 07, 2013 3:32 pm   Post subject: Re: RE:Please help fix my program :)

DemonWasp @ 3/7/2013, 3:14 pm wrote:
You forgot to tell us what's wrong with it. Does it compile? Does it do the wrong thing? When and how does it do the wrong thing? What have you tried to solve it?

You should also remember that a single equals sign (=) is assignment, and two (==) is comparison. You want to use comparison in your if statements, but you are using assignment, which will not do what you want.


The compiler won't compile it I think, I'll have to post the error after when I get back home. Thanks though, everyone!
DemonWasp




PostPosted: Thu Mar 07, 2013 5:57 pm   Post subject: Re: RE:Please help fix my program :)

Tony @ Thu Mar 07, 2013 3:26 pm wrote:
DemonWasp @ Thu Mar 07, 2013 3:14 pm wrote:
You want to use comparison in your if statements, but you are using assignment, which will not do what you want.

Btw, a compiler will warn you about such mistakes.


Yes! Good point, and one I should repeat in every thread about C++ problems. If you are using g++ or gcc, then you should be using at least -Wall -Wextra -Werror , as that will turn on all warnings and prevent you from compiling without fixing them. This will save you TONS of trouble later on. There are equivalents for other compilers, but I don't know them offhand.

Edit: The program posted by Cancer Sol is also misusing curly braces (these: {}). You have to put them in pairs, and you have to use them to group statements. For example:

c++:

if ( condition ) {
    statement1;
    statement2;
} else if ( other_condition ) {
    statement3;
    statement4;
} else {
    statement 5;
}
Cancer Sol




PostPosted: Thu Mar 07, 2013 7:20 pm   Post subject: Re: RE:Please help fix my program :)

DemonWasp @ 3/7/2013, 5:57 pm wrote:
Tony @ Thu Mar 07, 2013 3:26 pm wrote:
DemonWasp @ Thu Mar 07, 2013 3:14 pm wrote:
You want to use comparison in your if statements, but you are using assignment, which will not do what you want.

Btw, a compiler will warn you about such mistakes.


Yes! Good point, and one I should repeat in every thread about C++ problems. If you are using g++ or gcc, then you should be using at least -Wall -Wextra -Werror , as that will turn on all warnings and prevent you from compiling without fixing them. This will save you TONS of trouble later on. There are equivalents for other compilers, but I don't know them offhand.

Edit: The program posted by Cancer Sol is also misusing curly braces (these: {}). You have to put them in pairs, and you have to use them to group statements. For example:

c++:

if ( condition ) {
    statement1;
    statement2;
} else if ( other_condition ) {
    statement3;
    statement4;
} else {
    statement 5;
}


Well I think I found out the problem, but for the tut I'm using, I think that's how it was used though. Sec, I'll just go check again. Thanks for all the help though.
Okay actually.. yeah, I'm missing a hell lot of curly braces. LOL.
Fixed.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: