Cant figure it out...

This is a discussion on Cant figure it out... within the C++ Programming forums, part of the General Programming Boards category; I recently took a break from programming(like a month, month an a half maybe). So as a little warmup to ...

  1. #1
    Codigious FingerPrint's Avatar
    Join Date
    Mar 2006
    Posts
    60

    Cant figure it out...

    I recently took a break from programming(like a month, month an a half maybe). So as a little warmup to get back in the habit of programming, I made this small program. You just chose a category(addition, subtraction, mulitiplication, or division) and then enter 2 numbers. The 2 numbers are manipulated differently, depending on which category you chose. Then it gives you the answer and you go back to the main menu.

    Its a really simple program, but for some reason its not working properly. It compiles just fine. But after you select one of the categories it just closes the program. I dont see why though. But I bet its something simple im overlooking.

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    
    void add(){
         
         int x;
         int y;
         int result;
         result = x + y;
         
         cout << " Enter a number: ";
         cin >> x;
         cin.get();
         cout << "\n\n Enter another number: ";
         cin >> y;
         cin.get();
         cout << " " << x << " + " << y << " = " << result;
         cin.get();
    
         cout << "\n\n\n";
         }
         
    void sub(){
         
         int x;
         int y;
         int result;
         result = x - y;
         
         cout << " Enter a number: ";
         cin >> x;
         cin.get();
         cout << "\n\n Enter another number: ";
         cin >> y;
         cin.get();
         cout << " " << x << " - " << y << " = " << result;
         cin.get();
    
         cout << "\n\n\n";
         }
         
    void mult(){
         
         int x;
         int y;
         int result;
         result = x * y;
         
         cout << " Enter a number: ";
         cin >> x;
         cin.get();
         cout << "\n\n Enter another number: ";
         cin >> y;
         cin.get();
         cout << " " << x << " * " << y << " = " << result;
         cin.get();
    
         cout << "\n\n\n";
         }
         
    void div(){
         
         int x;
         int y;
         int result;
         result = x/y;
         
         cout << " Enter a number: ";
         cin >> x;
         cin.get();
         cout << "\n\n Enter another number: ";
         cin >> y;
         cin.get();
         cout << " " << x << " / " << y << " = " << result;
         cin.get();
    
         cout << "\n\n\n";
         }
         
                                
    int main(){
        
        int lesson;
         
         cout << "\n\n\n";
         cout << " Welcome to Math Fun Lessons 1 - 4 \n\n\n";
         cout << " Please select a lesson.\n\n";
         cout << " 1. Addition \n";
         cout << " 2. Subtraction \n";
         cout << " 3. Multiplication \n";
         cout << " 4. Division \n\n";
         cout << " Lesson: ";
         cin >> lesson;
         if(lesson == 1){
                   cout << "\n\n";
                   void add();
                   }
         else if(lesson == 2){
              cout << "\n\n";
              void sub();
              }
         else if(lesson == 3){
              cout << "\n\n";
              void mult();
              }
         else if(lesson == 4){
              cout << "\n\n";
              void div();
              }
         else{
              cout << " Invalid Syntax. Try again.\n\n";
              }
        }
    Where the IF statement is at the end, that was originally a switch case. But since it wasnt working I figured I did the switch case incorrectly, so I changed it to an IF.
    Code:
    /* ------------------------------------------------------------------*/
                               // INSERT CODE HERE
    /* ------------------------------------------------------------------*/

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Also, you seem to have become confused about the idea of an expression - take this piece:

    Code:
    void add(){
         
         int x;
         int y;
         int result;
         result = x + y;
    That red line calculates the sum of x and y, and stores the answer in result, based on the current values of x and y - it does not automatically update the result at any time after this line of code, i.e. when you enter values for x and y. You need to move this line to after the lines where you read in values and then print the result. You'll have to do that for each of your functions.

    >>It compiles just fine.

    You need to turn up the warning level on your compiler - you should get warnings about x and y being used before being initialised - x and y are not guaranteed to be any value, and will probably contain whatever junk value was in that memory location before the program started.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    > You need to turn up the warning level on your compiler - you should get warnings about x and y being used before being initialised

    Not with g++ 3.4.5 (MinGW)
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    74
    Also, when calling functions you simply write:

    Code:
     
    if(lesson == 1)
    {
       cout << "\n\n";
       add();
    }
    rather than

    Code:
     
    if(lesson == 1)
    {
       cout << "\n\n";
       void add();
    }
    If you do the second as you are doing it never calls the function.

    --------------------------------------------------------------------
    Also, not sure if you know, but you can have functions accept values and return values to simplify 'em and reduce code redundancy, which may result in more errors and difficulty changing your code at a later time. Of course this isn't necessary as your code should work just as well as is. Just providing an alternative example.

    Ex:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int add(int x, int y)
    {
      return x + y;
    }
    
    int sub(int x, int y)
    {
      return x - y;
    }
    
    int mult(int x, int y)
    {
      return x * y;
    }
    
    int div(int x, int y)
    {
      return x / y;
    }
                                
    int main()
    {
      int lesson;
      int x, y;
         
      cout << "\n\n\n";
      cout << " Welcome to Math Fun Lessons 1 - 4 \n\n\n";
      cout << " Please select a lesson.\n\n";
      cout << " 1. Addition \n";
      cout << " 2. Subtraction \n";
      cout << " 3. Multiplication \n";
      cout << " 4. Division \n\n";
      cout << " Lesson: ";
      
      cin >> lesson;
           
      cout << " Enter a number: ";
      cin >> x;
      cin.get();
      cout << "\n\n Enter another number: ";
      cin >> y;
      cin.get();
      cout << "\n\n\n";
         
      if(lesson == 1)
      {
        cout << " " << x << " + " << y << " = " << add(x, y);
      }
      else if(lesson == 2)
      {
       cout << " " << x << " - " << y << " = " << sub(x, y);
      }
      else if(lesson == 3)
      {
       cout << " " << x << " * " << y << " = " << mult(x, y);
      }
      else if(lesson == 4)
      {
       cout << " " << x << " / " << y << " = " << div(x, y);
      }
      else
      {
       cout << " Invalid Syntax. Try again.\n\n";
      }
    }
    Last edited by Kurisu33; 09-18-2006 at 04:58 PM.

  5. #5
    Codigious FingerPrint's Avatar
    Join Date
    Mar 2006
    Posts
    60
    I figured it was something pretty simple.

    Richie T, the reason I have it like that is because Ive used( result = x + y; ) before and it worked. Wel, worked in that spot.

    >>>If you do the second as you are doing it never calls the function.

    I have to do the second one. I get erros when I do it without the void.


    And about the alternative you suggested. That was my original idea but I couldnt remember how to do it lol(ive only used that format like 2-3 times before). Oh, and its all better now. Thanks.
    Code:
    /* ------------------------------------------------------------------*/
                               // INSERT CODE HERE
    /* ------------------------------------------------------------------*/

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I have to do the second one. I get erros when I do it without the void.
    Then you've got something else wrong.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Next you should add a differentiation and calculus lesson

    Sorry, I am differentiatingly mad at the mo as I am doing it in school at the moment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Figure out how method was called?
    By jcafaro10 in forum C++ Programming
    Replies: 2
    Last Post: 02-07-2009, 09:43 AM
  2. 3 dimensional figure volume
    By thekautz in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2009, 04:22 PM
  3. trying to figure out someone's code for a plugin
    By paulpars in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2006, 10:57 AM
  4. newb to C, cant figure something out.
    By Phate4219 in forum C Programming
    Replies: 16
    Last Post: 03-06-2006, 12:47 AM
  5. ahh i can't figure out this loop
    By blindleaf in forum C Programming
    Replies: 1
    Last Post: 03-18-2003, 08:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21