Thread: Beginner needing help in C language

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    19

    Beginner needing help in C language

    I am having problems with my code. I thought it was right, but the compiler disagrees with me. My code is posted below along with the errors that I am receiving. Any help would be greatly appreciated and I thank you beforehand. I just dont know what I am doing wrong!

    Code:
    #include <stdio.h> 
    
    int main(void) 
    { 
    int sum = 0; 
    int product = 0; 
    int curr; 
    char letter; 
    
    do 
    { 
    printf("CS2432 Program 3 Menu Program\n"); 
    printf("A. Calculate the sum of a series of whole numbers.\n"); 
    printf("B. Calculate the product of a series of whole numbers.\n"); 
    printf("C. Exit the program\n"); 
    printf("Please enter your choice:\n"); 
    scanf("%c", &letter); 
    
    if(char != 'a' || char != 'b' || char != 'c' || char != 'A' || 
    char != 'B' || char != 'C') 
    { 
    printf("Error: Invalid input! Try Again!\n"); 
    break; 
    } 
    else 
    switch(&letter) 
    { 
    case 'a': case 'A': 
    printf("Enter the integers to be summed(0 to exit):\n"); 
    while(curr != 0) 
    { 
    scanf("%d", &curr); 
    sum += curr; 
    } /*end of while statement*/ 
    
    printf("The total is %d\n", sum); 
    return sum; 
    break; 
    
    case 'b': case 'B': 
    printf("Enter the integers to be multiplied(1 to exit):\n"); 
    while(curr != 1) 
    { 
    scanf("%d", &curr); 
    product *= curr; 
    } /*end of while statement*/ 
    
    printf("The product is %d\n", product); 
    return product; 
    break; 
    
    case 'c': case 'C': 
    /*exit program*/ 
    
    
    } /*end of switch statement*/ 
    
    } /*end of do statement*/ 
    
    } /*end of main statement*/
    Here are the errors:

    program3.c:28: error: parse error before "char"
    program3.c:34: error: parse error before "else"
    program3.c:49: error: case label not within a switch statement
    program3.c:49: error: case label not within a switch statement
    program3.c:61: error: case label not within a switch statement
    program3.c:61: error: case label not within a switch statement
    program3.c:64: warning: deprecated use of label at end of compound statement
    program3.c:32: error: break statement not within loop or switch
    program3.c:47: error: break statement not within loop or switch
    program3.c:59: error: break statement not within loop or switch
    program3.c: At top level:
    program3.c:66: error: parse error before '}' token

  2. #2
    Registered User Markallen85's Avatar
    Join Date
    Nov 2002
    Posts
    53
    Here are a few probs to highlight:

    1- first two errors, where on earth did "char" come from. the letter was read into the letter variable

    2- it's probably a good idea to define a length for the letter string (in this case char letter[2] is enough, but I always tend to lnger strings to be safer)

    3- the if char!= to a valid letter statement is wrong. That will trigger invalid input under all circumstances (if you enter 'a', the character is not equal to bcABC so would triiger the statement. I think:

    Code:
    if(!(letter=='a'||letter=='b'||letter=='c'))
    continued for all six would work better

    4- why switch(&letter) ? what's the ampersand meant to do?, also, that will return a string value, you then check integers in your switch statement. I would try:

    switch(letter[0])

    to retrieve an integer

    5- you need to open the else statement.

    you have:
    Code:
    if()
      {
      ....
      }
      else
      switch()
        {
        ....
        }
    it should be:

    Code:
    if()
      {
      ....
      }
    else
      {
      switch()
        {
         ....
        }
      }
    this would account for the "not a switch" errors, and possibly the final parse error, depending on compiler


    a final note. you don't need the break; when you have just called return. the return statement closes the function (or it has in my experience) so the break statement never runs anyway.

    EDIT:
    you should probably initialize curr to a value as well, just to keep everything in order (probably won't make a difference, but keeping things clean never hurts)
    /EDIT


    That's everything I can see immediatley. Hope it helps

    -mark
    "never argue with an idiot, they will drag you down to their level and beat you with experience"

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Markallen85
    4- why switch(&letter) ? what's the ampersand meant to do?, also, that will return a string value, you then check integers in your switch statement.
    Not exactly. The & in this case gives them the address of the variable, not what the variable contains. It doesn't have anything to do with a string. The & operator as used in this method is for retrieving the address of a variable. (The other use is a bitwise and, but since there is no lvalue there, it is taken as the 'address of' operator.)

    Originally posted by Markallen85
    5- you need to open the else statement.
    No, actually you don't. An if, and also an else, take the next code block as their own. Thus:
    Code:
    if( foo )
        happens_if_foo( );
    else
        happens_if_not_foo( );
    always_happens( );
    Above we have three functions. The first will execute if foo evaluates to true, the second if it doesn't, and the third will always happen. If you do not proved braces blocks, then the if/else only take the next code statement.
    Code:
    if( foo )
    {
        happens_if_foo( );
        so_does_this( );
    }
    else
    {
        happens_if_not_foo( );
        so_does_this( );
    }
    always_happens( );
    Here we use braces blocks to have multiple things happen in one shot for each if and else.

    In the case of the switch statement, the entire switch statement is considered one single statement. No matter how many lines the switch statement contains. One 'switch' is considered a single code block. Thus, we could do:
    Code:
    if( foo == 0 )
        switch ( bar )
        {
            case 1: do_stuff1( );
            ...
            case 100: do_stuff100( ); 
        }
    else
        switch (  foo )
        {
            case 1: do_other_stuff1( );
            ...
            case 100: do_other_stuff100( );
        }
    And it would all work correctly. If foo was 0, the first switch would execute correctly. Otherwise the second switch would execute.

    The initial problem they have is they got mixed up on their variable names. The first if statement should be using 'letter' instead of 'char'. Since 'char' is a C data type, it gives you an error because it thinks you're trying to declare variables there, which you cannot do.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why C Matters
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 136
    Last Post: 01-16-2008, 09:09 AM
  2. C++(new to any computer language) beginner
    By bba in forum C++ Programming
    Replies: 3
    Last Post: 12-29-2005, 11:09 AM
  3. Beginner Needing Help
    By MadProfessor in forum C Programming
    Replies: 4
    Last Post: 08-08-2005, 04:31 PM
  4. What language did they make Java in?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 07-03-2005, 04:18 PM
  5. Language of choice after C++
    By gandalf_bar in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 06-15-2004, 01:20 AM