Hey, need some quick help.

This is a discussion on Hey, need some quick help. within the C Programming forums, part of the General Programming Boards category; Hey i have completed the majority of my work needed but i just have these two problems. They are both ...

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

    Hey, need some quick help.

    Hey i have completed the majority of my work needed but i just have these two problems. They are both very short. I hope someone will be able to help me with the codes.

    1. Write the definition of a function printAttitude, which has an int parameter and returns nothing. The function prints a message to standard output depending on the value of its parameter.

    If the parameter equals 1, the function prints disagree.
    If the parameter equals 2, the function prints no opinion.
    If the parameter equals 3, the function prints agree.
    In the case of other values, the function does nothing.

    Each message is printed on a line by itself.

    I tried this but it is incorect.....


    Code:
    void printAttitude(int k)
    {
      int k;
      scanf("%d", &k);
      if(k=1)
        printf("disagree\n");
      else if(k=2)
        printf("no opinion\n");
      else if(k=3)
        printf("agree\n");
    }
    For the code above i get an error that says "Make sure you have a set of properly working conditionals in your code."


    and 2. Write the definition of a function printGrade, which has a char parameter and returns nothing. The function prints on a line by itself the message string Grade: followed by the char parameter (printed as a character) to standard output. Don't forget to put a new line character at the end of your line.

    Thus, if the value of the parameter is 'A', the function prints out Grade: A

    I have no idea where to begin this one.
    Thanx for all your help guys.
    Last edited by stehigs321; 10-06-2003 at 01:36 PM.

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    61
    1. There's no reason to redeclare k twice and in if-else tests use '==' operator to compare int variables.
    Example:
    Code:
    void printAttitude(int k) /* declaration of k */
    {
      if(k == 1)
        printf("disagree\n");
      else if(k == 2)
        printf("no opinion\n");
      else if(k == 3)
        printf("agree\n");
    }
    and call it using printfAttitude(k) after getting the value of k.

    2. Use printf to print the value of the function's argument:
    printf("%c", grade), where grade is the char variable passed to this function.
    Last edited by cc0d3r; 10-06-2003 at 01:59 PM.
    $ENV: FreeBSD, gcc, emacs

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    You defined the variable k 2 times. You should only define it 1 time. Also why is there a scanf? You don't need to read any input in your function.
    So delete this two lines:
    int k;
    scanf("%d", &k);

    You should also remember the difference between = and ==. You need the second form for comparisons in if statements and the first form for assignments.

    Code:
    void printGrade(char c)
    {
       printf("Grade: %c\n",c);
    }

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    56

    thanks so much

    thank you guys so much. I may have other questions down the line, and i hope youll be able to help me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 11:33 AM
  2. Questions on basic Quick Sort
    By Weng in forum C++ Programming
    Replies: 4
    Last Post: 12-16-2003, 10:06 AM
  3. Quick Sort Help
    By NavyBlue in forum C Programming
    Replies: 1
    Last Post: 03-02-2003, 10:34 PM
  4. Hey i got a quick Question
    By cis in forum C Programming
    Replies: 1
    Last Post: 03-15-2002, 10:12 PM
  5. Replies: 34
    Last Post: 11-26-2001, 01:17 PM

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