Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   combining (http://www.go4expert.com/forums/combining-t18862/)

shizzle08 7Aug2009 17:10

combining
 
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

shabbir 7Aug2009 18:57

Re: combining
 
Put them into separate function and use the user to select which one to be executed.

SaswatPadhi 7Aug2009 19:05

Re: combining
 
@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;

shizzle08 7Aug2009 20:02

Re: combining
 
i dont have any idea what your telling can u please edit my prog..? i newbie

xpi0t0s 9Aug2009 01:06

Re: combining
 
(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)
}


shizzle08 10Aug2009 16:33

Re: combining
 
this is how it should be sir...

Quote:

1. Basic Calculator
2. Days
3. Positive or Negative
4. odd or even
please enter that number that u want to use:
then when they chose 1 the basic calculator must be seem lyk this

Quote:

Enter the first number: x
Enter the second number: x

The sum is: x
The difference is: x
The product is: x
Then quotient is: x
and then it will ask them

Quote:

would you like to use it again yes or no

shizzle08 10Aug2009 16:39

Re: combining
 
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

xpi0t0s 10Aug2009 19:26

Re: combining
 
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.

shizzle08 11Aug2009 00:14

Re: combining
 
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 Code:

#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 ();
     



xpi0t0s 11Aug2009 13:44

Re: combining
 
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.


All times are GMT +5.5. The time now is 06:11.