Author Topic: Help me with my code.  (Read 3185 times)

0 Members and 1 Guest are viewing this topic.

Offline ManiK

  • /dev/null
  • *
  • Posts: 18
  • Cookies: 5
  • cout<<"Be your-self <<endl;
    • View Profile
Help me with my code.
« on: October 16, 2012, 02:47:16 pm »
Code: [Select]
#include <iostream>
#include <cmath>




int main()
{
    using namespace std;
   
    double answer;
    double option;
    double num1;
    double num2;
   
    do{
   
    cout << "Enter operator <1=add 2=subtract 3=multiply 4=divide>" << endl;
    cin >> option;
   
    if (option == 1)
       {
       
        cout << "Enter first number" << endl;
        cin >> num1;
        cout << "Enter second number" << endl;
        cin >> num2;   
        cout << "answer is" << endl;
        cout << num1 + num2 << endl;
        cout << endl;
}   
    else if (option == 2)
    {
        cout << "Enter first number" << endl;
        cin >> num1;
        cout << "Enter second number" << endl;
        cin >> num2;   
        cout << "answer is" << endl;
        cout << num1 - num2 << endl;
        cout << endl;
}   
    else if (option == 3)
    {
        cout << "Enter first number" << endl;
        cin >> num1;
        cout << "Enter second number" << endl;
        cin >> num2;   
        cout << "answer is" << endl;
        cout << num1 * num2;
        cout << endl;
    }   
    else if (option == 4)
       { cout << "Enter first number" << endl;
        cin >> num1;
        cout << "Enter second number" << endl;
        cin >> num2;   
        cout << "answer is" << endl;
        cout << num1 / num2 << endl;
        cout << endl;
   }   
   
   
   else{
       cout << "Enter the real operator you dingbat." << endl;
       }
} while(.1);




 
    system("pause");
    }


This is a basic calculator.I am still learning C++.This is a program i made with help from some tutorial.The problem is when i enter a option and use it smoothly .After that i need to clear the screen,it keeps stacking after using operators.I am not sure if u understood my problem.I tried using system("CLS") but i failed.Help me.

Thank you.

Offline relax

  • Sir
  • ***
  • Posts: 562
  • Cookies: 114
  • The one and only
    • View Profile
Re: Help me with my code.
« Reply #1 on: October 16, 2012, 02:59:30 pm »
try
include "stdlib.h"
when you do a system cls
another option might be to add multiple empty lines with a loop
« Last Edit: October 16, 2012, 03:03:05 pm by relax »

Offline blackboxx

  • /dev/null
  • *
  • Posts: 11
  • Cookies: 0
  • Programmer, Student
    • View Profile
Re: Help me with my code.
« Reply #2 on: October 16, 2012, 03:31:10 pm »
You should use #include <cstdlib> in order to use "system" actions, like system("PAUSE");, or system("CLS");. It should work if you include that.


On a side note, you may want to create functions for finding the sum, difference, product, and quotient to make your main program source code more neat, and structured.


For example:


Code: [Select]

#include<iostream>


double findSum(double n1, double n2)
{
    double sum = n1 + n2;
    return (sum);
}


int main()
{
    double num1, num2;


    std::cout << "Input 1st number:";
    std::cin >> num1;
    std::cout << std::endl;
    std::cout << "Input 2nd number:";
    std::cin >> num2;
    std::cout << std::endl;


    // Output
    std::cout << num1 << " + " << num2 << " = " << findSum(num1, num2);


    std::cin.ignore();
    return 0;
}

Offline ManiK

  • /dev/null
  • *
  • Posts: 18
  • Cookies: 5
  • cout<<"Be your-self <<endl;
    • View Profile
Re: Help me with my code.
« Reply #3 on: October 16, 2012, 06:48:18 pm »
try
include "stdlib.h"
when you do a system cls
another option might be to add multiple empty lines with a loop



Thank you .. i ll do that...system cls is working but it clears the screen as i enter option just tell me the right place to put it..

Offline ManiK

  • /dev/null
  • *
  • Posts: 18
  • Cookies: 5
  • cout<<"Be your-self <<endl;
    • View Profile
Re: Help me with my code.
« Reply #4 on: October 16, 2012, 06:50:09 pm »
You should use #include <cstdlib> in order to use "system" actions, like system("PAUSE");, or system("CLS");. It should work if you include that.


On a side note, you may want to create functions for finding the sum, difference, product, and quotient to make your main program source code more neat, and structured.


For example:


Code: [Select]

#include<iostream>


double findSum(double n1, double n2)
{
    double sum = n1 + n2;
    return (sum);
}


int main()
{
    double num1, num2;


    std::cout << "Input 1st number:";
    std::cin >> num1;
    std::cout << std::endl;
    std::cout << "Input 2nd number:";
    std::cin >> num2;
    std::cout << std::endl;


    // Output
    std::cout << num1 << " + " << num2 << " = " << findSum(num1, num2);


    std::cin.ignore();
    return 0;
}




I was not able to understand your code as i am in learning stage..i tried to compile and run your code but it seems like cin.get() isnt working..i dont know but why..anyways thanks for your help.

Offline relax

  • Sir
  • ***
  • Posts: 562
  • Cookies: 114
  • The one and only
    • View Profile
Re: Help me with my code.
« Reply #5 on: October 16, 2012, 07:19:31 pm »

Thank you .. i ll do that...system cls is working but it clears the screen as i enter option just tell me the right place to put it..

hmm i would put a pause at end of the do loop and a cls at the start
i woul also loop it with infinity and add a (q) option to quit
and i would use char insted of int in the option menu

pllaybuoy

  • Guest
Re: Help me with my code.
« Reply #6 on: October 17, 2012, 06:38:19 am »
just use
cin.get();
cin.get();
it will let you press enter twice to quit :)

Offline ande

  • Owner
  • Titan
  • *
  • Posts: 2664
  • Cookies: 256
    • View Profile
Re: Help me with my code.
« Reply #7 on: October 17, 2012, 06:52:19 am »
Okay, this is crap. Your code is a mess and you have a using namespace std inside your main()...

This should work:
Code: (c++) [Select]
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double answer, option, num1, num2;
   
    do {
cout << "Enter operator\r\n0=exit\r\n1=add\r\n2=subtract\r\n3=multiply\r\n4=divide>" << endl;
cin >> option;

if (option == 1)
{
cout << "Enter first number" << endl;
cin >> num1;
cout << "Enter second number" << endl;
cin >> num2;   
cout << "answer is" << endl;
cout << num1 + num2 << endl;
cout << endl;
}   
else if (option == 2)
{
cout << "Enter first number" << endl;
cin >> num1;
cout << "Enter second number" << endl;
cin >> num2;   
cout << "answer is" << endl;
cout << num1 - num2 << endl;
cout << endl;
}   
else if (option == 3)
{
cout << "Enter first number" << endl;
cin >> num1;
cout << "Enter second number" << endl;
cin >> num2;   
cout << "answer is" << endl;
cout << num1 * num2;
cout << endl;
}   
else if (option == 4)
   { cout << "Enter first number" << endl;
cin >> num1;
cout << "Enter second number" << endl;
cin >> num2;   
cout << "answer is" << endl;
cout << num1 / num2 << endl;
cout << endl;
   }   
   else
   {
   cout << "Enter the real operator you dingbat." << endl;
}

} while(option != 0);

cout << "Hit any key to exit";
cin.get();
}
if($statement) { unless(!$statement) { // Very sure } }
https://evilzone.org/?hack=true

Offline Super_mario666

  • Knight
  • **
  • Posts: 160
  • Cookies: 7
  • Professional Badass
    • View Profile
Re: Help me with my code.
« Reply #8 on: October 17, 2012, 07:15:49 am »
here is a revised version. study it. and learn from it


Code: (cpp) [Select]

#include <iostream>
#include <conio.h>


using namespace std;


void add(int,int);              // This is a function prototype. it lets the compiler
                                // know your gonna use this function later.   


void subtract(int, int);        // You should tried to make the code more modular.     
void multiply(int, int);        // Try to put some of the operations in 'main' into       
void divide(int,int);           // it own function.


int main()
{


        char repeat;            // use this for the 'while' loop so the user chooses when exit the loop
        double answer;
        int option;
        double num1;
        double num2;


        do{


        cout << "Enter operator <1=add 2=subtract 3=multiply 4=divide>" << endl;
        cin >> option;
        while(option <= 0 || option > 4) // use a 'while' loop to make sure user doesn't type crap
             {
                 cout<< "Enter the real operator you dingbat. " << endl;
                 cin >> option;
             }
        cout << "Enter first number" << endl;
        cin >> num1;
        cout << "Enter second number" << endl;
        cin >> num2;


        switch(option)                   // you should also learn to use the 'switch' statement rather than a bunch of 'if/else'
        {
            case 1:
                    add(num1,num2);      // This is a function call. unless you call the function in 'main' it won't run
                    break;


            case 2:
                    subtract(num1,num2); // Variables defined in 'main' can't be used in other function so
                    break;               // pass them in as arguments.
            case 3:
                    multiply(num1,num2);
                    break;
            case 4:
                    divide(num1,num2);
                    break;


           default:
                    break;
        }


        cout << "Would you like another go?[Y/n] : ";
        cin >> repeat;
        while (repeat != 'y' && repeat != 'Y' && repeat != 'n' && repeat != 'N') //to make sure the user types either 'y' or 'n'
              {
                  cout << "Please choose [Y/n]: ";
                  cin >> repeat;
              }


} while(repeat == 'y' || repeat == 'Y');




    getch();   // the same as 'cin.get()' is in 'conio.h'
    return 0;
    }






void add(int num1,int num2)        // Here is where you define the function   
{
       cout << "answer is" << endl;
       cout << num1 + num2 << endl;
       cout << endl;
}


void subtract(int num1, int num2)  // When a value is passed from 'main' you must redeclare it in the parenthesis
{
        cout << "answer is" << endl;
        cout << num1 - num2 << endl;
        cout << endl;
}


void multiply(int num1, int num2)  // You can choose what the function should return to 'main'
{                                  // 'void' means it doesn't return any value. 'int' means it returns an integer
        cout << "answer is" << endl;
        cout << num1 * num2;
        cout << endl;
}


void divide(int num1,int num2)
{
        cout << "answer is" << endl;
        cout << num1 / num2 << endl;
        cout << endl;
}
« Last Edit: October 17, 2012, 07:19:06 am by Super_mario666 »
The Bigger they are...The more likely you'll get your ass kicked

Offline ManiK

  • /dev/null
  • *
  • Posts: 18
  • Cookies: 5
  • cout<<"Be your-self <<endl;
    • View Profile
Re: Help me with my code.
« Reply #9 on: October 17, 2012, 10:06:55 am »
Code: [Select]
#include <iostream>
#include <cmath>
#include <stdlib.h>






int main()
{
    using namespace std;
   
    double answer;
    double option;
    double num1;
    double num2;
   
    do{
             
   
    cout << "Enter operator <1=add 2=subtract 3=multiply 4=divide>" << endl;
    cin >> option;
   
    if (option == 1)
       {
       
        cout << "Enter first number" << endl;
        cin >> num1;
        cout << "Enter second number" << endl;
        cin >> num2;   
        cout << "answer is" << endl;
        cout << num1 + num2 << endl;
        cout << endl;
        system("pause");
        system("CLS");
}   
    else if (option == 2)
    {
        cout << "Enter first number" << endl;
        cin >> num1;
        cout << "Enter second number" << endl;
        cin >> num2;   
        cout << "answer is" << endl;
        cout << num1 - num2 << endl;
        cout << endl;
        system("pause");
        system("CLS");
}   
    else if (option == 3)
    {
        cout << "Enter first number" << endl;
        cin >> num1;
        cout << "Enter second number" << endl;
        cin >> num2;   
        cout << "answer is" << endl;
        cout << num1 * num2;
        cout << endl;
        system("pause");
        system("CLS");
    }   
    else if (option == 4)
       { cout << "Enter first number" << endl;
        cin >> num1;
        cout << "Enter second number" << endl;
        cin >> num2;   
        cout << "answer is" << endl;
        cout << num1 / num2 << endl;
        cout << endl;
        system("pause");
        system("CLS");
   }   
   
   
   else{
       cout << "Enter the real operator you dingbat." << endl;
       }
} while(.1);








 
    system("pause");
    system("CLS");
    }


Thank you guys for your support and help. ;)


\Problem solved