combining

Discussion in 'C++' started by shizzle08, Aug 7, 2009.

  1. shizzle08

    shizzle08 New Member

    Joined:
    Aug 7, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    how can i combine this 2 c++ in to 1 c++ prog thx for the help

    the first one

    Code:
    #include <iostream>
    #include <conio.h>
    using namespace std;
    main ()
    {
         int a,b,sum,dif,prod,quot;
         cout << " \n Enter the First number: ";
         cin >> a;
         cout << " \n Enter the Second number: ";
         cin >> b;
         sum = a + b;
         dif = a - b;
         prod = a * b;
         quot = a / b;
         cout << " \n The sum is: "<< sum ;
         cout << " \n The difference: "<< dif ;
         cout << " \n The Product is: "<< prod;
         cout << " \n The quotient is: "<< quot;
         cout << " \n \n \n ";
         return main();
         getch();
    }
         
    
    and this one

    Code:
    #include <iostream>
    #include <conio.h>
    using namespace std;
    main()
    {
          int a, b;
          char response;
          cout << "*************************************** \n";
          cout << " \t INPUT THE DAY NUMBER: " ;
          cin >> a;
          if (a==1)
          cout << " \t THE DAY IS MONDAY\n " ;
          if (a==2)
          cout << " \t THE DAY IS TUESDAY\n " ;
          if (a==3)
          cout << " \t THE DAY IS WEDNESDAY\n " ;
          if (a==4)
          cout << " \t THE DAY IS THURSDAY\n " ;
          if (a==5)
          cout << " \t THE DAY IS FRIDAY\n " ;
          if (a==6)
          cout << " \t THE DAY IS SATURDAY\n " ;
          if (a==7)
          cout << " \t THE DAY IS SUNDAY\n " ;
          return main();
          getch();
    }
          
    
    into one prog
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Put them into separate function and use the user to select which one to be executed.
     
  3. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    @shizzle08 :

    Why do both your programs end with "return main();" ?
    That will let your program fall into an infinite loop and you have to manually break your program's execution.

    If you wanted to repeat the portion inside main(), then you can use some other techniques like while(<some condition>) or for(<something else>) etc... and always return 0;
     
  4. shizzle08

    shizzle08 New Member

    Joined:
    Aug 7, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    i dont have any idea what your telling can u please edit my prog..? i newbie
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    (1) Put both main() functions into a new file.
    (2) Rename both main() functions to something appropriate.
    (3) Write a new main() function to call the two functions in whatever order you see fit.

    Saswat is talking about this line of code:
    Code:
      return main();
    
    This is wrong. I don't know where you got the idea that you should include the function name in a return statement. A return statement should return no value at all, i.e.
    Code:
      return;
    
    OR should return some suitable value:
    Code:
    int func() // returns an integer
    {
      int x;
      // ...some code that calculates x...
      return x;
    }
    
    A return statement isn't necessary at the end of a function as the function will return once it hits the closing brace anyway:
    Code:
    void func()
    {
      // ...some code...
      return; // this line is pointless
    
      // (unless it was a conditional return, and you have more code here)
    }
    
     
  6. shizzle08

    shizzle08 New Member

    Joined:
    Aug 7, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    this is how it should be sir...

    then when they chose 1 the basic calculator must be seem lyk this

    and then it will ask them

     
  7. shizzle08

    shizzle08 New Member

    Joined:
    Aug 7, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    sorry for double posting i can't see the edit button
    @sir xpi0t0s
    can u make me a c++ that has basic calculator,number of days. so that i cant see how to do it thanks
     
  8. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    No, the idea of you being given assignments is that you learn by figuring this stuff out for yourself, using the course notes you've already been given. All you need to solve this is some input, output and some if statements, all of which you already know from the code you've already written. So start by displaying the menu selections, reading an option from the user and displaying it back to them. Make sure that works, then start adding code to process that input; if it equals 1 then call a function that reads the values from the user, processes them and prints some output. Make sure that works OK, then add more code to handle the other options.
     
  9. shizzle08

    shizzle08 New Member

    Joined:
    Aug 7, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Finish.:)) one last thing after they use one of the following a question will prompt asking if they want to use it again? 'Y' or 'N' and if 'Y' then it will return to Int Main () if 'N' it will close? hehe

    PHP:
    #include <iostream>
    #include <conio.h>
    using namespace std;

    void Calc();
    void Days();
    void Post();
    void Circle();
    void Rectangle();
    void Square();
    void Sphere();

    int main()
    {
        
    int k;

        
    cout << "1 > Basic Calculator." << endl;
        
    cout << "2 > Number of Days." << endl;
        
    cout << "3 > Postive/ Negative." << endl;
        
    cout << "4 > Area and Cirumference of a Circle." << endl;
        
    cout << "5 > Area of Rectangle." << endl;
        
    cout << "6 > Area Square." << endl;
        
    cout << "7 > Volume of Sphere." << endl;

        
    cin >> k;    

        switch(
    k)
        {
            case 
    1:
                
    Calc();
            case 
    2:
                
    Days();
            case 
    3:
                
    Post();
            case 
    4:
                
    Circle();
            case 
    5:
                
    Rectangle();
            case 
    6:
                
    Square();
            case 
    7:
                
    Sphere();
            default:
                
    cout << "Wrong input" << endl;     
        }

        return 
    0;
    }

    void Calc()
    {
    int a,b,sum,dif,prod,quot;
    cout << " \n Enter the First number: ";
    cin >> a;
    cout << " \n Enter the Second number: ";
    cin >> b;
    sum b;
    dif b;
    prod b;
    quot b;
    cout << " \n The sum is: "<< sum ;
    cout << " \n The difference: "<< dif ;
    cout << " \n The Product is: "<< prod;
    cout << " \n The quotient is: "<< quot;
    cout << " \n \n \n ";
    getch();


    void Days()
    {
    int ab,;
    cout << "*************************************** \n";
    cout << " \t INPUT THE DAY NUMBER: " ;
    cin >> a;
    if (
    a==1)
    cout << " \t THE DAY IS MONDAY\n " ;
    if (
    a==2)
    cout << " \t THE DAY IS TUESDAY\n " ;
    if (
    a==3)
    cout << " \t THE DAY IS WEDNESDAY\n " ;
    if (
    a==4)
    cout << " \t THE DAY IS THURSDAY\n " ;
    if (
    a==5)
    cout << " \t THE DAY IS FRIDAY\n " ;
    if (
    a==6)
    cout << " \t THE DAY IS SATURDAY\n " ;
    if (
    a==7)
    cout << " \t THE DAY IS SUNDAY\n " ;
    getch();
    }  

    void Post()
    {
    int a,rush;
    cout <<"************************************\n";
    cout << "\t \"Enter a number\": ";
    cin >> a;
    rush a%2;
    if (
    a<0)
    cout << "\t \"the number is negative\" \n";
    else
    if (
    a>0)
    cout << "\t \"the number is positive\" \n";
    if (
    rush==0)
    cout << "\t \"the number is even\" \n";
    else
    cout << "\t \"the number is odd\" \n";
    getch();
    }

    void Circle()
    {
          
    float a,c,pi,r;
          
    cout << " Enter the Radius: ";
          
    cin >> r;
          
    pi 3.14;
          
    pi*r*r;
          
    2*pi*r;
          
    cout << " \nThe area is: "<< a;
          
    cout << " \nThe circumference is: "<<c;
          
    getch();
    }

    void Rectangle()
    {
           
    float a,h,w;
           
    cout << " Enter The Hieght: ";
           
    cin >> h;
           
    cout << " Enter The Width: ";
           
    cin >> w;
           
    0.5*h*w;
           
    cout << "The area is: "<<a;
           
    getch();
           
    }

    void Square()
    {
         
    float s,a;
         
    cout << " Enter the side: ";
         
    cin >> s;
         
    s;
         
    cout << " The answer is : "<< a;
         
    getch();
    }

    void Sphere()
    {
         
    float r,pi,sp;
         
    cout << " Enter The Radius: ";
         
    cin >> r;
         
    pi 3.1416;
         
    sp = (4/3)*pi*r*r*r;
         
    cout << " the Volume is: "<< sp;
         
    getch ();
         
    }
     
  10. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Not sure if that's a question; if it is, just wrap the contents of main in a for(;;) block and add a menu option, e.g. 8 Quit, and make case 8 return 0; Then the program will quit when they select 8; this is better than having a separate Y/N prompt to continue or quit. Think about the design, if you have several tasks to perform with the program, you would want to run them one after the other then select quit when you're done; you don't want to be asked whether or not to continue after every operation.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice