Thread: problem with chars and function call

  1. #1
    Registered User
    Join Date
    Jul 2019
    Posts
    4

    problem with chars and function call

    Hello,I'm learning C and I've got a question. what is wrong with my code? I expect that the users enter sunny, cloudy, rainy and the program to pick the first letter. Then with that first letters fo to swit function and print.

    Code:
    #include <stdio.h>
    #define TAMANIO 10
    void swit(char st[TAMANIO]);
    int main () {
        int i;
        char str[TAMANIO];
    
    
        printf("Enter your weather - sunny - cloudy- rainy: \n");
        for (i = 0;i < TAMANIO ;i++  ){
        scanf("%s",&str[i]);
        if (str[i] == EOF) {
        i = (TAMANIO -1);
        } //IF user enter eof breaks the for cycle.
        }
        swit(str[0]);
    }
    void swit(char st[TAMANIO]) {
    
    
        switch (st[0]) {
            case 's':
                printf("pick up your sunscreen: ");
            break;
    
    
            case 'c':
                printf("pick up your jacket");
            break;
    
    
            case 'r':
                printf("pick up your umbrella");
            break;
    
    
            default:
                printf("wrong weather! ");
            break;
        }//end swith
    }

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Darknahu View Post
    Hello,I'm learning C and I've got a question. what is wrong with my code?
    Bad indentation and wrong usage of standarized functions...

  3. #3
    Registered User
    Join Date
    Jul 2019
    Posts
    4
    Quote Originally Posted by flp1969 View Post
    Bad indentation and wrong usage of standarized functions...
    Can you help me?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    scanf("%s",&str[i]);
    The above should give a warning on most GCC compilers.
    &str[i] is a char not a C-string.

    You likely want "str" instead of "&str[i]".

    Edit: Learn how to turn on compiler warnings!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Jul 2019
    Posts
    4
    Quote Originally Posted by stahta01 View Post
    Code:
    scanf("%s",&str[i]);
    The above should give a warning on most GCC compilers.
    &str[i] is a char not a C-string.

    You likely want "str" instead of "&str[i]".

    Edit: Learn how to turn on compiler warnings!

    Tim S.
    I tried this, still having problems:

    Code:
    #include <stdio.h>
    #define TAMANIO 10
    void swit(char st);
    int main () {
        //int i;
        char str[TAMANIO];
        printf("Enter your weather - sunny - cloudy- rainy: \n");
       // for (i = 0;i < 10 ;i++  ){
        scanf("%s",str);
        //if (str[i] == EOF) {
        //i = (TAMANIO -1);
        //} //IF user enter eof breaks the for cycle.
        //}
        swit(str[TAMANIO]);
    }
    void swit(char st){
    
    
    
    
        switch (st) {
            case 'sunny':
                printf("pick up your sunscreen: ");
            break;
    
    
    
    
            case 'cloudy':
                printf("pick up your jacket");
            break;
    
    
    
    
            case 'rainy':
                printf("pick up your umbrella");
            break;
    
    
    
    
            default:
                printf("wrong weather! ");
            break;
        }//end swith
    }

  6. #6
    Registered User
    Join Date
    May 2019
    Posts
    214
    First, if your compiler has alternate versions of scanf, consider using them. If the user just continues typing more than 10 characters, you'll have a crash.

    That said, you have two basic problem.

    First, this:

    Code:
    swit(str[TAMANIO]);
    This call to the function swit provides the character at index 10 (TAMANIO) in the str array, but there isn't one. That should crash (sometimes it won't, but it's bad to do this).

    You are actually asking for one character past the end of str

    What you meant was str[0], the first character in the string.

    Next, constants for the switch cases must be integer or related types (a character qualifies), but you've entered full words in each case.

    Try 's' insead of 'sunny' (and similar for the others).

    That at least moves you forward. There's more, but for now, that should do.

  7. #7
    Registered User
    Join Date
    Jul 2019
    Posts
    4
    Quote Originally Posted by Niccolo View Post
    First, if your compiler has alternate versions of scanf, consider using them. If the user just continues typing more than 10 characters, you'll have a crash.

    That said, you have two basic problem.

    First, this:

    Code:
    swit(str[TAMANIO]);
    This call to the function swit provides the character at index 10 (TAMANIO) in the str array, but there isn't one. That should crash (sometimes it won't, but it's bad to do this).

    You are actually asking for one character past the end of str

    What you meant was str[0], the first character in the string.

    Next, constants for the switch cases must be integer or related types (a character qualifies), but you've entered full words in each case.

    Try 's' insead of 'sunny' (and similar for the others).

    That at least moves you forward. There's more, but for now, that should do.
    I was able to solve it, thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with function call
    By bluetxxth in forum C Programming
    Replies: 6
    Last Post: 10-07-2010, 09:40 AM
  2. Function call problem
    By Swerve in forum C++ Programming
    Replies: 7
    Last Post: 11-17-2009, 09:16 PM
  3. Replies: 5
    Last Post: 10-17-2006, 08:54 AM
  4. Noob problem - function call
    By Ultraman in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2006, 02:28 AM
  5. Problem with a function call
    By damonbrinkley in forum C Programming
    Replies: 2
    Last Post: 06-09-2003, 04:07 PM

Tags for this Thread