Optimizing C++ Case Statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • clockworx05
    New Member
    • Sep 2007
    • 11

    #16
    ok here is my code now: Same error as above:

    Code:
    /*
    Write a function called foo that asks the user for their age. 
    Pass the age value to a function called showAge that uses a select case structure to print out the following: 
    If the age is 0-12 print out "You are still a child. If the age is 13 - 19 print out "You are a teenager. 
    If the age is 20 - 40 print "You are an adult" anything else print "You are starting to become mature"
    */
    
    #include <iostream>
    using namespace std; 
    int foo (int x);
    
    
    int showAge (int &x);
    int main()
    {
    
    	foo();
    	
    return 0;
    }
    int foo(int x)
    {
    	
    	cout << "Please Enter Your Age:" << endl;
    	cin >> x;
    	showAge();
    return x;
    }
    int showAge (int  &x)
    {
    if(x>0&&x<12) x=1;
    if(x>=13&&x<19) x=2;
    if(x>20) x=3;
    switch ( x )
        {
    			case 1:
    				  cout << "You are still a child" << endl;	
    			    break;
    			case 2:
    					cout << "You are a teenager." << endl;
    				break;
    			case 3:
    				cout << "You are an adult" << endl;
    			  break;
    			 	
    		
            default:
                cout << "ou are starting to become mature or too old" << endl;
    
    			return x;
        }

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #17
      Originally posted by Savage
      He can use switch statements,but after:

      [CODE="cpp"]int k;
      if(age>0&&age<= 12) k=1;
      if(age>=13&&age <=19) k=2;
      .
      .
      .

      switch(k)
      {

      //blah,blah,blah. .

      .
      .
      .
      [/CODE]
      Savage
      That'd be silly because why postpone what you can deal with right after the
      if-clause itself?

      kind regards,

      Jos

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #18
        Originally posted by clockworx05
        ok here is my code now: Same error as above:

        Code:
        /*
        Write a function called foo that asks the user for their age. 
        Pass the age value to a function called showAge that uses a select case structure to print out the following: 
        If the age is 0-12 print out "You are still a child. If the age is 13 - 19 print out "You are a teenager. 
        If the age is 20 - 40 print "You are an adult" anything else print "You are starting to become mature"
        */
         
        #include <iostream>
        using namespace std; 
        int foo (int x);
        
        
        int showAge (int &x);
        int main()
        {
        
        	foo();
        	
        return 0;
        }
        int foo(int x)
        {
        	
        	cout << "Please Enter Your Age:" << endl;
        	cin >> x;
        	showAge();
        return x;
        }
        int showAge (int  &x)
        {
        if(x>0&&x<12) x=1;
        if(x>=13&&x<19) x=2;
        if(x>20) x=3;
        switch ( x )
            {
        			case 1:
        				  cout << "You are still a child" << endl;	
        			    break;
        			case 2:
        					cout << "You are a teenager." << endl;
        				break;
        			case 3:
        				cout << "You are an adult" << endl;
        			  break;
        			 	
        		
                default:
                    cout << "ou are starting to become mature or too old" << endl;
        
        			return x;
            }

        Anything odd here:

        [CODE="cpp"]
        int foo(int x)
        {

        cout << "Please Enter Your Age:" << endl;
        cin >> x;
        vvvvvvvvvvvvvvv
        --------------->\\showAge();//<-------------------
        ^^^^^^^^^^^^^^^
        return x;
        }[/CODE]

        ???

        Savage

        Comment

        • Savage
          Recognized Expert Top Contributor
          • Feb 2007
          • 1759

          #19
          Originally posted by JosAH
          That'd be silly because why postpone what you can deal with right after the
          if-clause itself?

          kind regards,

          Jos
          Writing around 40 case statements is boring,silly and everything bad you can think of.

          Savage

          Comment

          • clockworx05
            New Member
            • Sep 2007
            • 11

            #20
            now when i do that to foo it says:

            variable "x" is being used without defined:

            /*
            Write a function called foo that asks the user for their age.
            Pass the age value to a function called showAge that uses a select case structure to print out the following:
            If the age is 0-12 print out "You are still a child. If the age is 13 - 19 print out "You are a teenager.
            If the age is 20 - 40 print "You are an adult" anything else print "You are starting to become mature"
            */

            #include <iostream>
            using namespace std;
            int foo (int x);


            int showAge (int &x);
            int main()
            {
            int x;
            foo(x);

            return 0;
            }
            int foo(int x)
            {

            cout << "Please Enter Your Age:" << endl;
            cin >> x;
            showAge(x);
            return x;
            }
            int showAge (int &x)
            {
            if(x>0&&x<12) x=1;
            if(x>=13&&x<19) x=2;
            if(x>20) x=3;
            switch ( x )
            {
            case 1:
            cout << "You are still a child" << endl;
            break;
            case 2:
            cout << "You are a teenager." << endl;
            break;
            case 3:
            cout << "You are an adult" << endl;
            break;


            default:
            cout << "ou are starting to become mature or too old" << endl;

            return x;
            }


            }

            Comment

            • clockworx05
              New Member
              • Sep 2007
              • 11

              #21
              the error is:

              uninitialized local variable 'x' used

              Comment

              • sicarie
                Recognized Expert Specialist
                • Nov 2006
                • 4677

                #22
                Originally posted by clockworx05
                the error is:

                uninitialized local variable 'x' used
                Move your cin to main.

                Comment

                • clockworx05
                  New Member
                  • Sep 2007
                  • 11

                  #23
                  Ok i fixed it :)

                  Thanks everyone for the help;


                  Code:
                  /*
                  Write a function called foo that asks the user for their age. 
                  Pass the age value to a function called showAge that uses a select case structure to print out the following: 
                  If the age is 0-12 print out "You are still a child. If the age is 13 - 19 print out "You are a teenager. 
                  If the age is 20 - 40 print "You are an adult" anything else print "You are starting to become mature"
                  */
                  
                  #include <iostream>
                  using namespace std; 
                  int foo (int x);
                  int showAge (int &x);
                  int main()
                  {
                  int x = 0;
                  	foo(x);
                  	
                  return 0;
                  }
                  int foo(int x)
                  {
                  	
                  	cout << "Please Enter Your Age:" << endl;
                  	cin >> x;
                  	showAge(x);
                  return x;
                  }
                  int showAge (int  &x)
                  {
                  if(x>0&&x<12) x=1;
                  if(x>=13&&x<19) x=2;
                  if(x>20&&x<40) x=3;
                  switch ( x )
                      {
                  			case 1:
                  				  cout << "You are still a child" << endl;	
                  			    break;
                  			case 2:
                  					cout << "You are a teenager." << endl;
                  				break;
                  			case 3:
                  				cout << "You are an adult" << endl;
                  			  break;
                  			 	
                  		
                          default:
                              cout << "You are starting to become mature or too old" << endl;
                  
                      }
                  
                  
                  			return x;
                  }

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #24
                    Originally posted by Savage
                    Writing around 40 case statements is boring,silly and everything bad you can think of.

                    Savage
                    You didn't read my reply (in the context of yours) carefully enough; btw, the
                    amount of typing is nowhere proportional to the (in)efficiency of an algorithm
                    big-Oh wise speaking.

                    kind regards,

                    Jos

                    Comment

                    • Savage
                      Recognized Expert Top Contributor
                      • Feb 2007
                      • 1759

                      #25
                      Originally posted by JosAH
                      You didn't read my reply (in the context of yours) carefully enough; btw, the
                      amount of typing is nowhere proportional to the (in)efficiency of an algorithm
                      big-Oh wise speaking.

                      kind regards,

                      Jos
                      That's just what they want us to believe. :P

                      Just for fun using high accuracy counter(QueryPe rformanceFreuqe ncy and QueryPerformanc eCounter),I discovered that my solution is faster for 142 ticks.(Avg for my sol:2483,Avg. for pure case sol.:2625 dif=-142 ticks)


                      :P

                      regards,

                      Savage

                      Comment

                      Working...