function switch help

This is a discussion on function switch help within the C Programming forums, part of the General Programming Boards category; hey guys I'm new here and pretty new to programming and needed some insight. Here's a little background : I'm ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    4

    function switch help

    hey guys I'm new here and pretty new to programming and needed some insight. Here's a little background : I'm writing a program that asks a user to input their class number (CRN) but is limited to only 3 classes. In my function that asks the user to input their crn. For some reason its not storing the data. I dont know if I am over looking something or what.
    Here's the function:
    Code:
    int getClass (int number)
    {   
         int crn1=0, crn2=0, crn3=0, credit1=0, credit2=0, credit3=0;
        
             while(number < 1 || number >= 4)
             {
              printf ("Enter how many courses-up to 3 : ");
              scanf ("%d",&number);
              
              switch (number)
              {
               case 1: printf ("Enter the first course number : ");
                       scanf ("%d",&crn1); break;
               case 2: printf ("Enter the first course number : ");
                       scanf ("%d",&crn1);
                       printf ("Enter the second course number : ");
                       scanf ("%d", &crn2); break;
               case 3: printf ("Enter the first course number : ");
                       scanf ("%d",&crn1);
                       printf ("Enter the second course number : ");
                       scanf ("%d", &crn2);
                       printf ("Enter the third course number : ");
                       scanf ("%d", &crn3); break;
               default: printf ("Invalid number! Try again. \n");
              }
             }
             
             switch (crn1)
             {
               case 4587: credit1 = 4; break; 
               case 4599: credit1 = 3; break;
               case 8997: credit1 = 1; break;
               case 9696: credit1 = 3; break;
               case 5587: credit1 = 5; break;
               case 7599: credit1 = 3; break;
               case 1997: credit1 = 3; break;
               case 2696: credit1 = 1; break;
             }
             
             switch (crn2)
             {
              case 4587: credit2 = 4; break; 
              case 4599: credit2 = 3; break;
              case 8997: credit2 = 1; break;
              case 9696: credit2 = 3; break;
              case 5587: credit2 = 5; break;
              case 7599: credit2 = 3; break;
              case 1997: credit2 = 3; break;
              case 2696: credit2 = 1; break;
             }
             
             switch (crn3)
             {
              case 4587: credit3 = 4; break; 
              case 4599: credit3 = 3; break;
              case 8997: credit3 = 1; break;
              case 9696: credit3 = 3; break;
              case 5587: credit3 = 5; break;
              case 7599: credit3 = 3; break;
              case 1997: credit3 = 3; break;
              case 2696: credit3 = 1; break;
             }
         
    }
    I added printf("crn1:%d", crn1); to the int main () after that function to see what it stores as crn1 and it says 0. So I know its not returning it. I just dont know why? Thanks again!

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by etbjr182 View Post
    I added printf("crn1:%d", crn1); to the int main () after that function to see what it stores as crn1 and it says 0. So I know its not returning it. I just dont know why? Thanks again!

    your crn1 variable is local inside the getClass function, so how do you plan to print it in the main function?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    Quote Originally Posted by vart View Post
    your crn1 variable is local inside the getClass function, so how do you plan to print it in the main function?
    so should I make another function just for the crn?

    my print function is like this (still working on it)
    Code:
    void printCourseData (int crn1, int crn2, int crn3)
    {     
         switch (crn1)
         {
            case 4587 : printf ("\t\t%d\t%s\t%d\t$%.2lf\n\n",crn1,"MAT 236",4,4*120.25);
            break;
            case 4599 : printf ("\t\t%d\t%s\t%d\t$%.2lf\n\n",crn1,"COP 220",3,3*120.25);
            break;
            case 8997 : printf ("\t\t%d\t%s\t%d\t$%.2lf\n\n",crn1,"GOL 124",1,120.25);
            break;
            case 9696 : printf ("\t\t%d\t%s\t%d\t$%.2lf\n\n",crn1,"COP 100",3,3*120.25);
         }
         
         switch (crn2)
         {
            case 4587 : printf ("\t\t%d\t%s\t%d\t$%.2lf\n\n",crn2,"MAT 236",4,4*120.25);
            break;
            case 4599 : printf ("\t\t%d\t%s\t%d\t$%.2lf\n\n",crn2,"COP 220",3,3*120.25);
            break;
            case 8997 : printf ("\t\t%d\t%s\t%d\t$%.2lf\n\n",crn2,"GOL 124",1,120.25);
            break;
            case 9696 : printf ("\t\t%d\t%s\t%d\t$%.2lf\n\n",crn2,"COP 100",3,3*120.25);
         }
         
         switch (crn3)
         {
            case 4587 : printf ("\t\t%d\t%s\t%d\t$%.2lf\n\n",crn3,"MAT 236",4,4*120.25);
            break;
            case 4599 : printf ("\t\t%d\t%s\t%d\t$%.2lf\n\n",crn3,"COP 220",3,3*120.25);
            break;
            case 8997 : printf ("\t\t%d\t%s\t%d\t$%.2lf\n\n",crn3,"GOL 124",1,120.25);
            break;
            case 9696 : printf ("\t\t%d\t%s\t%d\t$%.2lf\n\n",crn3,"COP 100",3,3*120.25);
         }
    }

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    am I just going the wrong way about this? lol Starting to doubt myself lol

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    You need to read up on pointers. It should be a solution to your problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    Quote Originally Posted by Elysia View Post
    You need to read up on pointers. It should be a solution to your problem.
    hahaha actually this is without pointers This is for loops, switches. We are learning pointers next week. Thanks though!

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by etbjr182 View Post
    am I just going the wrong way about this?
    Probably. Why don't you post the relative parts of main?

    Also, it sounds like your issue is "how do I use a function to modify a value from another function, so that both functions respect the changes?" If so, you should simplify your life by writing a short program with two functions (main, plus some other one), where you have a number in main(), send it to your testfunc(), and then check to see what happened back in main(). Once you get that to work, you should keep it so that next time you can't figure out/remember, you have a simple example you wrote yourself to use. And if you need to ask a question here, it will be easily and quickly solved.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Then you are limited to returning one number or using global variables (bad) or simply not breaking it out into functions at all.
    The problem is that the variables are local - they exist only inside the function. This is the same with arguments as well - a copy is made inside the function and the value you pass is assigned so no information escapes at all.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    global variables (bad)
    Is that a religious thing? I would observe that a function is a global variable, and while they may not always be good, I won't condemn them.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Global variables is not a solution to this problem.
    They have their uses, yes, but using them wrong will only cause problems. That is why they should be used carefully. In this case, I'd call them bad.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21