0

This is an extensive problem. "How to get input including only character and blank". I misunderstand the requirements at first and try to control the input, I have stuck in it for 2 days.

I post my code here. But there's an error in c:19:15 "uninitialized variable --> name". I don't know why I can't get the name value after the for loop in c:23

Anyone help me please !!! Thanks in advance

#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>

char* getname(void);
int main(void)
{        
    char* s = getname();
    printf("%s\n", s);        
}

char* getname(void)
{    
    int n=1;
    char* name;
    do    
    {
        char* name = GetString();
        for (int i = 0; i < strlen(name); i++)
        {
            int a = isalpha(name[i]);
            int b = isblank(name[i]);
            if (a != 0 || b != 0)
                n = n*1;
            else 
            {   
                n = n*0;                
            }           
        }
    }
    while (n == 0);
    return name;
}     
1

Two problems here. First, you're getting a warning on the unitialized name, easily corrected by setting it = NULL. Compiler flags are set to treat all warnings as errors, so you have to do something.

The second and more significant problem is that you are redeclaring name inside of another set of curly braces that define scope. This causes the second name variable to override the first one. Once you leave the do/while loop, the second name var disappears and the first one becomes active again, but it was never given a value.

To fix this, just remove the char * from the statement.

If this answers your question, please click on the check to accept. Let's keep up on forum housekeeping. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .