5 Replies - 311 Views - Last Post: 26 February 2009 - 12:31 PM Rate Topic: -----

#1 lexmarkkiller  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-February 09

Switch to other funtion thingy

Post icon  Posted 26 February 2009 - 06:46 AM

I am trying to do a switch here but i cant get it to work i finally got it to compile after looking on these forums (thanks) but as you can see i cant get it to switch from the main to the add and then ill worry about going back to the main again well if anyone understood that thanks for looking

why im doing this you might ask ...i had a C++ class a while ago and now im getting back into the fassion of it, it is kinda fun

#include <iostream>
#include <string>

using namespace std;
int a;
int b;
int c;
int d;
int result;
int add();
int sub();
int mult();
int dev();
int choice;

int main()
{
	cout << "Please make a selection to:" << endl;
	cout << "1. Add" << endl << "2. Subtract" << endl;
	cout << "3. Multiply" << endl << "4.Devide" << endl << endl;
	cout << "Please make a selection: ";
	cin >> choice;

	switch (choice)
	{
		   case '1': int add();
		   break;
	
		   case '2': int sub();
		   break;
	
		   case '3': int mult(); 
		   break;
	
		   case '4': int dev();
		   break;
	}
}
	

int add()
{
	cout << endl << "Please select your first number ";
	cin >> a;
	cout << endl << "Please Select your second number ";
	cin >> b;
	a=a;
	b=b;
	result=a+b;
	cout << endl << a << " + " << b << " = " << result;
	system("pause");
}



Is This A Good Question/Topic? 0
  • +

Replies To: Switch to other funtion thingy

#2 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Switch to other funtion thingy

Posted 26 February 2009 - 07:08 AM

Programming is fun, isn't it? :D

You don't need the int part in your function call. And you don't need the ' marks around your cases.

This works (yes, I tested it):

    switch (choice)
    {
           case 1: add();
           break;
    
           case 2: sub();
           break;
    
           case 3: mult(); 
           break;
    
           case 4: dev();
           break;
    }


p.s. since int sub() appears in blue, I think it might be a reserved word (so you may not be able to use it like you are trying to do) but I might be wrong about that.

I hope this helps you! :)

This post has been edited by OliveOyl3471: 26 February 2009 - 07:16 AM

Was This Post Helpful? 1

#3 Armie  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 79
  • Joined: 09-August 08

Re: Switch to other funtion thingy

Posted 26 February 2009 - 07:09 AM

I think the problem is with the case.

First, output the value of choice after cin.

Second, try to use case 1: etc instead of case '1':

or try

char input[2];
int choice;

cin >> input;
choice = atoi(input);



Didn't test it, but I think it might work.
Was This Post Helpful? 0
  • +
  • -

#4 lexmarkkiller  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-February 09

Re: Switch to other funtion thingy

Posted 26 February 2009 - 11:43 AM

thankyou for the help taking away the int and ' worked but i have a question with the same code afer the
#include <iostream>
#include <string>

using namespace std;
int a;
int b;
int c;
int d;
int result;
int add();
int subt();
int mult();
int dev();
int choice;

int main()
{
	cout << "Please make a selection to:" << endl;
	cout << "1. Add" << endl << "2. Subtract" << endl;
	cout << "3. Multiply" << endl << "4.Devide" << endl << endl;
	cout << "Please make a selection: ";
	cin >> choice;
	switch (choice)
	{
		   case 1: add();
		   break;
	
		   case 2: subt();
		   break;
	
		   case 3: mult(); 
		   break;
	
		   case 4: dev();
		   break;
	}
}
	

int add()
{
	cout << endl << "Please select your first number ";
	cin >> a;
	cout << endl << "Please Select your second number ";
	cin >> b;
	a=a;
	b=b;
	result=a+b;
	cout << endl << a << " + " << b << " = " << result << " ";
}


how do i make it zap back to the main function to restart right now im just using systeym("pause") at the end...yeah that code made this post really long sorry but if you can help thanks
Was This Post Helpful? 0
  • +
  • -

#5 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Switch to other funtion thingy

Posted 26 February 2009 - 12:30 PM

Actually, it will go back to the main after your switch. It is just that you have no code after your switch for it to execute. Add some and you'll see. When you get to the function call in your program, control temporarily leaves main and executes the function. When it is done with that, it goes back to the next line after your function call.

See what this does (I commented out the parts we aren't using yet, just so it would compile for me):
#include <iostream>
#include <string>

using namespace std;
int a;
int b;
int c;
int d;
int result;
int add();
//int subt();
//int mult();
//int dev();
int choice;

int main()
{
    cout << "Please make a selection to:" << endl;
    cout << "1. Add" << endl << "2. Subtract" << endl;
    cout << "3. Multiply" << endl << "4.Devide" << endl << endl;
    cout << "Please make a selection: ";
    cin >> choice;

    switch (choice)
    {
           case 1: add();
           break;
    
           case 2:// subt();
           break;
    
           case 3:// mult(); 
           break;
    
           case 4:// dev();
           break;
    }
    //let's add some code here that will run after your function is done
    cout << "Now we're back in main. What shall we do next?" << endl;
    
    //I also moved your pause to the end of your main function
    system("pause"); 
}//this is the end of your main function

int add()
{
    cout << endl << "Please select your first number ";
    cin >> a;
    cout << endl << "Please Select your second number ";
    cin >> b;
    a=a;
    b=b;
    result=a+b;
    cout << endl << a << " + " << b << " = " << result << endl;
    //system("pause");
}




Now if you want to keep using your switch after they select an option and the code for that option is finished, then you can add a loop.

#include <iostream>
#include <string>

using namespace std;
int a;
int b;
int c;
int d;
int result;
int add();
//int subt();
//int mult();
//int dev();
int choice;

char quit; //to continue or end the loop as user chooses

int main()
{
    while(quit != 'q' && quit != 'Q')
    {
      cout << "Please make a selection to:" << endl;
      cout << "1. Add" << endl << "2. Subtract" << endl;
      cout << "3. Multiply" << endl << "4.Devide" << endl << endl;
      cout << "Please make a selection: ";
      cin >> choice;

      switch (choice)
      {
           case 1: add();
           break;
    
           case 2:// subt();
           break;
    
           case 3:// mult(); 
           break;
    
           case 4:// dev();
           break;
      }
      
      //see if user wants to continue
      cout << "Enter C to continue or Q to quit." << endl;
      cin >> quit;
      
    }//end of while loop
    

      //let's add some code here that will run after your function is done
      cout << "Now we're back in main. What shall we do next?" << endl;
      
      //I also moved your pause to the end of your main function
      system("pause"); 
      

}//this is the end of your main function

int add()
{
    cout << endl << "Please select your first number ";
    cin >> a;
    cout << endl << "Please Select your second number ";
    cin >> b;
    a=a;
    b=b;
    result=a+b;
    cout << endl << a << " + " << b << " = " << result << endl;
    //system("pause");
}




Your post isn't that long. You should see some of the code we get here! Programmers like reading code anyway.

This post has been edited by OliveOyl3471: 26 February 2009 - 12:40 PM

Was This Post Helpful? 0
  • +
  • -

#6 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Switch to other funtion thingy

Posted 26 February 2009 - 12:31 PM

Vouch, that isn't long at all. That's quite short.

When you hear people saying that programs are thousands upon thousands of lines long, in C (a high level language mind you), they aren't kidding. It is literally in the thousands.

What you have is under 100 (I'm assuming from a glance).

This post has been edited by Hyper: 26 February 2009 - 12:32 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1