Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Please help me /prog/

Name: Anonymous 2007-11-11 22:10

I have this due for my class tomorrow and I can't figure out why this wont work. When it prompts the user to input something for the "selection" variable it completely ignores what is put into it and just goes right to the addition function. I know it's a problem with the if area, but I can't find anything wrong with my limited knowledge. Please help.

#include <cstdlib>
#include <iostream>

using namespace std;

int multiplication(int a, int b);
int division(int a, int b);
int addition(int a, int b);
int subtraction(int a, int b);


int main()
{
    int answer;
    int selection;
    int a;
    int b;
    cout << "Please enter a positive number: ";
    cin >> a;
    cout << "\n\nPlease enter another positive number: ";
    cin >> b;
    cout << "\n\nDo you want to add, subtract, multiply, or divide?";
    cout << "\n\nPress enter 1 for addation, 2 for subtraciton, 3 for";
    cout << "\nmultiplication, or 4 for division." << endl;
    cin >> selection;
    if (selection = 1)
    {
                 answer = addition(a,b);
                 cout << "\n\nThe answer is: " << answer << endl;
                 char response;
                 cin >> response;
                 return 0;
   
    if (selection = 2)
                 answer = subtraction(a,b);
                 cout << "\n\nThe answer is: " << answer << endl;
                 char response2;
                 cin >> response2;
                 return 0;
                
    if (selection = 3)
                 answer = multiplication(a,b);
                 cout << "\n\nThe answer is: " << answer << endl;
                 char response3;
                 cin >> response3;
                 return 0;
    
    if (selection = 4)
                 answer = division(a,b);
                 cout << "\n\nThe answer is: " << answer << endl;
                 char response4;
                 cin >> response4;
                 return 0;
                 }
    }
   
  


int addition(int a, int b)
{
      int c;
      return c = a + b;
      }
     
int subtraction(int a, int b)
{
      int c;
      return c = a - b;
      }

int multiplication(int a, int b)
{
      int c;
      return c = a * b;
      }
     
int division(int a, int b)
{
      int c;
      return c = a / b;
      }

Name: Anonymous 2007-11-11 22:47

Name: Anonymous 2007-11-11 23:08

>>2
get out now

Name: Anonymous 2007-11-11 23:19

>>1
You gotta be a troll... FUCKING HELL

Name: Anonymous 2007-11-11 23:25

>>1
your code makes me cry.  why do you want me to cry?

Name: Anonymous 2007-11-11 23:32

>>4
>>5

Well, in my defense I am a beginner.

I know I could have left out the functions, but the assignment was to incorporate 4 functions, so I did.

Name: Anonymous 2007-11-11 23:48

>>3
YHBT. YHL. HAND.

Name: Anonymous 2007-11-11 23:57

Hahahaha, your code made me smile. Ok, I don't code in C, but I guess if you closed your ifs with "}"s it should work better.

Name: Anonymous 2007-11-11 23:59

>>8

I tried that originally, but it still didn't work. So I then looked in the book I'm using to supplement the class and theres a bunch of if functions in there without them after every if.

Name: Anonymous 2007-11-12 0:16

= != ==

Name: Anonymous 2007-11-12 0:26

>>10

Oh shit, now I see it. I love you anon.

Name: Anonymous 2007-11-12 1:01

"\n"

I THINK YOU MEAN endl THERE, DON'T YOU?

"if (selection = 1)"

DISCOUNT PROGRAMMERS ARE MADE OF DISCOUNTS.

ALSO, HOW DID I switch(selection)

int c;
return c = a + b;

LOL DECLARING USELESS VARIABLES LOL

ALSO, WAY TO AVOID ERROR HANDLING. HEY, WHAT IS "zomg" DIVIDED BY "rofl"?

Name: Anonymous 2007-11-12 2:05

>>12
"\n"
I THINK YOU MEAN endl THERE, DON'T YOU?
shut the fuck up junior programmer.
I think you mean std::endl.

Name: Anonymous 2007-11-12 3:00

using namespace std;
Learn2read

Name: Anonymous 2007-11-12 3:34

What kind of ghetto-ass compiler doesn't warn about = in an if statement?

Name: Anonymous 2007-11-12 3:47

Sorry /prog/, I'm feeling way too fucking nice tonight to even bother trolling (or counter-trolling).

>>1
Your code is a piece of shit; your indentation style is archaic and unreadable, and ultimately the cause of your frustration. There were some other serious design and syntactic issues with your "program", but I'm too fucking lazy to hold your hand.

Anyway, here's your fixed code, compiled and tested on GCC:

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int multiplication(int a, int b);
int division(int a, int b);
int addition(int a, int b);
int subtraction(int a, int b);

int main()
{
    int answer, selection, a, b;
    cout << "Please enter a positive number: ";
    cin >> a;
    cout << "\n\nPlease enter another positive number: ";
    cin >> b;
    cout << "\n\nDo you want to add, subtract, multiply, or divide?";
    cout << "\n\nPress enter 1 for addation, 2 for subtraciton, 3 for";
    cout << "\nmultiplication, or 4 for division." << endl;
    cin >> selection;

    if (selection == 1)
         answer = addition(a,b);
    else if (selection == 2)
         answer = subtraction(a,b);
    else if (selection == 3)
         answer = multiplication(a,b);
    else if (selection == 4)
         answer = division(a,b);    
    else {
        cout << "Invalid choice." << endl;
        return 1;
    }

     cout << "\n\nThe answer is: " << answer << endl;
     return 0;
}
  
int addition(int a, int b)
{
    int c;
    return c = a + b;
}
    
int subtraction(int a, int b)
{
    int c;
    return c = a - b;
}

int multiplication(int a, int b)
{
    int c;
    return c = a * b;
}
    
int division(int a, int b)
{
    int c;
    return c = a / b;
}

Enjoy failing your exams this December because you don't have a fucking clue about the basics of C++ syntax, and probably won't even bother to learn because shitheads like me do your homework while waiting for their ports tree to finish updating.

Name: Anonymous 2007-11-12 3:55


#define division(x,y) ((x)/(y))
#define multiplication(x,y) ((x)*(y))
#define substraction(x,y) ((x)-(y))
#define addition(x,y) ((x)+(y))

No need for functions

Name: Anonymous 2007-11-12 4:56

>>17
You can benifit from implicit casting
example:

in the code >>16 posted, addition(2.4, 1) would return 3
With the defines, addition(2.4, 1) would evaluate to 3.4
etc.

Name: Anonymous 2007-11-12 5:02

>>1
Two gods, one true brace style

(Also, three gods, one true faith, props to the huge faggot who knows where's this from.)

Name: Anonymous 2009-08-16 22:38

Lain.

Don't change these.
Name: Email:
Entire Thread Thread List